-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathcreate-git-tag.mjs
More file actions
79 lines (71 loc) · 1.99 KB
/
create-git-tag.mjs
File metadata and controls
79 lines (71 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* GitHub Script helper to create annotated tags when they do not already exist.
* Expects the following environment variables to be set:
* - INPUT_REPOSITORY: optional owner/repo target, defaults to current repo
* - TAG_NAME: required tag name
* - COMMIT_SHA: required commit sha to tag
* - TAG_MESSAGE: optional message for annotated tag
*/
export async function createGitTag({ github, core, context }) {
const repoInput = process.env.INPUT_REPOSITORY
let owner
let repo
if (repoInput && repoInput.trim().length > 0) {
const parts = repoInput.split('/')
if (parts.length !== 2) {
core.setFailed(`Invalid repository input: ${repoInput}`)
return
}
;[owner, repo] = parts
} else {
;({ owner, repo } = context.repo)
}
const tagName = process.env.TAG_NAME
if (!tagName) {
core.setFailed('Missing tag name input.')
return
}
const commitSha = process.env.COMMIT_SHA
if (!commitSha) {
core.setFailed('Missing commit SHA input.')
return
}
const messageEnv = process.env.TAG_MESSAGE
const message = messageEnv && messageEnv.trim().length > 0 ? messageEnv : tagName
try {
await github.rest.git.getRef({
owner,
repo,
ref: `tags/${tagName}`,
})
core.info(`Tag ${tagName} already exists on ${owner}/${repo}, skipping.`)
return
} catch (error) {
if (error.status !== 404) {
throw error
}
}
const tag = await github.rest.git.createTag({
owner,
repo,
tag: tagName,
message,
object: commitSha,
type: 'commit',
})
try {
await github.rest.git.createRef({
owner,
repo,
ref: `refs/tags/${tagName}`,
sha: tag.data.sha,
})
} catch (error) {
if (error.status === 422 && error.message?.includes('Reference already exists')) {
core.info(`Tag reference refs/tags/${tagName} already exists on ${owner}/${repo}, skipping.`)
return
}
throw error
}
core.info(`Created tag ${tagName} on ${owner}/${repo} at ${commitSha}.`)
}