我無法在 GitHub Actions 中訪問全域 npm 命令的輸出。運行任何全域 linting npm 包(我已經嘗試了幾個不同的包)總是以: 退出Process completed with exit code 1.。
有趣的是,我可以在本地機器上運行這些 bash 命令。
這是我正在嘗試做的簡化版本:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16.13.2
- name: Install npm package
run: npm install markdownlint-cli --location=global
- name: Run standard-markdown
id: run_md_lint
run: |
VAR=$((markdownlint **/*.md --ignore node_modules) 2>&1)
echo "::set-output name=LINT_RESULT::$VAR"
- name: Run a one-line script
run: echo "Resultes ${{ steps.run_md_lint.outputs.LINT_RESULT }}"
運行步驟的預期輸出記錄在 GitHub 操作日志中,但無法訪問結果并將其保存到變數中——我猜是由于退出錯誤。

我應該提到,這樣做的最終目標是在打開 PR 時捕獲輸出并將其添加到評論中。
- name: Add PR comment
uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "${{ steps.run_md_lint.outputs.LINT_RESULT }}"
})
任何想法表示贊賞。
編輯:它有效!
非常感謝@VonC 幫助解決我的問題。幾點注意事項:
- 如下所述,我需要添加
|| true到命令以回傳不同的退出狀態。 set-output僅適用于單行字串。我需要替換換行符和回車來保存多行。- npm 包必須與
-g.-location=global不起作用。
這是有效的 GitHub 操作:
name: Lint the docs!
on:
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Set up Node.js
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16.13.2
- name: Install markdownlint
run: npm install -g markdownlint-cli
- name: Run standard-markdown
id: run_md_lint
run: |
LINT=$(((markdownlint **/*.md --ignore node_modules) 2>&1) || true)
LINT="${LINT//'%'/'%'}"
LINT="${LINT//$'\n'/'
'}"
LINT="${LINT//$'\r'/'
'}"
echo "::set-output name=LINT_RESULT::$LINT"
- name: Add PR comment
uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Lint results:
\`\`\`bash
${{ steps.run_md_lint.outputs.LINT_RESULT }}
\`\`\`
`
})
uj5u.com熱心網友回復:
在這個執行緒中,嘗試添加|| true回傳不同于 0 的退出狀態的命令(插圖)
就像是:
VAR=$(((markdownlint **/*.md --ignore node_modules) 2>&1) || true)
# or
VAR=$(((markdownlint **/*.md --ignore node_modules) || true) 2>&1)
這將強制 GitHub 腳本運行步驟不將該步驟視為失敗。
OP 確認它適用于第一種語法:
- name: Install markdownlint
run: npm install -g markdownlint-cli
- name: Run standard-markdown
id: run_md_lint
run: |
LINT=$(((markdownlint **/*.md --ignore node_modules) 2>&1) || true)
LINT="${LINT//'%'/'%'}"
LINT="${LINT//$'\n'/'
'}"
LINT="${LINT//$'\r'/'
'}"
echo "::set-output name=LINT_RESULT::$LINT"
- name: Add PR comment
uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Lint results:
\`\`\`bash
${{ steps.run_md_lint.outputs.LINT_RESULT }}
\`\`\`
`
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494068.html
標籤:节点.js github npm github-动作 构建-github-动作
上一篇:如何讓npm或sass全域安裝?
