Create tag #12
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: Create tag | |
on: | |
workflow_dispatch: | |
permissions: | |
contents: write | |
jobs: | |
tag: | |
name: Create Tag | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- run: | | |
VERSION=`node --eval 'console.log(require("./package.json").version)'` | |
echo "VERSION=$VERSION" >> "$GITHUB_OUTPUT" | |
id: version | |
name: Get version number | |
- uses: actions/github-script@v6 | |
name: Create tag | |
with: | |
script: | | |
const tag = await github.rest.git.getRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'tags/${{ steps.version.outputs.VERSION }}' | |
}); | |
if (tag != null && tag.status == 200) { | |
console.error('Tag ${{ steps.version.outputs.VERSION }} already exists.'); | |
return; | |
} | |
const main = await github.rest.git.getRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'heads/main' | |
}); | |
const createdTag = await github.rest.git.createTag({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
tag: '${{ steps.version.outputs.VERSION }}', | |
message: 'Bump version to ${{ steps.version.outputs.VERSION }}', | |
object: main.data.object.sha, | |
type: 'commit' | |
}); | |
if (createdTag.status !== 201) { | |
console.error('Could not create tag ${{ steps.version.outputs.VERSION }}.'); | |
} | |
const createdRef = await github.rest.git.createRef({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
ref: 'refs/tags/${{ steps.version.outputs.VERSION }}', | |
sha: createdTag.data.sha | |
}); | |
if (createdRef.status === 201) { | |
console.log('Tag ${{ steps.version.outputs.VERSION }} was created successfully'); | |
} else { | |
console.error('Could not create ref ${{ steps.version.outputs.VERSION }}.'); | |
} |