gpt4 book ai didi

docker - 如何传递从GitHub操作收到的环境变量

转载 作者:行者123 更新时间:2023-12-01 15:01:48 44 4
gpt4 key购买 nike

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 Action 没有按预期运行?

这是 the link to the test repo

最佳答案

我认为您正在尝试读取错误的环境变量名称。 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

关于docker - 如何传递从GitHub操作收到的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58242357/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com