在GitHub动作中使用date作为ENV变量 [英] Using date as an ENV variable in GitHub action
问题描述
这是一个非常有趣但令人沮丧的问题.我正在使用一个ENV变量,它指定日期.我提供了符合ISO 8601的版本,在应用程序中,我对其进行检索和解析.当我在GH action工作流程中指定它时,它将被解析为日期(而不是字符串)并进行格式化.因此,我的应用程序解析失败.
This is very funny but very frustrating problem. I am using an ENV variable, which specifies date. I provide an ISO 8601 compliant version and in application, I retrieve it and parse. When I specify it in GH action workflow, it is get parsed as a date (rather than a string) and formatted. Therefore, my application parsing fails.
示例:.github/workflows/rust.yaml
Example: .github/workflows/rust.yaml
env:
MY_DATE: '2020-10-07T12:00:00+01:00'
run: echo $MY_DATE
结果(GH操作界面):
Result (GH action UI):
env:
TMOU_GAME_END: 10/07/2020 11:00:00
10/07/2020 11:00:00
它特定于GitHub动作及其yaml解析,可在Heroku,各种本地设置等上正常运行.
It is specific to GitHub action and their yaml parsing, it works OK on Heroku, on various local setups, etc.
我尝试过但无法使用的东西:
Things I tried and they don't work:
- 不使用引号,单引号('),双引号(')
- 将另一个ENV变量LC_TIME设置为en_DK.UTF-8
- 使用!! str的简写形式(请参见 https://yaml.org/spec/1.2/spec.html ,示例2.23.各种显式标签);此操作失败并显示
工作流程无效..github/workflows/rust.yml:意外标签'tag:yaml.org,2002:str'
或使用工作流程无效..github/workflows/rust.yml:标量样式'DoubleQuoted |第29行和第24列上的单引号"对标签'tag:yaml.org,2002:str'
无效
- using no quotes, single quotes ('), double quotes (")
- setting another ENV var, LC_TIME to en_DK.UTF-8
- using !!str shorthand (see https://yaml.org/spec/1.2/spec.html, section Example 2.23. Various Explicit Tags); this one fails either with
The workflow is not valid. .github/workflows/rust.yml: Unexpected tag 'tag:yaml.org,2002:str'
or withThe workflow is not valid. .github/workflows/rust.yml: The scalar style 'DoubleQuoted | SingleQuoted' on line 29 and column 24 is not valid with the tag 'tag:yaml.org,2002:str'
有什么帮助吗?我可以打开任何秘密参数吗?有任何转义序列吗?我只是想用GH Actions yaml解析器将值视为字符串.
Is there any help? Any secret parameter I can turn on? Any escaping sequence? I just wanna GH Actions yaml parser to treat the value as a string.
推荐答案
令人惊讶的是,似乎GitHub Actions工作流YAML解析器没有完全实现该标准,使用显式键入(如 !! str
)确实可以实现不行.但是,您可以通过以下方式解决此问题:将环境变量设置为所需的值,而不是在YAML文件本身中,而是使用
Surprisingly, it seems GitHub Actions workflow YAML parser does not fully implement the standard and using explicit typing (like !!str
) does not work. You can, however, workaround it by setting the environment variable to the desired value not in the YAML file itself but dynamically during a workflow execution using a dedicated workflow command:
steps:
- name: Dynamically set MY_DATE environment variable
run: echo "MY_DATE=2020-10-07T12:00:00+01:00" >> $GITHUB_ENV
- name: Test MY_DATE variable
run: echo ${{ env.MY_DATE }}
这应该可以解决问题.
这篇关于在GitHub动作中使用date作为ENV变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!