49

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
Electric Sheep
  • 2,999
  • 1
  • 31
  • 37
Casey Flynn
  • 13,116
  • 22
  • 101
  • 190
  • Relevant: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable and: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/ – 0 _ Jun 18 '21 at 23:59

1 Answers1

77

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.

peterevans
  • 24,033
  • 6
  • 67
  • 72
  • 11
    Just to be clear, these solutions do not work between steps of _different jobs_ – grokpot Mar 31 '20 at 08:53
  • Are this solutions deprecated? https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/ – Sandburg Oct 13 '21 at 16:09
  • @Sandburg No they are not deprecated. These solutions show the current way to create step outputs and environment variables. – peterevans Oct 14 '21 at 00:01
  • @grokpot outputs across different jobs also seem to be supported, but you need to forward the step output to the job output: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idoutputs – MakisH Jan 28 '22 at 08:34