gpt4 book ai didi

powershell - 从 .ps1 脚本设置环境变量值在 Github Actions 中不起作用

转载 作者:行者123 更新时间:2023-12-02 23:49:56 26 4
gpt4 key购买 nike

我在 Github Actions 中有两个 ps1 脚本。我的场景:

  1. 第一个脚本在构建之前执行
  2. 项目构建
  3. 第二个脚本在构建后执行。

我需要在第一个脚本中设置值并在第二个脚本中使用它。所以我决定使用 BUILD_NUMBER 环境变量并将其设置为 10 作为默认值。

jobs:
Droid:
runs-on: windows-latest
env:
BUILD_NUMBER: "10"

在第一个脚本中,我尝试以多种方式设置此变量,但在第二个脚本中,BUILD_NUMBER 的值为 10。

我尝试设置它:

[Environment]::SetEnvironmentVariable($env:BUILD_NUMBER, $buildNumber, 'Machine')

$env:BUILD_NUMBER: '123'

但在第二个脚本中,我通过这个 $newName = "${env:BUILD_NUMBER}" 获得了 10 个值

Github Actions端的全部代码:

name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches:
- 'master'
- 'develop'
- 'feature/*'
- 'rc/*'
pull_request:
branches:
- 'master'
- 'develop'
- 'feature/*'
- 'rc/*'


jobs:
Droid:
runs-on: windows-latest
env:
DOTNET_CLI_TELEMETRY_OPTOUT: 'true'
BUILD_NUMBER: "10"

steps:
- uses: actions/checkout@v1
- name: Run a calculate version and set sign in password script
run: .\Scripts\CalculateVersionAndSetSignPassword.ps1
shell: powershell

# Build goes here. It is skipped by me for testing purposes

- uses: actions/checkout@v1
- name: Run a change apk name script
run: |
.\Scripts\ChangeApkName.ps1
shell: powershell

最佳答案

set-env 已被删除 - 请检查 GitHub Actions: Deprecating set-env and add-path commands

作为替代你可以使用

echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV

然后:

jobs:
show:
runs-on: ubuntu-latest
steps:
- name: Is variable exported?
run: |
echo "BUILD_NUMBER=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:BUILD_NUMBER
- name: Read exported variable
run: |
echo "${{ env.BUILD_NUMBER}}"

关于powershell - 从 .ps1 脚本设置环境变量值在 Github Actions 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62478183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com