-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpublish.js
147 lines (128 loc) · 4.32 KB
/
publish.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2020-present unTill Pro, Ltd. and Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
const fs = require('fs')
const path = require('path')
const tmp = require('tmp');
var admzip = require('adm-zip');
const execute = require('./common').execute
const github = require('@actions/github')
function genVersion() {
// UTC date-time as yyyyMMdd.HHmmss.SSS
return new Date().toISOString().replace(/T/, '.').replace(/-|:|Z/g, '')
}
function prepareZip(source) {
let zipFile = source
const isDir = fs.lstatSync(source).isDirectory()
if (isDir || path.extname(source) !== '.zip') {
var zip = new admzip()
if (isDir)
zip.addLocalFolder(source)
else
zip.addLocalFile(source)
zipFile = tmp.tmpNameSync({ postfix: '.zip' })
zip.writeZip(zipFile)
}
return zipFile
}
const publishAsMavenArtifact = async function (artifact, token, repositoryOwner, repositoryName) {
if (!fs.existsSync(artifact))
throw new Error(`Artifact "${artifact}" is not found`)
const zipFile = prepareZip(artifact)
const version = genVersion()
// Publish artifact to: com.github.${repositoryOwner}:${repositoryName}:${version}:zip
await execute(`mvn deploy:deploy-file --batch-mode -DgroupId=com.github.${repositoryOwner} \
-DartifactId=${repositoryName} -Dversion=${version} -DgeneratePom=true \
-DrepositoryId=GitHubPackages -Durl=https://x-oauth-basic:${token}@maven.pkg.github.com/${repositoryOwner}/${repositoryName} -Dfile="${zipFile}"`)
if (zipFile !== artifact)
fs.unlinkSync(zipFile)
}
const publishAsRelease = async function (asset, token, keep, repositoryOwner, repositoryName, targetCommitish) {
if (!fs.existsSync(asset))
throw new Error(`Asset "${asset}" is not found`)
if (!fs.existsSync('deployer.url'))
throw new Error(`File "deployer.url" missing`)
const version = genVersion()
const zipFile = prepareZip(asset)
const octokit = github.getOctokit(token)
// Create release (+tag)
const createReleaseResponse = await octokit.rest.repos.createRelease({
owner: repositoryOwner,
repo: repositoryName,
tag_name: version,
target_commitish: targetCommitish,
name: version,
})
console.log(`Release ID: ${createReleaseResponse.data.id}`)
console.log(`Release URL: ${createReleaseResponse.data.html_url}`)
let result = {
release_id: createReleaseResponse.data.id,
release_name: createReleaseResponse.data.name,
release_html_url: createReleaseResponse.data.html_url,
release_upload_url: createReleaseResponse.data.upload_url,
}
// Upload asset
const zipFileHeaders = {
'content-type': 'application/zip',
'content-length': fs.statSync(zipFile).size,
};
const uploadAssetResponse = await octokit.rest.repos.uploadReleaseAsset({
url: createReleaseResponse.data.upload_url,
headers: zipFileHeaders,
name: `${repositoryName}.zip`,
data: fs.readFileSync(zipFile),
});
console.log(`Release asset URL: ${uploadAssetResponse.data.browser_download_url}`)
result.asset_browser_download_url = uploadAssetResponse.data.browser_download_url
// Upload deploy.txt
const deployTxt = Buffer.concat([
Buffer.from(uploadAssetResponse.data.browser_download_url + '\n'),
fs.readFileSync('deployer.url'),
])
const deployTxtHeaders = {
'content-type': 'text/plain',
'content-length': deployTxt.length,
};
await octokit.rest.repos.uploadReleaseAsset({
url: createReleaseResponse.data.upload_url,
headers: deployTxtHeaders,
name: 'deploy.txt',
data: deployTxt,
});
if (zipFile !== asset)
fs.unlinkSync(zipFile)
// get repo list
const releases = await octokit.rest.repos.listReleases({
owner: repositoryOwner,
repo: repositoryName,
})
// Delete old releases (with tag)
if (keep && keep > 0)
releases.data
.filter(release => /^\d{8}\.\d{6}\.\d{3}$/.test(release.name)
&& release.tag_name === release.name)
.sort((a, b) => (b.name > a.name) ? 1 : ((a.name > b.name) ? -1 : 0))
.slice(keep)
.forEach(release => {
console.log(`Delete release ${release.name}`)
octokit.rest.repos.deleteRelease({
owner: repositoryOwner,
repo: repositoryName,
release_id: release.id,
})
octokit.rest.git.deleteRef({
owner: repositoryOwner,
repo: repositoryName,
ref: `tags/${release.tag_name}`,
})
})
return result
}
module.exports = {
publishAsMavenArtifact,
publishAsRelease
}