Using date as an ENV variable in GitHub action

陌路散爱 提交于 2021-01-25 07:21:34

问题


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.

Example: .github/workflows/rust.yaml

env:
        MY_DATE: '2020-10-07T12:00:00+01:00'
run: echo $MY_DATE

Result (GH action UI):

env:
    TMOU_GAME_END: 10/07/2020 11:00:00

10/07/2020 11:00:00

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:

  • 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 with The 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'

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.


回答1:


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 }}

This should do the trick.



来源:https://stackoverflow.com/questions/64645858/using-date-as-an-env-variable-in-github-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!