From 7431b482dfa08610fe84185603cf648c1bfd6f79 Mon Sep 17 00:00:00 2001 From: katie Date: Mon, 18 Nov 2024 12:48:45 +0100 Subject: [PATCH] Added caching snap with github --- .../download-install-debian-deps/action.yaml | 0 .github/actions/download-snap/action.yaml | 70 +++++++++++++++++++ .../actions/install-offline-snap/action.yaml | 42 +++++++++++ .github/actions/install-snap/action.yaml | 42 +++++++++++ .github/workflows/static-checks.yaml | 2 +- .github/workflows/test.yaml | 4 +- .github/workflows/unit-tests.yaml | 10 +-- 7 files changed, 163 insertions(+), 7 deletions(-) rename .github/{workflows => }/actions/download-install-debian-deps/action.yaml (100%) create mode 100644 .github/actions/download-snap/action.yaml create mode 100644 .github/actions/install-offline-snap/action.yaml create mode 100644 .github/actions/install-snap/action.yaml diff --git a/.github/workflows/actions/download-install-debian-deps/action.yaml b/.github/actions/download-install-debian-deps/action.yaml similarity index 100% rename from .github/workflows/actions/download-install-debian-deps/action.yaml rename to .github/actions/download-install-debian-deps/action.yaml diff --git a/.github/actions/download-snap/action.yaml b/.github/actions/download-snap/action.yaml new file mode 100644 index 00000000000..5192910ca2b --- /dev/null +++ b/.github/actions/download-snap/action.yaml @@ -0,0 +1,70 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Canonical Ltd. + +name: Download snap package +author: Zygmunt Krynicki +description: | + Download snap action adapated from https://github.com/canonical/snapd-wsl-tests/blob/main/.github/actions/download-snap/action.yaml +inputs: + snap-name: + description: Name of the snap in the store + required: true + snap-revision: + description: Revision to download (mutually exclusive with channel) + snap-channel: + description: Name of the channel to use (mutually exclusive with revision) + default: latest/stable + snaps-dir: + description: Directory where snaps should be stored + default: snaps +outputs: + snap-name: + description: Name of the downloaded snap + value: ${{ inputs.snap-name }} + snap-revision: + description: Revision of the downloaded snap + value: ${{ inputs.snap-revision && inputs.snap-revision || steps.query-store.outputs.snap-revision }} + offline-snaps-dir: + description: Directory where downloaded snaps and assertions are stored + value: ${{ inputs.snaps-dir }} +branding: + icon: download + color: orange +runs: + using: composite + steps: + - name: Use specific revision + if: ${{ inputs.snap-revision != '' }} + shell: bash + env: + SNAP_REVISION: ${{ inputs.snap-revision }} + run: | + echo "SNAP_REVISION=$SNAP_REVISION" >> $GITHUB_ENV + - name: Query the Snap Store + id: query-store + if: ${{ inputs.snap-revision == '' }} + env: + SNAP_NAME: ${{ inputs.snap-name }} + SNAP_CHANNEL: ${{ inputs.snap-channel }} + shell: bash + run: | + SNAP_REVISION=$(curl -sS --unix-socket /run/snapd.socket http://localhost/v2/find?name=${SNAP_NAME} | jq -r ".result[].channels.\"${SNAP_CHANNEL}\".revision") + echo "SNAP_REVISION=$SNAP_REVISION" >> $GITHUB_ENV + - name: Cache ${{ inputs.snap-name }} + id: cache-snap + uses: actions/cache@v4 + with: + path: | + ${{ inputs.snaps-dir }}/${{ inputs.snap-name }}_${{ inputs.snap-revision && inputs.snap-revision || steps.query-store.outputs.snap-revision }}.snap + ${{ inputs.snaps-dir }}/${{ inputs.snap-name }}_${{ inputs.snap-revision && inputs.snap-revision || steps.query-store.outputs.snap-revision }}.assert + key: snaps/${{ inputs.snap-name }}-${{ inputs.snap-revision && inputs.snap-revision || steps.query-store.outputs.snap-revision }} + - name: Download ${{ inputs.snap-name }} + if: ${{ steps.cache-snap.outputs.cache-hit != 'true' }} + env: + SNAP_NAME: ${{ inputs.snap-name }} + SNAP_REVISION: ${{ inputs.snap-revision && inputs.snap-revision || steps.query-store.outputs.snap-revision }} + SNAPS_DIR: ${{ inputs.snaps-dir }} + shell: bash + run: | + mkdir -p $SNAPS_DIR + snap download --revision=$SNAP_REVISION --target-directory=$SNAPS_DIR $SNAP_NAME diff --git a/.github/actions/install-offline-snap/action.yaml b/.github/actions/install-offline-snap/action.yaml new file mode 100644 index 00000000000..48cbaecc0c5 --- /dev/null +++ b/.github/actions/install-offline-snap/action.yaml @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: Canonical Ltd. + +name: Install offline snap package +author: Zygmunt Krynicki +description: | + Install snap action using an initialized WSL distribution and downloaded snap + file. +inputs: + snap-name: + description: Name of the snap to install + required: true + snap-revision: + description: Revision of the snap to install + required: true + offline-snaps-dir: + description: Directory where downloaded snaps and assertions are stored + default: snaps + snap-classic-confinement: + description: Use classic confinement + type: boolean + default: false +branding: + icon: download + color: orange +runs: + using: composite + steps: + - name: Install ${{ inputs.snap-name}} revision ${{ inputs.snap-revision }} snap + env: + SNAP_NAME: ${{ inputs.snap-name }} + SNAP_REVISION: ${{ inputs.snap-revision }} + SNAP_CLASSIC_CONFINEMENT: ${{ inputs.snap-classic-confinement }} + OFFLINE_SNAPS_DIR: ${{ inputs.offline-snaps-dir }} + shell: bash + run: | + sudo snap ack "${OFFLINE_SNAPS_DIR}/${SNAP_NAME}_${SNAP_REVISION}.assert" + classic= + if [ ${{ inputs.snap-classic-confinement }} == 'true' ]; then + classic="--classic" + fi + sudo snap install $classic "${OFFLINE_SNAPS_DIR}/${SNAP_NAME}_${SNAP_REVISION}.snap" diff --git a/.github/actions/install-snap/action.yaml b/.github/actions/install-snap/action.yaml new file mode 100644 index 00000000000..66ef3ce3a0a --- /dev/null +++ b/.github/actions/install-snap/action.yaml @@ -0,0 +1,42 @@ +name: Install snap package +author: Zygmunt Krynicki +description: | + Install snap action +inputs: + snap-name: + description: Name of the snap to install + required: true + snap-channel: + description: Name of the channel to use (mutually exclusive with revision) + default: latest/stable + snaps-dir: + description: Directory where snaps should be stored + default: snaps + snap-classic-confinement: + description: Use classic confinement + type: boolean + default: false +outputs: + snap-revision: + description: Revision of the installed snap + value: ${{ steps.download.outputs.snap-revision }} +branding: + icon: download + color: orange +runs: + using: composite + steps: + - name: Download snap ${{ inputs.snap-name }} from the Snap Store + id: download + uses: ./.github/actions/download-snap + with: + snap-name: ${{ inputs.snap-name }} + snap-channel: ${{ inputs.snap-channel }} + snaps-dir: ${{ inputs.snaps-dir }} + - name: Install downloaded snap ${{ inputs.snap-name }} + uses: ./.github/actions/install-offline-snap + with: + snap-name: ${{ inputs.snap-name }} + snap-revision: ${{ steps.download.outputs.snap-revision }} + snap-classic-confinement: ${{ inputs.snap-classic-confinement }} + offline-snaps-dir: ${{ steps.download.outputs.offline-snaps-dir }} \ No newline at end of file diff --git a/.github/workflows/static-checks.yaml b/.github/workflows/static-checks.yaml index f9100e9b408..8406b9dbb62 100644 --- a/.github/workflows/static-checks.yaml +++ b/.github/workflows/static-checks.yaml @@ -37,7 +37,7 @@ jobs: - name: Download and install Debian dependencies # Github does not allow variables in "uses"; this has to be a hard-coded path - uses: ./.github/workflows/actions/download-install-debian-deps + uses: ./.github/actions/download-install-debian-deps with: snapd-src-dir: "${{ github.workspace }}" diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 652c5aaf7bf..5865a581302 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -113,7 +113,7 @@ jobs: fail-fast: false matrix: gochannel: - - 1.18 + - 1.18/stable - latest/stable # TODO run unit tests of C code @@ -132,7 +132,7 @@ jobs: fail-fast: false matrix: gochannel: - - 1.18 + - 1.18/stable - latest/stable test-case: - { go-build-tags: snapd_debug, skip-coverage: false, snapd-debug: true, go-test-race: false} diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index dfec28cd8cb..e833aed4f79 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -48,14 +48,16 @@ jobs: - name: Download and install Debian dependencies # Github does not allow variables in "uses"; this has to be a hard-coded path - uses: ./.github/workflows/actions/download-install-debian-deps + uses: ./.github/actions/download-install-debian-deps with: snapd-src-dir: "${{ github.workspace }}" - # golang latest ensures things work on the edge - name: Install the go snap - run: | - sudo snap install --classic --channel=${{ inputs.gochannel }} go + uses: ./.github/actions/install-snap + with: + snap-name: go + snap-channel: ${{ inputs.gochannel }} + snap-classic-confinement: true - name: Get deps run: |