Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions, how to share a calculated value between job steps?

Is there a DRY way to calculate and share a value in multiple job steps with Github Actions?

In the below workflow yml file, echo ${GITHUB_REF} | cut -d'/' -f3`-${GITHUB_SHA} is repeated in multiple steps.

name: Test, Build and Deploy on:   push:     branches:       - master jobs:   build_and_push:     name: Build and Push     runs-on: ubuntu-latest     steps:       - name: Docker Build         uses: "actions/docker/cli@master"         with:           args: build . --file Dockerfile -t cflynnus/blog:`echo ${GITHUB_REF} | cut -d'/' -f3`-${GITHUB_SHA}       - name: Docker Tag Latest         uses: "actions/docker/cli@master"         with:           args: tag cflynnus/blog:`echo ${GITHUB_REF} | cut -d'/' -f3`-${GITHUB_SHA} cflynnus/blog:latest 
like image 400
Casey Flynn Avatar asked Sep 06 '19 09:09

Casey Flynn


People also ask

How do I share variables between jobs in actions GitHub?

We don't support to share variables between jobs. As you metioned artifacts, we would suggest you use it . You can write the version into a file , upload it as artifact in one job and then download the artifacts in the other job , read the file content.

How do I run jobs sequentially in GitHub Actions?

To run jobs sequentially, you can define dependencies on other jobs using the jobs. <job_id>. needs keyword. Each job runs in a runner environment specified by runs-on .

Do GitHub Actions jobs run in parallel?

You can configure a GitHub Actions workflow to be triggered when an event occurs in your repository, such as a pull request being opened or an issue being created. Your workflow contains one or more jobs which can run in sequential order or in parallel.

How do I set an environment variable in GitHub Actions?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.


1 Answers

set-output can be used to define outputs for steps. The outputs can then be used in later steps and evaluated in with and env input sections.

The following is what that would look like for your example.

name: Test, Build and Deploy on:   push:     branches:       - master jobs:   build_and_push:     name: Build and Push     runs-on: ubuntu-latest     steps:       - name: Set tag var         id: vars         run: echo ::set-output name=docker_tag::$(echo ${GITHUB_REF} | cut -d'/' -f3)-${GITHUB_SHA}       - name: Docker Build         uses: "actions/docker/cli@master"         with:           args: build . --file Dockerfile -t cflynnus/blog:${{ steps.vars.outputs.docker_tag }}       - name: Docker Tag Latest         uses: "actions/docker/cli@master"         with:           args: tag cflynnus/blog:${{ steps.vars.outputs.docker_tag }} cflynnus/blog:latest 

Here is another example showing how to dynamically set multiple variables to be used by an action.

      - name: Set output variables         id: vars         run: |           echo ::set-output name=pr_title::"[Test] Add report file $(date +%d-%m-%Y)"           echo ::set-output name=pr_body::"This PR was auto-generated on $(date +%d-%m-%Y) \             by [create-pull-request](https://github.com/peter-evans/create-pull-request)."       - name: Create Pull Request         uses: peter-evans/create-pull-request@v2         with:           title: ${{ steps.vars.outputs.pr_title }}           body: ${{ steps.vars.outputs.pr_body }} 

Alternatively you can create environment variables.

      - name: Set environment variables         run: |           echo "PR_TITLE=[Test] Add report file $(date +%d-%m-%Y)" >> $GITHUB_ENV           echo "PR_BODY=This PR was auto-generated on $(date +%d-%m-%Y) by [create-pull-request](https://github.com/peter-evans/create-pull-request)." >> $GITHUB_ENV       - name: Create Pull Request         uses: peter-evans/create-pull-request@v2         with:           title: ${{ env.PR_TITLE }}           body: ${{ env.PR_BODY }} 

Update: The docker actions in the first example are deprecated. Please see this answer for the latest way to work with docker in GitHub Actions.

Note: For sharing values between different jobs, see this question.

like image 162
peterevans Avatar answered Sep 23 '22 00:09

peterevans