Skip to content

Commit

Permalink
chore: move tags after release
Browse files Browse the repository at this point in the history
  • Loading branch information
kristof-mattei committed Nov 18, 2023
1 parent 59ee488 commit 68fbfff
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions .github/workflows/semantic-tags-after-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
name: Update semantic tags on repo after release

concurrency:
group: "${{ github.workflow }}"
cancel-in-progress: false # last one must win in case of multiple releases

on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"

permissions:
contents: write
packages: write

jobs:
move-git-tags:
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
show-progress: false

- name: Setup Node.js
uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0
with:
node-version-file: ".nvmrc"
cache: "npm"
cache-dependency-path: "**/package-lock.json"

- name: Install dependencies
shell: bash
run: |
npm ci --ignore-scripts
- name: Split the incoming tag into major, minor and patch
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
# because in the JS github variable ref_name isn't there
TAGNAME: ${{ github.ref_name }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const semver = require("semver");
// parse our version
let parsed = semver.parse(process.env.TAGNAME);
core.exportVariable("MAJOR", parsed.major);
core.exportVariable("MINOR", parsed.minor);
core.exportVariable("PATCH", parsed.patch);
- name: Move v${{ env.MAJOR }} and v${{ env.MAJOR }}.${{ env.MINOR }} tags
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { serializeError } = core.isDebug() ? require("serialize-error") : { serializeError: () => {} };
const tagsToMove = [
// already covered by semantic-release, it is what triggers this WF
// `v${process.env.MAJOR}.${process.env.MINOR}.${process.env.PATCH}`,
`v${process.env.MAJOR}.${process.env.MINOR}`,
// only do major if we're off v0, as v0 is the semver exception
...(0 !== process.env.MAJOR ? [`v${process.env.MAJOR}`] : [])
];
core.info(`Trying to move the following tags: ${tagsToMove.join(", ")} by deleting and re-creating.`);
async function move(tag) {
const ref = {
owner: context.repo.owner,
repo: context.repo.repo,
ref: `tags/${tag}`
};
try {
await github.rest.git.deleteRef({
...ref,
});
core.info(`Deleted ${tag}.`);
} catch (e) {
core.info(`The tag ${tag} doesn't exist yet, that's OK, it happens on new versions.`);
core.debug(serializeError(e));
}
try {
await github.rest.git.createRef({
...ref,
// to delete a tag with deleteRef the format of the ref is tags/{tag}
// to create a tag with createRef the format of the ref is /refs/tags/{tag}
// # confused
ref: `refs/${ref.ref}`,
sha: context.sha
});
core.info(`Created ${tag} on ${context.sha}.`);
} catch (e) {
core.setFailed(`Failed to create tag ${tag}.`);
core.debug(serializeError(e));
}
}
await Promise.all(tagsToMove.map(move));
core.info("All done!");

0 comments on commit 68fbfff

Please sign in to comment.