-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add retag-release workflow to add a vX.Y.Z tag for our GitHub A…
…ction (#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 <[email protected]> * refactor: move code out of YAML and into bin/ script * fix: lint fixes Co-authored-by: Kanad Gupta <[email protected]>
- Loading branch information
1 parent
fe24984
commit d600ef4
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This script adds a "vX.Y.Z" tag to every new release. | ||
# | ||
# Our releases are tagged like "1.2.3" but we want people to be able to write | ||
# GitHub Action workflows to say "uses: readmeio/[email protected]" because that's | ||
# the usual GitHub convention. | ||
|
||
name: retag-release | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
jobs: | ||
retag-release: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/github-script@v6 | ||
with: | ||
script: require('./bin/retag-release.js')(github, context); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable no-console */ | ||
module.exports = async (github, context) => { | ||
const { owner, repo } = context.repo; | ||
const oldTag = context.payload.release.tag_name; | ||
if (!oldTag.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) { | ||
console.log('Not retagging this release: This script will only retag releases that use'); | ||
console.log(`semantic versioning, like "1.2.3", but this release's tag is "${oldTag}".`); | ||
return {}; | ||
} | ||
const newTag = `v${oldTag}`; | ||
console.log(`Retagging release "${oldTag}" as "${newTag}".`); | ||
|
||
const oldRef = await github.rest.git.getRef({ | ||
owner, | ||
repo, | ||
ref: `tags/${oldTag}`, | ||
}); | ||
if (oldRef.status < 200 || oldRef.status >= 400) { | ||
console.log(oldRef); | ||
throw new Error(`GitHub API call returned HTTP status code ${oldRef.status}`); | ||
} | ||
const sha = oldRef.data.object.sha; | ||
console.log(`Found tag "${oldTag}"; commit hash is ${sha}`); | ||
|
||
console.log(`Creating tag "${newTag}" pointing to commit hash ${sha}...`); | ||
const newRef = await github.rest.git.createRef({ | ||
owner, | ||
repo, | ||
ref: `refs/tags/${newTag}`, | ||
sha, | ||
}); | ||
if (newRef.status < 200 || newRef.status >= 400) { | ||
console.log(newRef); | ||
throw new Error(`GitHub API call returned HTTP status code ${newRef.status}`); | ||
} | ||
console.log('Successfully retagged this release.'); | ||
return { | ||
original_tag: oldTag, | ||
new_tag: newTag, | ||
sha, | ||
}; | ||
}; |