From a292f14fe51170d385430a4ffaf3128a7c95d89c Mon Sep 17 00:00:00 2001 From: Alex Lowe Date: Tue, 17 Mar 2026 18:00:13 -0400 Subject: [PATCH] feat(snapcraft): snapcraft test action --- .github/workflows/test-snapcraft.yaml | 40 +++++++++ snapcraft/test/action.yaml | 117 ++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 snapcraft/test/action.yaml diff --git a/.github/workflows/test-snapcraft.yaml b/.github/workflows/test-snapcraft.yaml index 5c8353b..f0c39a6 100644 --- a/.github/workflows/test-snapcraft.yaml +++ b/.github/workflows/test-snapcraft.yaml @@ -85,4 +85,44 @@ jobs: name: test-snap-${{ matrix.os }}-${{ steps.snapcraft.outputs.snapcraft-revision }} path: test-repo/tests/spread/core22/clean/snaps/clean/*.snap overwrite: true + test: + strategy: + fail-fast: false + matrix: + os: + - ubuntu-22.04 + - ubuntu-24.04 + - ubuntu-22.04-arm + - ubuntu-24.04-arm + - self-hosted-linux-amd64-focal-medium + - self-hosted-linux-ppc64el-noble-edge + - self-hosted-linux-s390x-noble-edge + channel: [latest/candidate, latest/edge, 8.x/candidate] + parameters: ['', '--help'] + include: + - os: ubuntu-24.04 + channel: '' + revision: 16570 # 8.13.2, but really here to test the revision field. + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v5 + - uses: actions/checkout@v5 + with: + repository: lengau/astral-ty + path: test-repo + ref: main + - name: Test snap + id: snapcraft + uses: ./snapcraft/test + with: + path: test-repo + channel: ${{ matrix.channel }} + revision: ${{ matrix.revision }} + - name: Upload snap + continue-on-error: true + uses: actions/upload-artifact@v4 + with: + name: test-snap-${{ matrix.os }}-${{ steps.snapcraft.outputs.snapcraft-revision }} + path: test-repo/*.snap + overwrite: true diff --git a/snapcraft/test/action.yaml b/snapcraft/test/action.yaml new file mode 100644 index 0000000..be4a0b8 --- /dev/null +++ b/snapcraft/test/action.yaml @@ -0,0 +1,117 @@ +name: 'Test a snap' +description: 'Test a snap with Snapcraft 8 or higher' +author: Canonical + +inputs: + path: + description: > + The location of the Snapcraft project. + + Defaults to the root of the repository. + default: '.' + verbosity: + description: > + The build verbosity level, which can be set to "quiet", "brief", "verbose", "debug", + or "trace". + + The default is "trace". + default: 'trace' + parameters: + description: > + Additional parameters to pass to snapcraft test. + default: '' + channel: + description: > + The channel to install Snapcraft from. + + Defaults to the latest stable release. + default: 'latest/stable' + revision: + description: > + The revision of Snapcraft to install. + + Defaults to the latest stable revision. + + Overrides the `channel` option. + default: '' + lxd-channel: + description: > + The channel to install LXD from. + + Defaults to the recommended channel for Snapcraft. + default: '5.21/stable' + +outputs: + lxd-revision: + description: The LXD revision used. + value: ${{ steps.setup.outputs.lxd-revision }} + snapcraft-revision: + description: The Snapcraft revision used. + value: ${{ steps.setup.outputs.snapcraft-revision }} + snap: + description: The packed snap artifact. + value: ${{ steps.test.outputs.snap }} + components: + description: The packed components. + value: ${{ steps.test.outputs.components }} + +runs: + using: 'composite' + steps: + # This action is necessary as a workaround for https://github.com/actions/runner/issues/895. + # Since contexts cannot be used in the `uses:` key, we can't guarantee that this action will + # use the same branch of craft-actions to call other craft-actions workflows without just + # making our own local copy of the repository. See https://github.com/canonical/craft-actions/pull/45#discussion_r2586507869 + # for a full explanation. + - uses: actions/checkout@v6 + env: + GITHUB_REF: ${{ github.action_ref }} + with: + repository: 'canonical/craft-actions' + ref: ${{ env.GITHUB_REF }} + path: _tmp_craft-actions + sparse-checkout: | + snapcraft/ + + - uses: ./_tmp_craft-actions/snapcraft/setup + id: setup + with: + channel: ${{ inputs.channel }} + revision: ${{ inputs.revision }} + lxd-channel: ${{ inputs.lxd-channel }} + + - shell: bash + run: | + rm -rf ./_tmp_craft-actions + + - shell: bash + id: test + env: + CRAFT_VERBOSITY_LEVEL: ${{ inputs.verbosity }} + working-directory: ${{ inputs.path }} + run: | + # Snapcraft < 8 doesn't support `snapcraft test`. + # Examples of running `snapcraft --version`: + # snapcraft 8.14.0.post57+dirty + # snapcraft 8.14.2 + # snapcraft 7.5.9 + # snapcraft, version 6.1 + SNAPCRAFT_VERSION=$(snapcraft --version | awk '{print $NF}') + MAJOR_VERSION=$(echo "$SNAPCRAFT_VERSION" | cut -d'.' -f1) + if [[ "$MAJOR_VERSION" =~ ^[0-9]+$ ]] && [ "$MAJOR_VERSION" -lt 8 ]; then + echo "Snapcraft 8 needed for the test command" >> /dev/stderr + exit 1 + else + snapcraft test ${{ inputs.parameters }} + fi + echo "snap=$(ls *.snap)" >> "$GITHUB_OUTPUT" + components=( *.comp ) + if [ -e "${components[0]}" ]; then + echo "components=${components[*]}" >> "$GITHUB_OUTPUT" + else + echo "components=" >> "$GITHUB_OUTPUT" + fi + +branding: + icon: package + color: orange