如何在 GitHub 操作中将 package.json 版本作为 ENV 变量传递?

问题描述 投票:0回答:1

我有这个 GitHub 操作

on: push

jobs:
  test:
    runs-on: windows-2022
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Cache Tools
        uses: actions/[email protected]
        with:
          path: tools
          key: ${{ runner.os }}-cache
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20.10.x
      - name: Choco pack
        run: choco pack task.nuspec
      - name: LS
        run: dir .
      - name: Set PACKAGE_VERSION
        id: version
        run: echo "PACKAGE_VERSION=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
      - name: Choco install
        env:
          PACKAGE_VERSION: ${{ steps.version.outputs.PACKAGE_VERSION }}
        run:
          choco install task.${{ steps.version.outputs.PACKAGE_VERSION }}.nupkg
      - name: Choco set api key
        env:
          CHOCOLATEY_API_KEY: ${{ secrets.CHOCOLATEY_API_KEY }}
        run:
          choco apikey --key $CHOCOLATEY_API_KEY --source https://push.chocolatey.org/
      - name: Choco publish
        env:
          PACKAGE_VERSION: ${{ steps.version.outputs.PACKAGE_VERSION }}
        run:
          choco push task.${{ env.PACKAGE_VERSION }}.nupkg --source https://push.chocolatey.org/

Choco install
步骤中,它在日志中显示:

Run choco install task..nupkg
Chocolatey v2.2.2
Package name cannot point directly to a local, or remote file. Please use the --source argument and point it to a local file directory, UNC directory path or a NuGet feed instead.
Error: Process completed with exit code 1.

因此,使用

task..nupkg
显然没有传递 env 变量。我的仓库位于here
package.json
位于项目的顶层:

{
  "version": "0.0.2",
  ...
}

我至少尝试了这 3 种方法(如果不是更多的话)来让它正确地传递给它:

# this
- name: Choco install
  env:
    PACKAGE_VERSION: ${{ steps.version.outputs.PACKAGE_VERSION }}
  run:
    choco install task.${{ steps.version.outputs.PACKAGE_VERSION }}.nupkg
# or this
- name: Choco install
  env:
    PACKAGE_VERSION: ${{ steps.version.outputs.PACKAGE_VERSION }}
  run:
    choco install task.$PACKAGE_VERSION.nupkg
    # results in literal 'Run choco install task.$PACKAGE_VERSION.nupkg' error
# or this
- name: Choco install
  env:
    PACKAGE_VERSION: ${{ steps.version.outputs.PACKAGE_VERSION }}
  run:
    choco install task.${{ env.PACKAGE_VERSION }}.nupkg
    # results in 'Run choco install task..nupkg' error

在本地运行

node
命令会给出以下结果:

$ echo "PACKAGE_VERSION=$(node -p "require('./package.json').version")"
PACKAGE_VERSION=0.0.2

所以看起来它正在工作。

我错过了什么?我正在使用

GITHUB_OUTPUT
功能,但如果有更好的方法请告诉我。

基本上我只是想用正确的版本构建包,所以如果有更好的方法让我知道,也许使用

choco
命令。

environment-variables github-actions chocolatey
1个回答
0
投票

@Azeem 是对的(我同时想到的),我使用的是 PowerShell,而这是使用 Bash。所以我改变了它并且它有效。

- name: Set ENV vars
  id: version
  run: |
    echo "PACKAGE_VERSION=$(cat choco.json | jq -r '.version')" >> $env:GITHUB_ENV
- name: Choco publish
  env:
    CHOCOLATEY_API_KEY: ${{ secrets.MY_KEY }}
  run:
    cd load/choco && choco push --key $env:MY_KEY task.${{ env.PACKAGE_VERSION }}.nupkg --source
    https://push.chocolatey.org/
© www.soinside.com 2019 - 2024. All rights reserved.