Skip to content

Commit d600ef4

Browse files
feat: add retag-release workflow to add a vX.Y.Z tag for our GitHub Action (#545)
* feat: add retag-release workflow to add a vX.Y.Z tag for our GitHub Action * Update .github/workflows/retag-release.yaml Co-authored-by: Kanad Gupta <kgupta@umn.edu> * refactor: move code out of YAML and into bin/ script * fix: lint fixes Co-authored-by: Kanad Gupta <kgupta@umn.edu>
1 parent fe24984 commit d600ef4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

.github/workflows/retag-release.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This script adds a "vX.Y.Z" tag to every new release.
2+
#
3+
# Our releases are tagged like "1.2.3" but we want people to be able to write
4+
# GitHub Action workflows to say "uses: readmeio/readme@v1.2.3" because that's
5+
# the usual GitHub convention.
6+
7+
name: retag-release
8+
9+
on:
10+
release:
11+
types: [created]
12+
13+
jobs:
14+
retag-release:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: actions/github-script@v6
19+
with:
20+
script: require('./bin/retag-release.js')(github, context);

bin/retag-release.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* eslint-disable no-console */
2+
module.exports = async (github, context) => {
3+
const { owner, repo } = context.repo;
4+
const oldTag = context.payload.release.tag_name;
5+
if (!oldTag.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
6+
console.log('Not retagging this release: This script will only retag releases that use');
7+
console.log(`semantic versioning, like "1.2.3", but this release's tag is "${oldTag}".`);
8+
return {};
9+
}
10+
const newTag = `v${oldTag}`;
11+
console.log(`Retagging release "${oldTag}" as "${newTag}".`);
12+
13+
const oldRef = await github.rest.git.getRef({
14+
owner,
15+
repo,
16+
ref: `tags/${oldTag}`,
17+
});
18+
if (oldRef.status < 200 || oldRef.status >= 400) {
19+
console.log(oldRef);
20+
throw new Error(`GitHub API call returned HTTP status code ${oldRef.status}`);
21+
}
22+
const sha = oldRef.data.object.sha;
23+
console.log(`Found tag "${oldTag}"; commit hash is ${sha}`);
24+
25+
console.log(`Creating tag "${newTag}" pointing to commit hash ${sha}...`);
26+
const newRef = await github.rest.git.createRef({
27+
owner,
28+
repo,
29+
ref: `refs/tags/${newTag}`,
30+
sha,
31+
});
32+
if (newRef.status < 200 || newRef.status >= 400) {
33+
console.log(newRef);
34+
throw new Error(`GitHub API call returned HTTP status code ${newRef.status}`);
35+
}
36+
console.log('Successfully retagged this release.');
37+
return {
38+
original_tag: oldTag,
39+
new_tag: newTag,
40+
sha,
41+
};
42+
};

0 commit comments

Comments
 (0)