Skip to content

Commit

Permalink
Add workflow and customize scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ecanuto committed Jun 18, 2024
1 parent b703e29 commit ab26adb
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
113 changes: 113 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
name: Release

on:
workflow_dispatch:
push:
tags:
- v*

permissions:
contents: write

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# these are overwritten by "settings.sh"
DEBIAN_VERSION: ""
DEBIAN_VERNAME: ""
DEBIAN_RELEASE: ""
DEBIAN_FILEURL: ""
GIT_CACHE_NAME: ""
GITHUB_RELEASE: ""
SOURCE_IMGFILE: ""
TARGET_IMGFILE: ""

jobs:
release:
name: Create release
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Load settings
run: source settings.sh

- name: Create a new GitHub Release
if: startsWith(github.ref, 'refs/tags/')
run: |
# Create a new GitHub Release if it does not exists
gh release list | grep "${{ github.ref_name }}" ||
gh release create "${{ github.ref_name }}" \
--repo="${{ github.repository }}" \
--title="${{ github.ref_name }}" \
--notes="Debian ${{ env.DEBIAN_VERSION }} \"${{ env.DEBIAN_VERNAME }}\" ${{ env.DEBIAN_RELEASE }}" \
--verify-tag \
--latest
download:
name: Download base image
needs: release
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Load settings
run: source settings.sh

- id: cache
name: Cache
uses: actions/cache@v4
with:
key: ${{ env.GIT_CACHE_NAME }}
path: ${{ env.SOURCE_IMGFILE }}

- name: Download base image
run: wget "${{ env.DEBIAN_FILEURL }}" -O ${{ env.SOURCE_IMGFILE }}
if: steps.cache.outputs.cache-hit != 'true'

- name: Upload original image artifact
run: gh release upload "${{ env.GITHUB_RELEASE }}" ${{ env.SOURCE_IMGFILE }} --clobber

customize:
name: Customize image
needs: download
runs-on: ubuntu-latest

strategy:
matrix:
variant: [qemu]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Load settings
run: source settings.sh ${{ matrix.variant }}

- name: Load cached files
uses: actions/cache@v4
with:
key: ${{ env.GIT_CACHE_NAME }}
path: ${{ env.SOURCE_IMGFILE }}

- name: Install libguestfs
run: sudo apt-get install -y libguestfs-tools

- name: Rebuild image
run: |
sudo virt-customize -a "${{ env.SOURCE_IMGFILE }}" \
--run "customize-${{ matrix.variant }}.sh" \
--run-command "apt-get --yes autoremove" \
--run-command "apt-get --yes clean autoclean" \
--run-command "rm -rf /var/lib/apt/lists/*" \
--no-logfile
- name: Compress image artifact
run: sudo virt-sparsify ${{ env.SOURCE_IMGFILE }} --compress ${{ env.TARGET_IMGFILE }}

- name: Upload custom image to a GitHub Release
run: gh release upload "${{ env.GITHUB_RELEASE }}" ${{ env.TARGET_IMGFILE }} --clobber
5 changes: 5 additions & 0 deletions customize-qemu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

# Install packages
apt-get --yes update
apt-get --yes install qemu-guest-agent
37 changes: 37 additions & 0 deletions settings.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

# Debian
DEBIAN_VERSION="12"
DEBIAN_VERNAME="bookworm"
DEBIAN_RELEASE="20240415-1718"
DEBIAN_VARIANT="genericcloud-amd64"
DEBIAN_BASEURL="https://cloud.debian.org/images/cloud"
DEBIAN_FILEURL="$DEBIAN_BASEURL/$DEBIAN_VERNAME/$DEBIAN_RELEASE/debian-${DEBIAN_VERSION}-${DEBIAN_VARIANT}-${DEBIAN_RELEASE}.qcow2"

# Image filename
SOURCE_IMGFILE="$(basename $DEBIAN_FILEURL)"
TARGET_IMGFILE="${SOURCE_IMGFILE/genericcloud/$1}"

# GitHub cache
GIT_CACHE_NAME="debian-$DEBIAN_VERSION-$DEBIAN_VARIANT-$DEBIAN_RELEASE"

# GitHub release name
if [[ "${GITHUB_REF_NAME}" == "refs/tags/*" ]]; then
GITHUB_RELEASE="${GITHUB_REF_NAME}"
else
GITHUB_RELEASE="v${DEBIAN_VERSION}-${DEBIAN_RELEASE}"
fi

# Export variables to github workflow
if [[ -f "$GITHUB_ENV" ]]; then
cat >> "$GITHUB_ENV" <<-EOF
DEBIAN_VERSION=$DEBIAN_VERSION
DEBIAN_VERNAME=$DEBIAN_VERNAME
DEBIAN_RELEASE=$DEBIAN_RELEASE
DEBIAN_FILEURL=$DEBIAN_FILEURL
GIT_CACHE_NAME=$GIT_CACHE_NAME
GITHUB_RELEASE=$GITHUB_RELEASE
SOURCE_IMGFILE=$SOURCE_IMGFILE
TARGET_IMGFILE=$TARGET_IMGFILE
EOF
fi

0 comments on commit ab26adb

Please sign in to comment.