Skip to main content

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.

caution

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:

caution

The set-output command is deprecated and will be disabled on 31st May 2023.
Please upgrade to using environment files or job outputs instead.

.releaserc
[
"@semantic-release/exec",
{
"publishCmd": 'echo "nextVersion=${nextRelease.version}" >> $GITHUB_OUTPUT',
},
]

Declare a new joboutput in the workflow yaml file:

workflow.yaml
  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 }}
caution

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 the actions/checkout action to remove this restriction:

context: .

Documentation about Path context.

note

Full running example with other variable names

Workflow.
Semantic Release configuration file.
Example run.