在 GitHub Actions 中包含变量的选项

All*_* Xu 3 github-actions

我知道我可以在 GitHub Actions 中使用 Runner 的 Linux 环境变量。

我是否还有其他选项可以拥有变量并在工作流程步骤中使用它们?

Krz*_*tof 7

这就是变量在 GitHub Actions 中的设计方式。我的意思是声明的变量映射到环境变量,如下所示:

name: Show env

on:
  push:
    branches:
      - '*'
env:
  somevar: 'lastvar'
jobs:
  show:
    runs-on: ubuntu-latest
    steps:
      - name: Is variable exported?
        run: |
          echo "${{ env.somevar }}"
Run Code Online (Sandbox Code Playgroud)

但是,您不能在任何地方使用它们 - 请检查此主题

env:
  pluginId: 'plugin-fn-xml-node'

on:
  push:
    paths:
      - ${{env.pluginId}}/**
      - .github/workflows/**
  pull_request:
    paths:
      - ${{env.pluginId}}/**
      - '.github/workflows/**'

jobs:
  build:
    env:
      working-directory: ${{env.pluginId}}
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [8.x, 10.x, 12.x]

    steps:
Run Code Online (Sandbox Code Playgroud)

这将不起作用,因为您无法在作业级别使用工作流变量。

因此,如果您在工作流程级别定义变量,您应该能够跨步骤使用它。

我还添加了根据文档动态设置变量

env:
  somevar: 'lastvar'
jobs:
  show:
    runs-on: ubuntu-latest
    steps:
      - name: Is variable exported?
        run: |
          echo "${{ env.somevar }}"
          echo "action_state=yellow" >> $GITHUB_ENV
      - name: PowerShell script
        # You may pin to the exact commit or the version.
        # uses: Amadevus/pwsh-script@25a636480c7bc678a60bbf4e3e5ac03aca6cf2cd
        uses: Amadevus/pwsh-script@v2.0.0
        continue-on-error: true
        with: 
          # PowerShell script to execute in Actions-hydrated context
          script: | 
            Write-Host $env:somevar
            Write-Host $env:action_state
      - name: Read exported variable
        run: |
          echo "$action_state"
          echo "${{ env.action_state }}"
Run Code Online (Sandbox Code Playgroud)