Skip to content

Commit

Permalink
Added caching snap with github
Browse files Browse the repository at this point in the history
  • Loading branch information
maykathm committed Nov 18, 2024
1 parent 18bd70b commit 7431b48
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 7 deletions.
70 changes: 70 additions & 0 deletions .github/actions/download-snap/action.yaml
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions .github/actions/install-offline-snap/action.yaml
Original file line number Diff line number Diff line change
@@ -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"
42 changes: 42 additions & 0 deletions .github/actions/install-snap/action.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion .github/workflows/static-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
fail-fast: false
matrix:
gochannel:
- 1.18
- 1.18/stable
- latest/stable

# TODO run unit tests of C code
Expand All @@ -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}
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down

0 comments on commit 7431b48

Please sign in to comment.