|
| 1 | +name: snap-publish |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [release/snap-publish] |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + build_run_id: |
| 8 | + description: Successful snap-strict build run to publish |
| 9 | + required: true |
| 10 | + default: '39972678135' |
| 11 | + publish_to_store: |
| 12 | + description: Upload and release to Snap Store latest/stable |
| 13 | + type: boolean |
| 14 | + default: true |
| 15 | +permissions: |
| 16 | + actions: read |
| 17 | + contents: write |
| 18 | +jobs: |
| 19 | + publish: |
| 20 | + runs-on: ubuntu-24.04 |
| 21 | + timeout-minutes: 20 |
| 22 | + env: |
| 23 | + GH_TOKEN: ${{ github.token }} |
| 24 | + BUILD_RUN_ID: ${{ inputs.build_run_id || '39972678135' }} |
| 25 | + steps: |
| 26 | + - name: Require successful strict build |
| 27 | + id: source |
| 28 | + run: | |
| 29 | + [[ "$BUILD_RUN_ID" =~ ^[0-9]+$ ]] |
| 30 | + gh api "repos/$GITHUB_REPOSITORY/actions/runs/$BUILD_RUN_ID" > source-run.json |
| 31 | + python3 - <<'PY' |
| 32 | + import json, os |
| 33 | + run = json.load(open('source-run.json')) |
| 34 | + assert run['conclusion'] == 'success', 'Source build did not pass' |
| 35 | + assert run['path'] == '.github/workflows/snap.yml', 'Not a snap-strict build' |
| 36 | + with open(os.environ['GITHUB_OUTPUT'], 'a') as f: |
| 37 | + f.write('sha=' + run['head_sha'] + '\n') |
| 38 | + PY |
| 39 | + - uses: actions/download-artifact@v4 |
| 40 | + with: |
| 41 | + name: medha-strict-snap |
| 42 | + run-id: ${{ env.BUILD_RUN_ID }} |
| 43 | + github-token: ${{ github.token }} |
| 44 | + path: package |
| 45 | + - name: Verify package identity and test evidence |
| 46 | + id: package |
| 47 | + run: | |
| 48 | + sudo apt-get update -qq |
| 49 | + sudo apt-get install -y squashfs-tools python3-yaml |
| 50 | + python3 - <<'PY' |
| 51 | + import json, os, pathlib, re, subprocess, yaml |
| 52 | + snaps = list(pathlib.Path('package').rglob('*.snap')) |
| 53 | + assert len(snaps) == 1, 'Expected exactly one Snap package' |
| 54 | + snap = snaps[0] |
| 55 | + metadata = yaml.safe_load(subprocess.check_output(['unsquashfs', '-cat', str(snap), 'meta/snap.yaml'])) |
| 56 | + assert metadata['name'] == 'medha-ide' |
| 57 | + assert metadata['confinement'] == 'strict' |
| 58 | + version = str(metadata['version']) |
| 59 | + assert re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+', version) |
| 60 | + evidence = list(pathlib.Path('package').rglob('gui-smoke.json')) |
| 61 | + assert len(evidence) == 1 and json.loads(evidence[0].read_text())['status'] == 'PASS' |
| 62 | + with open(os.environ['GITHUB_OUTPUT'], 'a') as f: |
| 63 | + f.write(f'snap={snap}\nversion={version}\n') |
| 64 | + PY |
| 65 | + - name: Publish verified package on GitHub |
| 66 | + env: |
| 67 | + SNAP_FILE: ${{ steps.package.outputs.snap }} |
| 68 | + VERSION: ${{ steps.package.outputs.version }} |
| 69 | + SOURCE_SHA: ${{ steps.source.outputs.sha }} |
| 70 | + run: | |
| 71 | + cd "$(dirname "$SNAP_FILE")" |
| 72 | + sha256sum "$(basename "$SNAP_FILE")" > SHA256SUMS |
| 73 | + printf 'Strict Snap from successful build %s. Installed compiler and offscreen GUI tests passed. GDB/host-installed tools are not supported by this confined package. Snap Store release is tracked separately.\n' "$BUILD_RUN_ID" > release-notes.txt |
| 74 | + if ! gh release view "snap-$VERSION" >/dev/null 2>&1; then |
| 75 | + gh release create "snap-$VERSION" --target "$SOURCE_SHA" --title "MEDHA $VERSION strict Snap" --notes-file release-notes.txt |
| 76 | + fi |
| 77 | + gh release upload "snap-$VERSION" "$(basename "$SNAP_FILE")" SHA256SUMS --clobber |
| 78 | + printf '## Verified Snap available\n\nhttps://github.com/%s/releases/tag/snap-%s\n\nSource build: %s\n' "$GITHUB_REPOSITORY" "$VERSION" "$BUILD_RUN_ID" >> "$GITHUB_STEP_SUMMARY" |
| 79 | + - name: Require Snap Store publisher credential |
| 80 | + if: github.event_name == 'workflow_dispatch' && inputs.publish_to_store |
| 81 | + env: |
| 82 | + SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }} |
| 83 | + run: | |
| 84 | + if [ -z "$SNAPCRAFT_STORE_CREDENTIALS" ]; then |
| 85 | + echo '::error::Add the medha-ide scoped SNAPCRAFT_STORE_CREDENTIALS repository secret. GitHub access alone cannot authorize a Snap Store upload.' |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | + - name: Release to Snap Store stable |
| 89 | + if: github.event_name == 'workflow_dispatch' && inputs.publish_to_store |
| 90 | + uses: snapcore/action-publish@v1 |
| 91 | + env: |
| 92 | + SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }} |
| 93 | + with: |
| 94 | + snap: ${{ steps.package.outputs.snap }} |
| 95 | + release: stable |
0 commit comments