如何传递从GitHub操作接收的环境变量 [英] How to pass environment variable received from GitHub actions

查看:0
本文介绍了如何传递从GitHub操作接收的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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变量...现在,我的文档文件如下所示: ;

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操作没有按预期工作?

这里the link to the test repo

推荐答案

我认为您试图读取错误的环境变量名。GitHub操作将INPUT_添加到输入变量的名称中。因此,请尝试以下操作:

print(os.getenv('INPUT_TEST_VAR'))

摘自文档:

当您在工作流文件中指定操作的输入或使用 默认输入值时,GitHub会为 名称为INPUT_的输入。环境变量 Created将输入名称转换为大写字母并替换空格 WITH_CHARACTS。

例如,如果工作流定义了数字十字猫和八字猫EyeColor 输入时,操作代码可以使用 INPUT_NUMOCTOCATS和INPUT_OCTOCATEYECOLOR环境变量。

https://help.github.com/en/articles/metadata-syntax-for-github-actions#inputs

这篇关于如何传递从GitHub操作接收的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆