如何在 GitHub 操作中使用 bash 表达式设置环境变量?
How do I set an env var with a bash expression in GitHub Actions?
在 GitHub 操作中,我想计算一个 bash 表达式,然后将其分配给一个环境变量:
- name: Tag image
env:
GITHUB_SHA_SHORT: ${{ $(echo $GITHUB_SHA | cut -c 1-6) }}
..do other things...
然而,这种幼稚的尝试失败了。 According to the docs 这似乎不受支持;稍微干净的解决方法就可以了。
这个问题的原始答案使用了 Actions runner 函数 set-env
。由于 security vulnerability set-env
已弃用,不应再使用。
这是设置环境变量的新方法。
name: my workflow
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set env
run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
- name: Test
run: echo $GITHUB_SHA_SHORT
Setting an environment variable
echo "{name}={value}" >> $GITHUB_ENV
Creates or updates an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access. Environment variables are case-sensitive and you can include punctuation.
使用输出到 $GITHUB_ENV 方法的示例:
echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
这是在工作流中引用环境变量的另一种方法。
- name: Test
run: echo ${{ env.GITHUB_SHA_SHORT }}
文档 https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#about-environment-variables 描述了 2 种定义环境变量的方法。
To set custom environment variables, you need to specify the variables
in the workflow file. You can define environment variables for a step,
job, or entire workflow using the jobs.<job_id>.steps[*].env,
jobs.<job_id>.env, and env keywords.
steps:
- name: Hello world
run: echo Hello world $FIRST_NAME $middle_name $Last_Name!
env:
FIRST_NAME: Mona
middle_name: The
Last_Name: Octocat
You can also use the GITHUB_ENV environment file to set an environment
variable that the following steps in a workflow can use. The
environment file can be used directly by an action or as a shell
command in a workflow file using the run keyword.
在 GitHub 操作中,我想计算一个 bash 表达式,然后将其分配给一个环境变量:
- name: Tag image
env:
GITHUB_SHA_SHORT: ${{ $(echo $GITHUB_SHA | cut -c 1-6) }}
..do other things...
然而,这种幼稚的尝试失败了。 According to the docs 这似乎不受支持;稍微干净的解决方法就可以了。
这个问题的原始答案使用了 Actions runner 函数 set-env
。由于 security vulnerability set-env
已弃用,不应再使用。
这是设置环境变量的新方法。
name: my workflow
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set env
run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
- name: Test
run: echo $GITHUB_SHA_SHORT
Setting an environment variable
echo "{name}={value}" >> $GITHUB_ENV
Creates or updates an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access. Environment variables are case-sensitive and you can include punctuation.
使用输出到 $GITHUB_ENV 方法的示例:
echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
这是在工作流中引用环境变量的另一种方法。
- name: Test
run: echo ${{ env.GITHUB_SHA_SHORT }}
文档 https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#about-environment-variables 描述了 2 种定义环境变量的方法。
To set custom environment variables, you need to specify the variables in the workflow file. You can define environment variables for a step, job, or entire workflow using the jobs.<job_id>.steps[*].env, jobs.<job_id>.env, and env keywords.
steps:
- name: Hello world
run: echo Hello world $FIRST_NAME $middle_name $Last_Name!
env:
FIRST_NAME: Mona
middle_name: The
Last_Name: Octocat
You can also use the GITHUB_ENV environment file to set an environment variable that the following steps in a workflow can use. The environment file can be used directly by an action or as a shell command in a workflow file using the run keyword.