Skip to content

Add pre-run-script to the test workflow #60

Add pre-run-script to the test workflow

Add pre-run-script to the test workflow #60

# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
name: Workflow Self-Test (Charmcraft Cache)
on:
pull_request:
jobs:
save-cache:
runs-on: ubuntu-latest
strategy:
matrix:
charm: [ indico-operator ]
steps:
- uses: actions/checkout@master
with:
repository: canonical/${{ matrix.charm }}
- name: Generate charmcraft cache key
run: |
CHARM_NAME=$(yq .name ./metadata.yaml)
CHARMCRAFT_BASE="$(yq -o=j -I=0 '.bases | sort_keys(..)' ./charmcraft.yaml)"
CHARMCRAFT_BASE=$(printf %s "$CHARMCRAFT_BASE"|jq -sRr @uri)
CHARMCRAFT_CACHE_KEY_BASE="charmcraft-cache?name=$CHARM_NAME&bases=$CHARMCRAFT_BASE"
CHARMCRAFT_CACHE_KEY="$CHARMCRAFT_CACHE_KEY_BASE&date=$(date +%Y-%m-%d)"
echo 'CHARMCRAFT_CACHE_ALT_KEYS<<EOF' >> $GITHUB_ENV
for d in {1..2}
do CHARMCRAFT_CACHE_ALT_KEYS="$CHARMCRAFT_CACHE_KEY_BASE&date=$(date -d"-$d days" +%Y-%m-%d)"
done
echo 'EOF' >> $GITHUB_ENV
echo "CHARMCRAFT_CACHE_KEY=$CHARMCRAFT_CACHE_KEY" >> $GITHUB_ENV
- name: Setup lxd
run: |
sudo groupadd --force --system lxd
sudo usermod --append --groups lxd runner
sudo snap refresh lxd --channel latest/stable
sudo lxd init --auto
sudo iptables -P FORWARD ACCEPT
- name: Build Charm
run: |
sudo snap install charmcraft --classic
sudo charmcraft pack
- name: Generate charmcraft container cache
run: |
mkdir -p ~/.charmcraft-cache
INODE_NUM=$(ls -id . | cut -f 1 -d " ")
for CONTAINER in $(sudo lxc list --project charmcraft -f table | grep charmcraft | cut -f 2 -d " ")
do sudo lxc --project charmcraft export $CONTAINER --compression none ~/.charmcraft-cache/$(echo $CONTAINER | sed s/$INODE_NUM/INODE/g).tar
done
- name: Delete charmcraft container cache
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/actions/caches?key=$(printf %s "${{ env.CHARMCRAFT_CACHE_KEY }}"|jq -sRr @uri) || :
for key in $(echo "${{ env.CHARMCRAFT_CACHE_ALT_KEYS }}")
do gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/actions/caches?key=$(printf %s "$key"|jq -sRr @uri) || :
done
- name: Save charmcraft container cache
uses: actions/cache/[email protected]
with:
path: ~/.charmcraft-cache/
key: ${{ env.CHARMCRAFT_CACHE_KEY }}
restore-cache:
needs: [ save-cache ]
runs-on: ubuntu-latest
strategy:
matrix:
charm: [ indico-operator ]
steps:
- uses: actions/checkout@master
with:
repository: canonical/${{ matrix.charm }}
- uses: actions/checkout@master
with:
repository: canonical/indico-operator
- name: Generate charmcraft cache key
run: |
CHARM_NAME=$(yq .name ./metadata.yaml)
CHARMCRAFT_BASE="$(yq -o=j -I=0 '.bases | sort_keys(..)' ./charmcraft.yaml)"
CHARMCRAFT_BASE=$(printf %s "$CHARMCRAFT_BASE"|jq -sRr @uri)
CHARMCRAFT_CACHE_KEY_BASE="charmcraft-cache?name=$CHARM_NAME&bases=$CHARMCRAFT_BASE"
CHARMCRAFT_CACHE_KEY="$CHARMCRAFT_CACHE_KEY_BASE&date=$(date +%Y-%m-%d)"
echo 'CHARMCRAFT_CACHE_ALT_KEYS<<EOF' >> $GITHUB_ENV
for d in {1..2}
do echo "$CHARMCRAFT_CACHE_KEY_BASE&date=$(date -d"-$d days" +%Y-%m-%d)" >> $GITHUB_ENV
done
echo 'EOF' >> $GITHUB_ENV
echo "CHARMCRAFT_CACHE_KEY=$CHARMCRAFT_CACHE_KEY" >> $GITHUB_ENV
- name: Restore charmcraft container cache
uses: actions/cache/[email protected]
id: charmcraft-cache
with:
path: ~/.charmcraft-cache/
key: ${{ env.CHARMCRAFT_CACHE_KEY }}
restore-keys: ${{ env.CHARMCRAFT_CACHE_ALT_KEYS }}
- name: Setup lxd
if: steps.charmcraft-cache.outputs.cache-hit == 'true'
run: |
sudo groupadd --force --system lxd
sudo usermod --append --groups lxd runner
sudo snap refresh lxd --channel latest/stable
sudo lxd init --auto
sudo iptables -P FORWARD ACCEPT
- name: Import charmcraft container cache
if: steps.charmcraft-cache.outputs.cache-hit == 'true'
run: |
sudo lxc project create charmcraft -c features.images=false -c features.profiles=false
INODE_NUM=$(ls -id . | cut -f 1 -d " ")
for FILE in $(ls ~/.charmcraft-cache)
do sudo lxc --project charmcraft import ~/.charmcraft-cache/$FILE $(echo $FILE | sed s/INODE/$INODE_NUM/g | sed s/.tar//g)
done
- name: Build Charm
run: |
sudo snap install charmcraft --classic
sudo charmcraft pack