Release from Artifacts #2
This file contains hidden or 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: Release from Artifacts | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| tag: | |
| description: "Tag to create (e.g. v1.2.3)" | |
| required: true | |
| name: | |
| description: "Release name (optional)" | |
| required: false | |
| body: | |
| description: "Release body (optional)" | |
| required: false | |
| draft: | |
| description: "Create as draft? (true/false)" | |
| required: false | |
| default: "false" | |
| prerelease: | |
| description: "Create as prerelease? (true/false)" | |
| required: false | |
| default: "false" | |
| jobs: | |
| create-release: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| actions: read | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Find latest successful "Build and Test" run and list artifacts | |
| id: find_artifacts | |
| uses: actions/github-script@v6 | |
| with: | |
| script: | | |
| const workflowName = 'Build and Test'; | |
| const runs = await github.rest.actions.listWorkflowRunsForRepo({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| per_page: 50, | |
| }); | |
| const run = runs.data.workflow_runs.find(r => r.name === workflowName && r.conclusion === 'success'); | |
| if (!run) { | |
| core.setFailed('No recent successful "Build and Test" workflow run found.'); | |
| return; | |
| } | |
| const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: run.id, | |
| }); | |
| const fs = require('fs'); | |
| fs.writeFileSync('artifacts.json', JSON.stringify({run_id: run.id, artifacts: artifacts.data.artifacts}, null, 2)); | |
| core.setOutput('run_id', run.id); | |
| - name: Download artifacts | |
| run: | | |
| set -euo pipefail | |
| echo "Artifacts file contents:" && cat artifacts.json | |
| mkdir -p downloaded_artifacts | |
| jq -c '.artifacts[]' artifacts.json | while read -r a; do | |
| name=$(echo "$a" | jq -r '.name') | |
| url=$(echo "$a" | jq -r '.archive_download_url') | |
| echo "Downloading $name -> downloaded_artifacts/$name" | |
| mkdir -p "downloaded_artifacts/$name" | |
| tmp=$(mktemp) | |
| curl -L -H "Authorization: token $GITHUB_TOKEN" -o "$tmp" "$url" | |
| unzip -o "$tmp" -d "downloaded_artifacts/$name" | |
| rm -f "$tmp" | |
| done | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Create release | |
| id: create_release | |
| uses: actions/create-release@v1 | |
| with: | |
| tag_name: ${{ github.event.inputs.tag }} | |
| release_name: ${{ github.event.inputs.name || github.event.inputs.tag }} | |
| body: ${{ github.event.inputs.body || '' }} | |
| draft: ${{ github.event.inputs.draft }} | |
| prerelease: ${{ github.event.inputs.prerelease }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Upload extracted artifacts to release | |
| run: | | |
| set -e | |
| echo "Uploading extracted files to release..." | |
| UPLOAD_URL=${{ steps.create_release.outputs.upload_url }} | |
| UPLOAD_URL=${UPLOAD_URL//\{*\}/} | |
| for f in downloaded_artifacts/*; do | |
| if [ -d "$f" ]; then | |
| for file in "$f"/*; do | |
| [ -f "$file" ] || continue | |
| name=$(basename "$file") | |
| echo "Uploading $file as $name" | |
| curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Content-Type: application/octet-stream" --data-binary @"$file" "$UPLOAD_URL?name=$name" | |
| done | |
| else | |
| if [ -f "$f" ]; then | |
| name=$(basename "$f") | |
| echo "Uploading $f as $name" | |
| curl -s -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -H "Content-Type: application/octet-stream" --data-binary @"$f" "$UPLOAD_URL?name=$name" | |
| fi | |
| fi | |
| done | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |