generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
_release.js
101 lines (91 loc) · 2.3 KB
/
_release.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict'
const github = require('@actions/github')
const semver = require('semver')
const fs = require('fs').promises
async function commit (client, opts) {
const files = await Promise.all(opts.files.map(async (file) => {
const { data: blob } = await client.git.createBlob({
owner: opts.owner,
repo: opts.repo,
content: await fs.readFile(file, { encoding: 'base64' }),
encoding: 'base64'
})
return {
sha: blob.sha,
path: file,
mode: '100644',
type: 'blob'
}
}))
const { data: tree } = await client.git.createTree({
owner: opts.owner,
repo: opts.repo,
tree: files,
base_tree: opts.sha
})
const { data: commit } = await client.git.createCommit({
owner: opts.owner,
repo: opts.repo,
message: opts.commitMessage || 'commit',
tree: tree.sha,
parents: [opts.sha]
})
return commit
}
async function createTag (client, opts) {
const { data: refs } = await client.git.listMatchingRefs({
owner: opts.owner,
repo: opts.repo,
ref: `tags/${opts.tag}`
})
if (refs.find((refObj) => refObj.ref.endsWith(opts.tag))) {
await client.git.updateRef({
owner: opts.owner,
repo: opts.repo,
force: true,
ref: `tags/${opts.tag}`,
sha: opts.sha
})
} else {
await client.git.createRef({
owner: opts.owner,
repo: opts.repo,
ref: `refs/tags/${opts.tag}`,
sha: opts.sha
})
}
}
;(async (token) => {
const client = new github.GitHub(token)
const context = github.context
const tag = context.ref.replace('refs/tags/v', '')
const ver = semver.parse(tag)
try {
const { sha } = await commit(client, {
...context.repo,
sha: context.sha,
commitMessage: `v${tag} build`,
files: [
'dist/index.js',
'dist/index.js.map',
'dist/sourcemap-register.js'
]
})
console.log(`Created commit: ${sha}`)
await createTag(client, {
...context.repo,
tag: `v${ver.major}`,
sha: sha
})
console.log(`Created tag: v${ver.major}@${sha}`)
await createTag(client, {
...context.repo,
tag: `v${ver.major}.${ver.minor}`,
sha: sha
})
console.log(`Created tag: v${ver.major}.${ver.minor}@${sha}`)
} catch (e) {
console.error(e)
throw e
}
})(process.argv[2])