我想改变这个工作流程,今天我签入一个 YAML 文件,一些代码运行,除非我手动完成预工作,否则将会失败。如果我想让它为我做这件事,我可以评论
!confirm
,否则我会被屏蔽。
name: Check and Run
on:
pull_request:
types:
- opened
- synchronize
jobs:
. . .
do_checks_and_work_if_true:
uses: ./.github/workflows/plan.yaml
secrets: inherit
with:
. . .
我输入
!confirm
,可重用工作流程会再次检查我是否完成了该工作,这次成功了。
name: Rerun On Confirm Comment
on:
issue_comment:
types: [created]
jobs:
. . .
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '!confirm')
do_checks_and_work_if_true:
uses: ./.github/workflows/plan.yaml
secrets: inherit
with:
. . .
但是 - 第二次运行后,即使在“操作”选项卡中被认为是成功的,它仍然从原始检查中报告失败/红色。
我是否必须以某种方式引用原始运行并通过 API 更新它?我看到 GitHub 检查 上有文档,但我不确定这是正确的路径还是有更简单的方法。
我看到 GitHub 检查上有文档,但我不确定这是正确的路径还是有更简单的方法。
我没有看到“更简单”或更原生的方法来做到这一点:您确实可以使用 GitHub Checks API 来更新原始运行的状态。
在
Rerun On Confirm Comment
工作流程中,添加一个步骤,触发脚本以使用 GitHub Checks API 更新原始检查运行的状态。
该脚本将:
Check and Run
工作流程相关的检查运行。
name: Rerun On Confirm Comment
on:
issue_comment:
types: [created]
jobs:
check_and_update:
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '!confirm')
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Rerun Checks and Update Status
uses: ./.github/workflows/plan.yaml
secrets: inherit
with:
. . .
- name: Update Original Check Status
run: |
# Add script/commands to update the status using GitHub Checks API
您将需要具有必要权限的 GitHub 令牌(checks:write
和
checks:read
)。该令牌应安全存储并在脚本中用于身份验证。
#!/bin/bash
# Variables
GITHUB_TOKEN="<YOUR-TOKEN>"
REPO_OWNER="<REPO-OWNER>"
REPO_NAME="<REPO-NAME>"
CHECK_RUN_ID=<CHECK-RUN-ID> # You should find a way to get this ID
HEAD_SHA="<COMMIT-SHA>" # The SHA of the commit for which to update the check run
# GitHub API URL for updating a check run
API_URL="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/check-runs/${CHECK_RUN_ID}"
# Data for updating the check run
PAYLOAD=$(cat <<EOF
{
"name": "mighty_readme",
"head_sha": "${HEAD_SHA}",
"status": "completed",
"conclusion": "success",
"completed_at": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"output": {
"title": "Mighty Readme report",
"summary": "The check run has completed successfully."
}
}
EOF
)
# Update the check run
curl -L \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-d "$PAYLOAD" \
"$API_URL"
除了列出和“猜测”检查运行 ID,您还可以在初始作业(您想要跟踪的作业)结束时添加一个步骤来创建包含工作流运行 ID 的文件并将其作为神器。
GitHub Actions 将工作流程运行 ID 公开为 GITHUB_RUN_ID
环境变量。
- name: Save Check Run ID
run: echo $GITHUB_RUN_ID > run_id.txt
- name: Upload Run ID
uses: actions/upload-artifact@v2
with:
name: run-id
path: run_id.txt
在需要 CHECK_RUN_ID
的作业中,下载上一个作业中的工件。并从文件中提取运行 ID。
#!/bin/bash
# Variables
GITHUB_TOKEN="<YOUR-TOKEN>"
REPO_OWNER="<REPO-OWNER>"
REPO_NAME="<REPO-NAME>"
ARTIFACT_NAME="run-id" # Name of the artifact uploaded in the initial job
WORKFLOW_RUN_ID_FILE="run_id.txt"
# GitHub API URLs
ARTIFACTS_API_URL="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/actions/runs/${GITHUB_RUN_ID}/artifacts"
CHECK_RUNS_API_URL="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/commits/${HEAD_SHA}/check-runs"
# Download the artifact with the run ID
# Get the download URL for the artifact
ARTIFACT_DOWNLOAD_URL=$(curl -s \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${ARTIFACTS_API_URL}" | jq -r ".artifacts[] | select(.name==\"${ARTIFACT_NAME}\") | .archive_download_url")
# Download the artifact zip file
curl -L \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
-o "artifact.zip" \
"${ARTIFACT_DOWNLOAD_URL}"
# Extract the workflow run ID from the artifact
unzip artifact.zip "${WORKFLOW_RUN_ID_FILE}"
WORKFLOW_RUN_ID=$(cat "${WORKFLOW_RUN_ID_FILE}")
# Retrieve the check run ID using the workflow run ID
CHECK_RUN_ID=$(curl -s \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${CHECK_RUNS_API_URL}" | jq -r ".check_runs[] | select(.id==${WORKFLOW_RUN_ID}) | .id")
# Output the check run ID
echo "Check Run ID: ${CHECK_RUN_ID}"