3

I have a github repository like the following

johndoe/hello-world

I am trying to set the following environment variables in github actions

env:
  DOCKER_HUB_USERID: ${{ github.actor }}
  REPOSITORY_NAME: ${GITHUB_REPOSITORY#*\/}
  IMAGE_NAME_CLIENT: "$REPOSITORY_NAME-client"
  IMAGE_NAME_SERVER: "$REPOSITORY_NAME-server"

My expected results for these variables are:

johndoe
hello-world
hello-world-client
hello-world-server

But i am getting

johndoe
${REPOSITORY_NAME#*\/}
$REPOSITORY_NAME-client
$REPOSITORY_NAME-server

Looks like the expressions are not being evaluated while declaring the env vars.

How can I achieve the expected behavior?

Rakib
  • 9,946
  • 11
  • 59
  • 93
  • For the second one, why not `${{github.repository}}`, similar to the first one that works? It looks like from [here](https://github.community/t5/GitHub-Actions/Repository-name-in-environment-variable/m-p/38030/highlight/true#M3198) that would work. – Wayne Feb 27 '20 at 21:36
  • `${{github.repository}}` includes the username... i want to get it without the username – Rakib Feb 28 '20 at 12:01

3 Answers3

6

Shell parameter expansion is not possible outside of a run step.

env:
  REPOSITORY_NAME: ${GITHUB_REPOSITORY#*\/}

Create an extra step to compute the value into a new variable.

      - name: Set env
        run: echo ::set-env name=REPOSITORY_NAME::${GITHUB_REPOSITORY#*\/}
      - name: Test
        run: echo $REPOSITORY_NAME

Or create a step output.

      - name: Set outputs
        id: vars
        run: echo ::set-output name=repo_name::${GITHUB_REPOSITORY#*\/}
      - name: Test set output
        run: echo ${{ steps.vars.outputs.repo_name }}

Once the computed environment variable REPOSITORY_NAME, or step output steps.vars.outputs.repo_name, exists, they can be used to set other variables like this.

env:
  IMAGE_NAME_CLIENT: ${{ env.REPOSITORY_NAME }}-server
  IMAGE_NAME_SERVER: ${{ steps.vars.outputs.repo_name }}-server
peterevans
  • 13,485
  • 3
  • 40
  • 55
1

Github has changed the way you set environment variables for security reasons, now you have to use thi way.

steps:
  - name: Set the environment variable
    run: echo REPOSITORY_NAME=${GITHUB_REPOSITORY#*\/} >> $GITHUB_ENV

  - name: Use the value
    run: echo $REPOSITORY_NAME # This will output repository name

https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable

0

New as of this month, still in a 'run'.

https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/

echo "action_state=yellow" >> $GITHUB_ENV

I also found that things like uses:with:ref will not take ${action_state} expansion, but they will take ${{ env.action_state }} expansion after being stuffed.

leemr
  • 31
  • 6