Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

How do you set an environment variable for a github action?

I've tried a bunch of different versions but the environment variable is never set.

These don't work. In all cases, the variable IMAGE does not exist.

echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}" >> $GITHUB_ENV
echo ${IMAGE}

env:
    DEPLOYMENT_NAME: my-deployment
echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}" >> $GITHUB_ENV
echo $IMAGE 

env:
    DEPLOYMENT_NAME: my-deployment
echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}" >> $GITHUB_ENV
echo ${{ env.IMAGE }}

env:
    DEPLOYMENT_NAME: my-deployment

Edit

Some more experimentation. I tried setting the environment variables like this.

env:
  IMAGE1: ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
  IMAGE2: "ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}"

This sets the variables - although I'd also like to be able to set them inside a job as described above. However, printing them in different ways always results in printing the variable names ${GITHUB_REPOSITORY} and ${GITHUB_SHA} rather than their content.

env:
  DEPLOYMENT_NAME: my-deployment
  IMAGE1: ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
  IMAGE2: ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
...
echo $IMAGE1
echo $IMAGE2
echo ${IMAGE1}
echo ${IMAGE2}
echo "$IMAGE1"
echo "$IMAGE2"
echo "${IMAGE1}"
echo "${IMAGE2}"

ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}
ghcr.io/${GITHUB_REPOSITORY}:${GITHUB_SHA}

So although this is some progress I'm still wondering how the variables are set inside a job/run and how the content of the variables can be printed and not their name.

question from:https://stackoverflow.com/questions/66045731/how-do-you-set-an-environment-variable-for-a-github-action

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The syntax for setting environment variables through job.<job_id>.env and job.<job_id>.steps[*].env is slightly different than Bash environment variable syntax. You should be able to access context objects in env: declarations though. Putting this all together to set environment variables for an entire job:

jobs:
  job1: # or whatever you want to name your job
    env:
      DEPLOYMENT_NAME: my-deployment
      IMAGE1: ghcr.io/${{ github.repository }}:${{ github.sha }}
      IMAGE2: ghcr.io/${{ github.repository }}:${{ github.sha }}

See also https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idenv .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...