Publish PR #142
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: Publish PR | |
# read-write repo token | |
# access to secrets | |
on: | |
workflow_run: | |
workflows: ["Build PR"] | |
types: | |
- completed | |
jobs: | |
upload: | |
runs-on: ubuntu-latest | |
if: | | |
( | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
) | |
steps: | |
- name: download artifacts | |
uses: actions/github-script@v3.1.0 | |
with: | |
script: | | |
const artifacts = await github.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{github.event.workflow_run.id }}, | |
}); | |
const snapArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "snap" | |
})[0]; | |
const prDataArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "pr-data" | |
})[0]; | |
if (snapArtifact == null) { | |
return core.setFailed("Missing artifact"); | |
} | |
if (prDataArtifact == null) { | |
return core.setFailed("Missing PR data artifact"); | |
} | |
const snapDownload = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: snapArtifact.id, | |
archive_format: 'zip', | |
}); | |
const prDataDownload = await github.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: prDataArtifact.id, | |
archive_format: 'zip', | |
}); | |
const fs = require('fs'); | |
fs.writeFileSync('${{github.workspace}}/snap.zip', Buffer.from(snapDownload.data)); | |
fs.writeFileSync('${{github.workspace}}/pr-data.zip', Buffer.from(prDataDownload.data)); | |
- name: unzip pr-data | |
run: unzip pr-data.zip | |
- name: get PR data | |
id: get_pr_data | |
run: | | |
pr_number="$(cat pr-number)" | |
pr_commit="$(cat pr-commit)" | |
echo "::set-output name=pr_number::$pr_number" | |
echo "::set-output name=pr_commit::$pr_commit" | |
- name: check if allowed | |
uses: actions/github-script@v6.1.0 | |
with: | |
script: | | |
const pr = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: ${{ steps.get_pr_data.outputs.pr_number }} | |
}); | |
if (pr && pr.data.labels) { | |
const index = pr.data.labels.findIndex(label => (label.name === "safe to publish")); | |
if (index !== -1) { | |
return; | |
} | |
} | |
core.setFailed("Pull request needs the 'safe to publish' label"); | |
- name: unzip snap | |
run: unzip snap.zip | |
- name: install snapcraft | |
run: sudo snap install snapcraft --classic | |
- name: publish snap as branch to edge/pr-$id | |
id: upload_snap | |
env: | |
SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.STORE_LOGIN }} | |
run: | | |
pr_number="${{ steps.get_pr_data.outputs.pr_number }}" | |
snap_channel="edge/pr-${pr_number}" | |
snapcraft upload --release="$snap_channel" freecad*.snap | |
# get revision | |
snap_revision=$(snapcraft status freecad --track latest | grep "$snap_channel" | awk '{print $3}') | |
echo "Snap release channel: ${snap_channel}" | |
echo "Snap revision: ${snap_revision}" | |
echo "::set-output name=snap_channel::$snap_channel" | |
echo "::set-output name=snap_revision::$snap_revision" | |
- name: comment on PR | |
uses: actions/github-script@v3 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
const prNumber = Number(${{ steps.get_pr_data.outputs.pr_number }}); | |
const prCommit = "${{ steps.get_pr_data.outputs.pr_commit }}"; | |
const snapRevision = '${{ steps.upload_snap.outputs.snap_revision }}'; | |
const snapChannel = '${{ steps.upload_snap.outputs.snap_channel }}'; | |
await github.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: prNumber, | |
body: `A build from commit ${prCommit} has been published to channel ${snapChannel} as revision ${snapRevision}. | |
## Installation instructions | |
\`\`\`bash | |
# Note: not all distros require 'sudo' | |
$ sudo snap install --channel=${snapChannel} freecad | |
# If you already have the freecad snap installed | |
$ sudo snap refresh --channel=${snapChannel} freecad | |
# Experimental: Parallel install (see https://github.com/FreeCAD/FreeCAD-snap/blob/master/docs/index.md#parallel-installs) | |
$ sudo snap set system experimental.parallel-instances=true | |
$ sudo snap install --channel=${snapChannel} freecad_pr${prNumber} | |
$ freecad_pr${prNumber} # Run it | |
# To switch back to preferred snap (swap out 'edge' for 'stable' if necessary) | |
$ snap refresh freecad --channel=edge freecad | |
\`\`\`` | |
}); | |