Using Environment Variables in Shell Script in a GitHub Action

Viewed 1404

I have a project where I'm using GitHub Actions for the CI / CD. I have a shell script where I would like to inject environment variables from my Actions yml. Here is what I have so far:

  - name: docker-push
    env:
      USER: joesan
      SOME_GITHUB_REPO_NAME: github-repo-name
      GH_REPO: github.com/$USER/$SOME_GITHUB_REPO_NAME
    run: |
      echo "Running sbt assembly"
      echo $GITHUB_REF
      echo "Pushing tag into Docker Registry"
      sh ./scripts/tag_deployment.sh

In the tag_deployment.sh, I'm trying to use these variables:

....
....
git clone https://${GH_REPO}
cd "${SOME_GITHUB_REPO_NAME}"
....
....

But I see when the action is run that it does not print what was set!

Cloning into '$SOME_GITHUB_REPO_NAME'...
fatal: unable to access 'https://github.com/$USER/$SOME_GITHUB_REPO_NAME/': The requested URL returned error: 400

How can I inject these environment variables properly into my shell script?

1 Answers

It was not so intuitive to understand from the GitHub documentation. I managed to fix this by moving the environment variables globally like this:

env:
  USER: joesan
  DEPLOYMENT_REPO_NAME: some-repo-name

And then use it in my some other job definition like this:

  - name: docker-push
    env:
      GH_REPO: github.com/${{ env.USER }}/${{ env.DEPLOYMENT_REPO_NAME }}
Related