Artifacts #28
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: Artifacts | |
on: | |
workflow_run: | |
workflows: ["CI"] | |
types: | |
- completed | |
branches-ignore: | |
- main | |
permissions: | |
pull-requests: write | |
jobs: | |
notify: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.event == 'pull_request' }} | |
steps: | |
- uses: actions/github-script@v7 | |
with: | |
script: | | |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: context.payload.workflow_run.id | |
}); | |
if (artifacts.data.total_count !== 1) { | |
throw new Error('Expected one artifact') | |
} | |
const artifact = artifacts.data.artifacts[0]; | |
const download = await github.rest.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: artifact.id, | |
archive_format: 'zip' | |
}); | |
const fs = require('fs'); | |
fs.writeFileSync('artifact.zip', Buffer.from(download.data)); | |
require('child_process').execSync('unzip artifact.zip'); | |
const ghInfo = JSON.parse(fs.readFileSync('gh.json', 'utf8')); | |
const pullNumber = ghInfo.number; | |
const artifactUrl = `${context.payload.workflow_run.html_url}/artifacts/${artifact.id}`; | |
const commentBody = `<!-- build-artifact-comment -->\n📦 Docs artifacts are ready: ${artifactUrl}`; | |
const comments = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: pullNumber | |
}); | |
const botComment = comments.data.find(comment => | |
comment.user.type === 'Bot' && | |
comment.body.includes('<!-- build-artifact-comment -->') | |
); | |
if (botComment) { | |
await github.rest.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: botComment.id, | |
body: commentBody | |
}); | |
} else { | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: pullNumber, | |
body: commentBody | |
}); | |
} |