How to perform string manipulation while declaring env vars in GitHub Actions
Asked Answered
S

4

22

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?

Seminole answered 27/2, 2020 at 12:4 Comment(2)
For the second one, why not ${{github.repository}}, similar to the first one that works? It looks like from here that would work.Coston
${{github.repository}} includes the username... i want to get it without the usernameSeminole
T
27

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, appending it to the file at $GITHUB_ENV.

      - name: Set env
        run: echo "REPOSITORY_NAME=${GITHUB_REPOSITORY#*\/}" >> $GITHUB_ENV
      - 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
Tracee answered 28/2, 2020 at 5:44 Comment(1)
set-env is deprecated since October 2020. The suggested replacement uses the $GITHUB_ENV file.Honeyhoneybee
M
8

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

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

then use it like this

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

Example of use on env

  - name: Install dependencies And Build Yarn and npm
    uses: fabiel-leon/npm-build@master
    env:
      REPO: ${{ env.REPOSITORY_NAME }}
  - name: Build and push Docker images
    uses: docker/build-push-action@v1
    with:
      tags: ${{ env.REPOSITORY_NAME }}

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

Massenet answered 21/2, 2021 at 22:11 Comment(1)
Exactly what I was looking for either use it as ${{ env.REPOSITORY_NAME }} or in a string "This is my string ${{ env.REPOSITORY_NAME }}"Intraatomic
M
4

New as of this month, still in a 'run' - 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.

Morganite answered 19/10, 2020 at 2:36 Comment(0)
W
4

Like this

IMAGE_NAME_SERVER: "${{ REPOSITORY_NAME }}-server"
Works answered 4/8, 2021 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.