Semantic release version in job
Get version from semantic release
Get version from semantic release and use for instance, to deploy Docker image with version tag.
This solution uses GitHub workflow specific commands.
This method is pretty hacky but gets the job done. Nonetheless a semantic release built-in method would be more future proof to use.
An official Github Action with proper inputs and outputs would be even more appreciated.
Use the exec
plugin to store the semantic release ${nextRelease.version}
variable in a GitHub workflow step output:
Install @semantic-release/exec
plugin
yarn add -D @semantic-release/exec
or using npm
:
npm install --save-dev @semantic-release/exec
Configuration
In the .releaserc
file:
The set-output
command is deprecated and will be disabled on 31st May 2023.
Please upgrade to using environment files or job outputs instead.
[
"@semantic-release/exec",
{
"publishCmd": 'echo "nextVersion=${nextRelease.version}" >> $GITHUB_OUTPUT',
},
]
Declare a new joboutput in the workflow yaml file:
release:
[...]
- name: 🔖 Release application
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
run: npx semantic-release
id: version # save the version
outputs:
version: ${{ steps.version.outputs.nextVersion }}
Use the step output in another job:
- name: 🐳 Build and push
uses: docker/build-push-action@v3
with:
context: . # https://github.com/marketplace/actions/build-and-push-docker-images#git-context
push: true
tags: name/docs:latest,name/docs:${{ needs.release.outputs.version }}
Take care about the context when running docker/build-push-action@v3
:
Be careful because any file mutation in the steps that precede the build step will be ignored, including processing of the
.dockerignore
file since the context is based on the Git reference.
However, you can use the Path context using the context input alongside theactions/checkout
action to remove this restriction:
context: .