Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move tags after release #446

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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!");