如何传递从 GitHub 操作接收到的环境变量
How to pass environment variable received from GitHub actions
在我的 action.yml 中,我定义了一个输入:
name: 'test action'
author: Param Thakkar
description: 'test'
inputs:
test_var:
description: 'A test variable'
required: true
runs:
using: 'docker'
image: 'Dockerfile'
在我的工作流程中,我通过了 test_var:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Test the GH action
uses: paramt/github-actions-playground@master
with:
test_var: "this is just a test"
所以应该有一个在工作流 运行 时创建的环境变量,对吗?但是当我 运行 这个简短的 python 脚本时:
import os
print(os.getenv('TEST_VAR'))
print("It works!")
exit(0)
它打印:
None
It works!
我想我必须通过我的 Dockerfile 传递 ENV 变量...现在我的 Dockerfile 看起来像这样:
FROM python:latest
# Add files to the image
ADD entrypoint.py /entrypoint.py
ADD requirements.txt /requirements.txt
# Save ENV var in a temp file
RUN $TEST_VAR > /temp_var
# Install dependencies and make script executable
RUN pip install -r requirements.txt
RUN chmod +x entrypoint.py
RUN echo "temp var: "
RUN cat /temp_var
# Run script with the ENV var
ENTRYPOINT export TEST_VAR="$TEST_VAR"; /entrypoint.py
但是变量没有回显,也没有传递给 pythons 脚本。我是不是遗漏了什么?当我尝试将我的 $TEMP_VAR
设置为一段随机字符串时,它 是 发送到 Python 脚本。这是我的错误还是 GitHub 操作没有按预期工作?
我认为您正在尝试读取错误的环境变量名称。 GitHub Actions 将 INPUT_
添加到输入变量的名称中。所以请尝试以下操作:
print(os.getenv('INPUT_TEST_VAR'))
来自文档:
When you specify an input to an action in a workflow file or use a
default input value, GitHub creates an environment variable for the
input with the name INPUT_. The environment variable
created converts input names to uppercase letters and replaces spaces
with _ characters.
For example, if a workflow defined the numOctocats and octocatEyeColor
inputs, the action code could read the values of the inputs using the
INPUT_NUMOCTOCATS and INPUT_OCTOCATEYECOLOR environment variables.
https://help.github.com/en/articles/metadata-syntax-for-github-actions#inputs
有点晚了,但对于下一个,您也可以使用 env
字段:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Test the GH action
uses: paramt/github-actions-playground@master
env:
test_var: "this is just a test"
将在您的 docker 创建过程中包含,并且在没有前缀 INPUT_
的情况下通过
通过在存储库的 Settings -> Secrets
中指定它们来保密环境变量,然后在工作流程中调用它们:
例如,考虑一个工作流程,其中 运行 是一个 R
脚本,后跟一个 Python
脚本。首先,在 .github/workflows/my_job.yml
中注意 MY_VAR
变量,它指向使用 ${{ secrets.MY_VAR}}
存储的秘密。其余的是标准代码(运行 在 cron 上,指定 Ubuntu OS 和 Docker 图像,定义工作流程步骤)。
on:
schedule:
- cron: '0 17 * * *'
jobs:
my_job:
name: my job
env:
MY_VAR: ${{ secrets.MY_VAR }}
runs-on: ubuntu-18.04
container:
image: docker.io/my_username/my_image:my_tag
steps:
- name: checkout_repo
uses: actions/checkout@v2
- name: run some code
run: bash ./src/run.sh
接下来,在组成您的工作流的脚本中,您可以像在本地一样访问上面工作流文件中指定的环境变量。
例如,在存储库中,我们假设 src/run.sh
调用 R
脚本,然后调用 Python
脚本。
在 R
中访问环境变量并存储为对象:
my_var <- Sys.getenv("MY_VAR")
.
.
.
在 Python
中访问环境变量并存储为对象:
import os
my_var = os.getenv("MY_VAR")
.
.
.
参见docs here。
就我而言,none 个答案有效。这是我修复它的方法。
---
name: Build and Push Docker Image to AWS ECR
on:
push:
branches: [ master ]
env:
FOO: '${{ secrets.FOO }}'
jobs:
build-and-push:
name: Build Project and Push to AWS ECR
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
...
- name: Build and Push to AWS ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
docker build --build-arg FOO=$FOO -t $ECR_REGISTRY/crew-charge-app:latest .
docker push $ECR_REGISTRY/crew-charge-app:latest
我首先必须使用 ${{secrets.FOO}} 从 github secrets 中获取 FOO 变量,然后使用 docker build 将其传递到 docker 文件 - -build-arg FOO=$FOO --build-arg BAR=$BAR -t .
然后在 docker 文件中,我必须将 ARG 和 ENV 都声明为始终可用。
FROM node:14
ARG FOO=${FOO}
ENV FOO=${FOO}
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN yarn install
COPY . /usr/src/app
RUN FOO=$FOO yarn build
EXPOSE 80
CMD ["yarn", "start" ]
重要的部分是 运行 $FOO=FOO 纱线构建,因为单独设置 ENV 不会将其传递到容器中。
在我的 action.yml 中,我定义了一个输入:
name: 'test action'
author: Param Thakkar
description: 'test'
inputs:
test_var:
description: 'A test variable'
required: true
runs:
using: 'docker'
image: 'Dockerfile'
在我的工作流程中,我通过了 test_var:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Test the GH action
uses: paramt/github-actions-playground@master
with:
test_var: "this is just a test"
所以应该有一个在工作流 运行 时创建的环境变量,对吗?但是当我 运行 这个简短的 python 脚本时:
import os
print(os.getenv('TEST_VAR'))
print("It works!")
exit(0)
它打印:
None
It works!
我想我必须通过我的 Dockerfile 传递 ENV 变量...现在我的 Dockerfile 看起来像这样:
FROM python:latest
# Add files to the image
ADD entrypoint.py /entrypoint.py
ADD requirements.txt /requirements.txt
# Save ENV var in a temp file
RUN $TEST_VAR > /temp_var
# Install dependencies and make script executable
RUN pip install -r requirements.txt
RUN chmod +x entrypoint.py
RUN echo "temp var: "
RUN cat /temp_var
# Run script with the ENV var
ENTRYPOINT export TEST_VAR="$TEST_VAR"; /entrypoint.py
但是变量没有回显,也没有传递给 pythons 脚本。我是不是遗漏了什么?当我尝试将我的 $TEMP_VAR
设置为一段随机字符串时,它 是 发送到 Python 脚本。这是我的错误还是 GitHub 操作没有按预期工作?
我认为您正在尝试读取错误的环境变量名称。 GitHub Actions 将 INPUT_
添加到输入变量的名称中。所以请尝试以下操作:
print(os.getenv('INPUT_TEST_VAR'))
来自文档:
When you specify an input to an action in a workflow file or use a default input value, GitHub creates an environment variable for the input with the name INPUT_. The environment variable created converts input names to uppercase letters and replaces spaces with _ characters.
For example, if a workflow defined the numOctocats and octocatEyeColor inputs, the action code could read the values of the inputs using the INPUT_NUMOCTOCATS and INPUT_OCTOCATEYECOLOR environment variables.
https://help.github.com/en/articles/metadata-syntax-for-github-actions#inputs
有点晚了,但对于下一个,您也可以使用 env
字段:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Test the GH action
uses: paramt/github-actions-playground@master
env:
test_var: "this is just a test"
将在您的 docker 创建过程中包含,并且在没有前缀 INPUT_
通过在存储库的 Settings -> Secrets
中指定它们来保密环境变量,然后在工作流程中调用它们:
例如,考虑一个工作流程,其中 运行 是一个 R
脚本,后跟一个 Python
脚本。首先,在 .github/workflows/my_job.yml
中注意 MY_VAR
变量,它指向使用 ${{ secrets.MY_VAR}}
存储的秘密。其余的是标准代码(运行 在 cron 上,指定 Ubuntu OS 和 Docker 图像,定义工作流程步骤)。
on:
schedule:
- cron: '0 17 * * *'
jobs:
my_job:
name: my job
env:
MY_VAR: ${{ secrets.MY_VAR }}
runs-on: ubuntu-18.04
container:
image: docker.io/my_username/my_image:my_tag
steps:
- name: checkout_repo
uses: actions/checkout@v2
- name: run some code
run: bash ./src/run.sh
接下来,在组成您的工作流的脚本中,您可以像在本地一样访问上面工作流文件中指定的环境变量。
例如,在存储库中,我们假设 src/run.sh
调用 R
脚本,然后调用 Python
脚本。
在 R
中访问环境变量并存储为对象:
my_var <- Sys.getenv("MY_VAR")
.
.
.
在 Python
中访问环境变量并存储为对象:
import os
my_var = os.getenv("MY_VAR")
.
.
.
参见docs here。
就我而言,none 个答案有效。这是我修复它的方法。
---
name: Build and Push Docker Image to AWS ECR
on:
push:
branches: [ master ]
env:
FOO: '${{ secrets.FOO }}'
jobs:
build-and-push:
name: Build Project and Push to AWS ECR
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
...
- name: Build and Push to AWS ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
docker build --build-arg FOO=$FOO -t $ECR_REGISTRY/crew-charge-app:latest .
docker push $ECR_REGISTRY/crew-charge-app:latest
我首先必须使用 ${{secrets.FOO}} 从 github secrets 中获取 FOO 变量,然后使用 docker build 将其传递到 docker 文件 - -build-arg FOO=$FOO --build-arg BAR=$BAR -t .
然后在 docker 文件中,我必须将 ARG 和 ENV 都声明为始终可用。
FROM node:14
ARG FOO=${FOO}
ENV FOO=${FOO}
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN yarn install
COPY . /usr/src/app
RUN FOO=$FOO yarn build
EXPOSE 80
CMD ["yarn", "start" ]
重要的部分是 运行 $FOO=FOO 纱线构建,因为单独设置 ENV 不会将其传递到容器中。