From 4b951ef093a7cf0ff8da3fa3c0f1c87ef719ba5c Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Fri, 5 Apr 2024 16:23:24 -0500 Subject: [PATCH] Add custom status check workflow (#15464) This PR adds a custom workflow that creates a custom github status check to `cudf` that will run after `workflow_run` event is triggered. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - AJ Schmidt (https://github.com/ajschmidt8) - Ray Douglass (https://github.com/raydouglass) URL: https://github.com/rapidsai/cudf/pull/15464 --- .github/workflows/status.yaml | 115 ++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 .github/workflows/status.yaml diff --git a/.github/workflows/status.yaml b/.github/workflows/status.yaml new file mode 100644 index 00000000000..0aad4c8a23e --- /dev/null +++ b/.github/workflows/status.yaml @@ -0,0 +1,115 @@ +name: Custom GH Status from Workflow Artifacts + +on: + workflow_run: + workflows: ["pr"] + types: + - completed + +jobs: + process_artifacts: + if: ${{ github.event.workflow_run.conclusion == 'success' }} + runs-on: ubuntu-latest + outputs: + artifact_downloaded: ${{ steps.download_artifact.outputs.artifact_downloaded }} + 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 + id: download_artifact + uses: actions/github-script@v7 + with: + retries: 3 + script: | + const fs = require('fs'); + const path = require('path'); + const artifactName = 'gh-status'; + + const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); + // Find the specific artifact + const artifact = allArtifacts.data.artifacts.find(artifact => artifact.name === artifactName); + if (!artifact) { + core.info(`Artifact "${artifactName}" not found. Exiting safely.`); + core.setOutput('artifact_downloaded', 'false'); + return; + } + core.setOutput('artifact_downloaded', 'true'); + // Download the artifact + const download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: artifact.id, + archive_format: 'zip', + }); + + // Write the artifact to a file + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/${artifactName}.zip`, Buffer.from(download.data)); + + - name: 'Unzip artifact' + if: ${{ steps.download_artifact.outputs.artifact_downloaded == 'true' }} + run: unzip 'gh-status.zip' + + - name: Create status + if: ${{ steps.download_artifact.outputs.artifact_downloaded == 'true' }} + 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: + retries: 3 + script: | + // Load the JSON content + const contentJSON = require('./gh-status.json'); + 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}`; + + console.log("job id: ", JOB_ID); + console.log("state: ", CUSTOM_STATE); + console.log("target url: ", targetUrl); + console.log("description: ", CUSTOM_DESCRIPTION); + console.log("context: ", CUSTOM_CONTEXT); + + // 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, + });