Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 956
Rakib Avatar asked Feb 27 '20 12:02

Rakib


People also ask

How do I pass an environment variable in GitHub actions?

Passing values between steps and jobs in a workflow If you generate a value in one step of a job, you can use the value in subsequent steps of the same job by assigning the value to an existing or new environment variable and then writing this to the GITHUB_ENV environment file.

Are env vars always strings?

How environment variables work. According to POSIX standards environment variables are all null-terminated strings in the format name=value where name cannot contain the = character. Depending on implementation name may not start with a digit and only consist of uppercase letters, digits, and the underscore _ .

How do I declare variables in GitHub actions?

To add variables to the repository, you can define the variable in Secrets tab and use them in the yaml file. Note: You can access the variable in the build. xml as highlighted below. And, to make use of your variables as unencrypted, you can directly use them in the yml as highlighted below.

How do I echo an object in action in GitHub?

You should use run: echo "$GITHUB. REPOSITORY" and run: echo "$GITHUB. REPOSITORY_OWNER" to see them directly on your workflow. Tip: You can identify most of the variables that can be shown with echo through the Github Context using run: echo "$GITHUB_CONTEXT" in your workflow.


Video Answer


3 Answers

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
like image 163
peterevans Avatar answered Oct 15 '22 15:10

peterevans


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

like image 43
Fabiel León Avatar answered Oct 15 '22 13:10

Fabiel León


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.

like image 37
leemr Avatar answered Oct 15 '22 15:10

leemr