Search code examples
bashshellunixshgithub-actions

Save env variable value not definition


I'm working on a github actions workflow and have sent an environment variable:

env:
  rundate: $(date --date="2 days ago" '+%Y%m%d')

If I check that in the terminal on local, I see what I need:

echo $(date --date="2 days ago" '+%Y%m%d')
20220721

But, when trying to use in workflow, in this case to set the path of where to download an artifact to:

  - name: Download BQ data from previous job
    uses: actions/download-artifact@v2
    with:
      name: bq-data
      path: _${{ env.rundate }}data.tsv

Expected/desired a path of _20220721data.csv but in the runner console I see:

/_"$(date --date="2 days ago" '+%Y%m%d')"data.tsv

How can I set the env variable to be a string, the string in this case here and now being 20220721 as opposed to being the method of calculating the value?


Solution

  • You're trying to set the value of a variable to the result of executing a shell script. You can't do that in the env section of a github action, which can only have static values (or references to context values, like ${{ github.ref }}). In this case, you're simply setting the value of rundate to the literal string $(date --date="2 days ago" '+%Y%m%d').

    You can have a step emit your desired value as a labelled output using the set-output command, and then refer to those outputs in subsequent steps:

    jobs:
      example:
        runs-on: ubuntu-latest
        steps:
          - id: set_date
            run: |
              timestamp="$(date --date="2 days ago" '+%Y%m%d')"
              echo "::set-output name=timestamp::$timestamp"
    
          - name: Download BQ data from previous job
            uses: actions/download-artifact@v2
            with:
              name: bq-data
              path: _${{ steps.set_date.outputs.timestamp }}data.tsv