Publish version #41
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
name: Publish version | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- master | |
paths-ignore: | |
- '.github/**' | |
- 'README.md' | |
- 'package.json' | |
- 'yarn.lock' | |
concurrency: publish | |
jobs: | |
job: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout source code | |
uses: actions/checkout@v4 | |
- name: setup node | |
uses: actions/setup-node@v4 | |
with: | |
node-version-file: .nvmrc | |
- name: Fetch major version | |
id: version | |
run: echo "majorMinor=$(jq --raw-output '.version' package.json)" >> $GITHUB_OUTPUT | |
- name: Tag release | |
uses: actions/github-script@v7.0.1 | |
with: | |
script: | | |
// Parse major and minor version from package.json | |
const majorMinorVersion = '${{ steps.version.outputs.majorMinor }}'; | |
const majorVersion = 'v' + majorMinorVersion.split('.')[0]; | |
// Fetch latest patch version for the given major and minor version | |
const response = await github.rest.git.listMatchingRefs({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'tags/v' + majorMinorVersion, | |
per_page: 100 | |
}); | |
// If we ever reach more than 100 releases for a major version, we will have to add pagination. | |
const refs = response.data; | |
// Create a new patch version by either incrementing the last patch version or by starting with .0 | |
let newVersion = `v${majorMinorVersion}.`; | |
let updateMajorTag; | |
if (refs.length === 0) { | |
// Create initial version | |
newVersion += '0'; | |
updateMajorTag = false; | |
} else { | |
// Fetch last version and increment | |
const currentVersion = refs[refs.length - 1].ref; | |
const patchVersion = Number(currentVersion.split('.')[2]); | |
newVersion += (patchVersion + 1); | |
updateMajorTag = true; | |
} | |
// Create the patch tag | |
await github.rest.git.createRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'refs/tags/' + newVersion, | |
sha: context.sha | |
}); | |
if (updateMajorTag) { | |
// Update the major tag to point to the new commit | |
await github.rest.git.updateRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'tags/' + majorVersion, | |
sha: context.sha, | |
force: true | |
}); | |
} else { | |
// Create the major tag | |
await github.rest.git.createRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'refs/tags/' + majorVersion, | |
sha: context.sha | |
}); | |
} |