generated from LizardByte/template-base
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
170 lines (158 loc) · 6.25 KB
/
action.yml
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
name: "Create Release"
description: "A reusable action to create a GitHub release."
author: "LizardByte"
inputs:
allowUpdates:
description: 'An optional flag which indicates if we should update a release if it already exists.'
required: false
default: 'true'
artifactErrorsFailBuild:
description: 'An optional flag which indicates if we should fail the build if there are errors with the artifacts.'
required: false
default: 'false'
artifacts:
description: 'The artifacts to upload.'
required: false
default: '*artifacts/*'
body:
description: 'The body of the release.'
required: false
default: ''
deleteOtherPreReleases:
description: 'Whether to delete other pre-releases.'
required: false
default: 'true'
deletePreReleaseTags:
description: 'Whether to delete other pre-releases tags.'
required: false
default: 'true'
generateReleaseNotes:
description: 'Indicates if release notes should be automatically generated.'
required: false
default: 'true'
keepPreReleaseCount:
description: 'The number of pre-releases to keep. The default is 2 incase there are in process downloads.'
required: false
default: '2'
name:
description: 'The version to create.'
required: true
prerelease:
description: 'Whether the release is a prerelease.'
required: false
default: 'true'
sleepDuration:
description: 'The duration to sleep in seconds before deleting tags.'
required: false
default: '15'
tag:
description: 'The tag to create.'
required: true
token:
description: 'Github Token.'
required: true
runs:
using: "composite"
steps:
- name: Create/Update GitHub Release
if: >-
github.repository == 'LizardByte/create-release-action' ||
(github.event_name == 'push' && github.ref == 'refs/heads/master')
uses: ncipollo/[email protected]
with:
allowUpdates: ${{ inputs.allowUpdates }}
artifactErrorsFailBuild: ${{ inputs.artifactErrorsFailBuild }}
artifacts: ${{ inputs.artifacts }}
body: ${{ inputs.body }}
commit: ${{ github.sha }}
generateReleaseNotes: ${{ inputs.generateReleaseNotes }}
name: ${{ inputs.name }}
prerelease: ${{ inputs.prerelease }}
tag: ${{ inputs.tag }}
token: ${{ inputs.token }}
- name: Sleep
# wait for the new release to be available in the API
if: >-
inputs.deleteOtherPreReleases == 'true' &&
(github.event_name == 'push' && github.ref == 'refs/heads/master')
shell: bash
run: sleep 30
- name: Delete Other PreReleases
if: >-
inputs.deleteOtherPreReleases == 'true' &&
(github.event_name == 'push' && github.ref == 'refs/heads/master')
uses: actions/github-script@v7
env:
DELETE_TAGS: ${{ inputs.deletePreReleaseTags }}
KEEP_LATEST: ${{ inputs.keepPreReleaseCount }}
SLEEP_DURATION: ${{ inputs.sleepDuration }}
with:
github-token: ${{ inputs.token }}
script: |
// process input
const DELETE_TAGS = process.env.DELETE_TAGS.toLowerCase() === 'true';
const KEEP_LATEST = parseInt(process.env.KEEP_LATEST, 10);
const SLEEP_DURATION = parseInt(process.env.SLEEP_DURATION, 10);
console.log(`DELETE_TAGS: ${DELETE_TAGS}`);
console.log(`KEEP_LATEST: ${KEEP_LATEST}`);
let regexPattern = new RegExp('^v(\\d{4,})\\.(\\d{1,4})\\.(\\d{1,6})(\\.(\\d{1,2}))?');
// list releases
const repoOpts = github.rest.repos.listReleases.endpoint.merge({
owner: context.repo.owner,
repo: context.repo.repo,
});
const allReleases = await github.paginate(repoOpts);
allReleases.forEach(release => {
console.log(`Release: ${release.tag_name}`);
console.log(`Is pre-release: ${release.prerelease}`);
console.log(`Matches regex: ${regexPattern.test(release.tag_name)}`);
});
let preReleases = allReleases.filter(release => release.prerelease && regexPattern.test(release.tag_name));
console.log('Matched Pre-release tags:', preReleases.map(release => release.tag_name));
// sort by tag/version number (e.g. v1.2.3 or v1.2.3.4)
preReleases.sort((a, b) => {
const aParts = a.tag_name.split('.').map(Number);
const bParts = b.tag_name.split('.').map(Number);
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
if (aParts[i] === undefined) return -1;
if (bParts[i] === undefined) return 1;
if (aParts[i] < bParts[i]) return -1;
if (aParts[i] > bParts[i]) return 1;
}
return 0;
});
// delete all but the last n pre-releases
for (let i = 0; i < preReleases.length - KEEP_LATEST; i++) {
const release = preReleases[i];
console.log(`Deleting release: ${release.tag_name}`);
try {
await github.rest.repos.deleteRelease({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release.id
});
} catch (error) {
console.error(`Failed to delete release: ${release.tag_name}`);
console.error(error);
}
}
// sleep to allow any on release deleted event workflow runs to be created
// if the tag is deleted before the workflow run is created, the run will fail to be created
await new Promise(resolve => setTimeout(resolve, SLEEP_DURATION * 1000));
if (DELETE_TAGS) {
for (let i = 0; i < preReleases.length - KEEP_LATEST; i++) {
const release = preReleases[i];
console.log(`Deleting tag: ${release.tag_name}`);
try {
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `tags/${release.tag_name}`
});
} catch (error) {
console.error(`Failed to delete tag: ${release.tag_name}`);
console.error(error);
}
}
}