【问题标题】:github workflow using environment variable使用环境变量的 github 工作流
【发布时间】:2022-10-23 22:54:13
【问题描述】:

我在 Windows 服务器上有一个自托管的 Github Action 运行器,我的目标是从 Jenkins 迁移到 GitHub 的工作流程。由于某种原因,我很难理解如何使用环境变量。

我的自托管服务器上有一个文件夹,我想在其中放置一些通用构建脚本,这些脚本是在 Powershell 中创建的。

所以我的main.yml 看起来像这样

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

name: Build integration

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "develop" branch
  push:
    branches: ["*"]
  pull_request:
    branches: ["*"]
    

  # 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"
  continuous-integration:
    # The type of runner that the job will run on
    runs-on: [self-hosted, .net]

    # 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      
      - name: Checkout code
        uses: actions/checkout@v3     

      # get environment
      - name: Get system environments
        uses: FranzDiebold/github-env-vars-action@v2 

      # show environment
      - name: Show all enronment variables
        run: SET
        shell: cmd
        
      # read meta file
      - name: read project meta file
        run: |
          .\$BUILD_SCRIPTS_PATH}\Powershell\meta.ps1 -MetaFilePath ProjectMeta.json
        shell: powershell       

我已经尝试了各种变体来获取环境变量BUILD_SCRIPTS_PATH

.\${env.BUILD_SCRIPTS_PATH}\Powershell\meta.ps1 -MetaFilePath ProjectMeta.json
.\${{BUILD_SCRIPTS_PATH}}\Powershell\meta.ps1 -MetaFilePath ProjectMeta.json
.\$env.BUILD_SCRIPTS_PATH\Powershell\meta.ps1 -MetaFilePath ProjectMeta.json
.\$BUILD_SCRIPTS_PATH\Powershell\meta.ps1 -MetaFilePath ProjectMeta.json

我不断收到此错误

+ ${BUILD_SCRIPTS_PATH}\Powershell\meta.ps1 -MetaFilePath ProjectMe ...
+                          ~~~~~~~~~~~~~~~~~~~~
Unexpected token '\Powershell\meta.ps1' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

SET 命令输出的一小部分

Run SET
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\Administrator\AppData\Roaming
BUILD_SCRIPTS_PATH=E:\github\BuildScripts
ChocolateyInstall=C:\ProgramData\chocolatey

我已经查看了来自 GitHub GitHub Documentation 的文档,但我真的不明白我做错了什么。我真的很感激有人可以向我解释我做错了什么。

【问题讨论】:

    标签: github workflow github-actions-self-hosted-runners


    【解决方案1】:

    您可以在 GitHub 操作here 中阅读有关使用环境变量的更多信息。

    从您发布的内容来看,您似乎并未将 BUILD_SCRIPTS_PATH 提供给工作流的环境。

    您可以像这样添加它:

    # read meta file
        - name: read project meta file
          run: |
            .$BUILD_SCRIPTS_PATHPowershellmeta.ps1 -MetaFilePath ProjectMeta.json
          shell: powershell   
          env:
             BUILD_SCRIPTS_PATH: ${{ secrets:build_scripts_path}}
    

    然后确保将 build_scripts_path 添加为GitHub secret

    【讨论】:

      猜你喜欢
      • 2020-10-25
      • 2021-11-20
      • 1970-01-01
      • 2019-08-30
      • 2021-03-01
      • 2022-11-10
      • 1970-01-01
      • 2022-09-27
      • 2021-10-18
      相关资源
      最近更新 更多