GitHub workflows env All In One

发布时间 2023-09-07 19:53:32作者: xgqfrms

GitHub workflows env All In One

$GITHUB_ENV

docs

GITHUB_ENV environment file

# 把变量和值 `>>` 追加到 GITHUB_ENV 环境变量文件中
echo "{environment_variable_name}={value}" >> "$GITHUB_ENV"

steps:
  - name: Set the value
    id: step_one
    run: |
      echo "action_state=yellow" >> "$GITHUB_ENV"
  - name: Use the value
    id: step_two
    run: |
      printf '%s\n' "$action_state" # This will output 'yellow'

https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable

env vs github.env

# ✅
${{ env.VARIABLE_NAME }}

# ✅ namespcae ❓
${{github.env.VARIABLE_NAME}}}

https://docs.github.com/en/actions/learn-github-actions/contexts#env-context

demos

# This is a basic workflow to help you get started with Actions
name: Tesla_Crawler

# Controls when the action will run.
on:
  # Triggers the workflow on push events but only for the main branch
  push:
    branches: [ main ]
  schedule:
    - cron: '00 08 * * *'
    # https://crontab.guru/#00_08_*_*_*
    # Runs at 08:00 on everyday
    # 分、时、日、月、周
    # https://www.cnblogs.com/xgqfrms/p/15384401.html
    # - cron: '00 08,20 * * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  # hello-job:
  #   # The type of runner that the job will run on
  #   runs-on: ubuntu-latest
  #   # Steps represent a sequence of tasks that will be executed as part of the job
  #   steps:
  #     # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
  #     - uses: actions/checkout@v2
  #     # Runs a single command using the runners shell
  #     - name: Run a one-line script
  #       run: echo Hello, world!
  # crawler job
  tesla-job:
    permissions:
      # Give the default GITHUB_TOKEN write permission to commit and push the
      # added or changed files to the repository.
      contents: write
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout codes'
        uses: actions/checkout@v3
      # 自定义 env, 并且 >> 追加到 GITHUB_ENV file 中 ✅
      # https://www.cnblogs.com/xgqfrms/p/17685587.html
      # 自定义 env, $GITHUB_ENV => ${{env.REPORT_DATE}} ✅
      # 自定义 env, $GITHUB_ENV => ${{github.env.REPORT_DATE}} ✅
      - name: 'set Date'
        run: echo "REPORT_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T')" >> $GITHUB_ENV
      - name: 'print Date'
        run: echo ${{env.REPORT_DATE}}
        # vscode warning: Context access might be invalid: REPORT_DATE
        # https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
      - name: 'Get Date'
        run: echo ${{github.env.REPORT_DATE}}
        # vscode not warning
      - name: 'install package'
        run: npm i
      - name: 'begin'
        run: echo "crawling ...✅"
      - name: '自动爬取'
        run: node ./auto-update.js
      - name: 'end'
        run: echo "finshed ?"
      # Commit all changed files back to the repository
      - uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: update tesla data

image

# ...

    steps:
      - name: 'Checkout codes'
        uses: actions/checkout@v3
      # 自定义 env, 并且 >> 追加到 GITHUB_ENV file 中 ✅
      # https://www.cnblogs.com/xgqfrms/p/17685587.html
      # 自定义 env, $GITHUB_ENV => ${{env.REPORT_DATE}} ✅
      # 自定义 env, $GITHUB_ENV => ${{github.env.REPORT_DATE}} ✅
      - name: 'set Date'
        run: echo "REPORT_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T')" >> $GITHUB_ENV
      - name: 'print Date'
        run: echo ${{env.REPORT_DATE}}
        # vscode warning: Context access might be invalid: REPORT_DATE
        # https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
      - name: 'Get Date'
        run: echo ${{github.env.REPORT_DATE}}
        # vscode not warning
      # 自定义 multi envs
      # $GITHUB_ENV => ${{github.env.AUTHOR}} ✅ $GITHUB_ENV => ${{github.env.WEBSITE}} ✅
      - name: '自定义多个 env 变量'
        run: |
          echo "AUTHOR=xgqfrms" >> $GITHUB_ENV
          echo "WEBSITE=https://www.xgqfrms.xyz" >> $GITHUB_ENV
      - name: '打印多个 env 变量'
        run: |
          echo "AUTHOR=${{env.AUTHOR}} ✅"
          echo "WEBSITE=${{env.WEBSITE}} ?"
        # Unexpected symbol: '$GITHUB_ENV'. Located at position 1 within expression: $GITHUB_ENV
        # Available expression contexts: github, inputs, vars, needs, strategy, matrix, secrets, steps, job, runner, env
        # Available expression functions: hashFiles
# ...

image

(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

export & set & unset

  • export an environment variable
  • set an environment variable
  • delete an exported environment variable

$ export GITHUB_ENV_var=1

$ echo $GITHUB_ENV_var
1
$ set GITHUB_ENV_var=2

$ echo $GITHUB_ENV_var
1

$ set | grep "GITHUB_ENV_var"
'*'=( 'GITHUB_ENV_var=2' )
@=( 'GITHUB_ENV_var=2' )
GITHUB_ENV_var=1
argv=( 'GITHUB_ENV_var=2' )

$ unset GITHUB_ENV_var

$ set | grep "GITHUB_ENV_var"
'*'=( 'GITHUB_ENV_var=2' )
@=( 'GITHUB_ENV_var=2' )
argv=( 'GITHUB_ENV_var=2' )

$ echo $GITHUB_ENV_var

image

https://stackoverflow.com/questions/77058657/why-both-the-set-and-unset-commands-dont-work-as-expected-in-my-macos-terminal

https://stackoverflow.com/questions/6877727/how-do-i-delete-an-exported-environment-variable

refs

https://github.com/web-full-stack/cyclic-express-server/issues/6



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!