Update 2024-11-15: @nil-two #730
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 'Update article' | |
run-name: 'Update ${{ github.event.issue.title }}: @${{ github.event.issue.user.login }}' | |
on: | |
issues: | |
types: | |
- 'opened' | |
- 'reopened' | |
- 'edited' | |
- 'closed' | |
jobs: | |
update-article: | |
name: 'Update article' | |
runs-on: 'ubuntu-latest' | |
if: | | |
contains(github.event.issue.labels.*.name, 'article') && ( | |
github.event.issue.state == 'open' || github.event.action == 'closed' || github.event.issue.state_reason == 'completed' | |
) | |
permissions: | |
issues: 'write' | |
steps: | |
- uses: 'actions/checkout@v4' | |
with: | |
ref: 'main' | |
ssh-key: '${{ secrets.ARTICLE_DEPLOY_KEY }}' | |
- uses: 'denoland/setup-deno@v2' | |
with: | |
deno-version: 'v2.0.0' | |
- name: 'Run script' | |
id: 'script' | |
run: | | |
echo 'RESULT<<MESSAGE' >> "${GITHUB_OUTPUT}" | |
deno --quiet run --allow-read --allow-write ./scripts/update-article.ts \ | |
"${GITHUB_EVENT_PATH}" \ | |
>> "${GITHUB_OUTPUT}" | |
echo 'MESSAGE' >> "${GITHUB_OUTPUT}" | |
- name: 'Push changes' | |
id: 'push' | |
if: | |
fromJSON(steps.script.outputs.RESULT).error == '' | |
env: | |
GIT_AUTHOR_NAME: '${{ github.event.issue.user.login }}' | |
GIT_AUTHOR_EMAIL: '${{ github.event.issue.user.login }}@users.noreply.github.com' | |
GIT_COMMITTER_NAME: 'github-actions[bot]' | |
GIT_COMMITTER_EMAIL: 'github-actions[bot]@users.noreply.github.com' | |
run: | | |
git add src/content.json | |
if git diff --cached --exit-code; then | |
exit | |
fi | |
git commit --message='Update content.json by #${{ github.event.issue.number }}' | |
echo "COMMIT_HASH=$(git rev-parse HEAD)" >> "${GITHUB_OUTPUT}" | |
git push | |
- name: 'Report' | |
uses: 'actions/github-script@v7' | |
env: | |
SCRIPT_RESULT: '${{ steps.script.outputs.RESULT }}' | |
COMMIT_HASH: '${{ steps.push.outputs.COMMIT_HASH }}' | |
with: | |
script: | | |
const issue = { | |
...context.repo, | |
issue_number: context.issue.number, | |
}; | |
const scriptResult = JSON.parse(process.env.SCRIPT_RESULT); | |
if (scriptResult.error) { | |
if (context.payload.action !== "closed") { | |
github.rest.issues.createComment({ | |
...issue, | |
body: `エラーが発生しました。\n\n- ${scriptResult.error}`, | |
}); | |
} else { | |
github.rest.issues.createComment({ | |
...issue, | |
body: `エラーが発生しました。\n\n- ${scriptResult.error}\n\n問題を解消したら再オープンしてから再び閉じてください。`, | |
}); | |
} | |
} else if (process.env.COMMIT_HASH) { | |
if ((context.payload.action === "opened" || context.payload.action === "reopened") && scriptResult.action === "insert") { | |
github.rest.issues.createComment({ | |
...issue, | |
body: `エントリーしました。\nキャンセルしたい場合は、Issue を \`Close as not planned\` で閉じてください。\n${process.env.COMMIT_HASH}`, | |
}); | |
} else if (context.payload.action === "closed" && scriptResult.action === "delete") { | |
github.rest.issues.update({ | |
...issue, | |
state_reason: "not_planned", | |
}); | |
github.rest.issues.createComment({ | |
...issue, | |
body: `キャンセルしました。\n${process.env.COMMIT_HASH}`, | |
}); | |
} | |
} | |
retry-on-failure: | |
name: 'Retry on failure' | |
runs-on: 'ubuntu-latest' | |
needs: 'update-article' | |
if: | | |
failure() && github.run_attempt < 5 | |
permissions: | |
actions: 'write' | |
steps: | |
- uses: 'actions/github-script@v7' | |
with: | |
script: | | |
github.rest.actions.createWorkflowDispatch({ | |
...context.repo, | |
workflow_id: "retry-update-article.yml", | |
ref: "main", | |
inputs: { | |
run_id: String(context.runId), | |
}, | |
}); |