Process Workflow Artifacts and Update Status After Echo Job #97
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: Process Workflow Artifacts and Update Status After Echo Job | |
on: | |
workflow_run: | |
workflows: ["Echo on PR"] | |
types: | |
- completed | |
jobs: | |
process_artifacts: | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
runs-on: ubuntu-latest | |
permissions: | |
actions: read | |
checks: read | |
contents: read | |
deployments: read | |
id-token: write | |
issues: read | |
discussions: read | |
packages: read | |
pages: read | |
pull-requests: read | |
repository-projects: read | |
security-events: read | |
statuses: write | |
steps: | |
- name: Download artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: gh-status | |
path: downloaded_artifacts/ | |
# workflow: copy.yml | |
run-id: ${{ github.event.workflow_run.id }} | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Create status | |
uses: actions/github-script@v7 | |
env: | |
WORKFLOW_RUN_ID: ${{ github.event.workflow_run.id }} | |
COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} | |
ATTEMPTS: ${{ github.event.workflow_run.run_attempt }} | |
with: | |
script: | | |
const fs = require('fs'); | |
const path = require('path'); | |
// Load the JSON content | |
const contentPath = path.join(process.env.GITHUB_WORKSPACE, 'downloaded_artifacts', 'gh-status.json'); | |
const contentJSON = JSON.parse(fs.readFileSync(contentPath)); | |
const { job_name: JOB_NAME, context: CUSTOM_CONTEXT = 'Custom CI Status Check', description: CUSTOM_DESCRIPTION = 'Custom CI Status description', target_url: CUSTOM_TARGET_URL, state: CUSTOM_STATE = 'success' } = contentJSON; | |
// Fetch the first job ID from the workflow run | |
const jobs = await github.rest.actions.listJobsForWorkflowRun({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: process.env.WORKFLOW_RUN_ID, | |
}); | |
const job = jobs.data.jobs.find(job => job.name === JOB_NAME); | |
const JOB_ID = job ? job.id : null; | |
// Set default target URL if not defined | |
const targetUrl = CUSTOM_TARGET_URL || `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.WORKFLOW_RUN_ID}/attempts/${process.env.ATTEMPTS}#summary-${JOB_ID}`; | |
// Create status | |
await github.rest.repos.createCommitStatus({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
sha: process.env.COMMIT_SHA, | |
state: CUSTOM_STATE, | |
target_url: targetUrl, | |
description: CUSTOM_DESCRIPTION, | |
context: CUSTOM_CONTEXT, | |
}); | |
retries: 4 | |
#abc123 |