Skip to content

Custom Image Build and Release #266

Custom Image Build and Release

Custom Image Build and Release #266

name: Custom Image Build and Release
on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
env:
URL_TO_OS_IMAGE: https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
IMAGE_FILE: ubuntu-custom-cloudimg.img
jobs:
# https://docs.github.com/ja/rest/releases/releases?apiVersion=2022-11-28#delete-a-release
# https://docs.github.com/ja/rest/overview/permissions-required-for-fine-grained-personal-access-tokens?apiVersion=2022-11-28#repository-permissions-for-contents
delete_release:
runs-on: ubuntu-latest
steps:
- name: Get Release ID and Delete Release
run: |
json_data=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.CONTENTS_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/mu-ruU1/docker-kubernetes-tutorial/releases/tags/v1)
if [ $? -ne 0 ]; then
echo "Failed to get JSON data from API"
exit 1
fi
url=$(echo "$json_data" | jq -r '.url')
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.CONTENTS_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$url"
build_and_release:
needs: delete_release
runs-on: ubuntu-latest
steps:
- name: Download OS Image
run: |
curl -o ${{ env.IMAGE_FILE }} ${{ env.URL_TO_OS_IMAGE }} -L
- name: Install libguestfs-tools
run: |
sudo apt-get update
sudo apt-get install -y libguestfs-tools
- name: Customize Image
run: |
sudo virt-customize \
-a ${{ env.IMAGE_FILE }} \
--install qemu-guest-agent,avahi-daemon \
--update \
--timezone Asia/Tokyo \
- name: Calculation SHA-256 Hash
run: |
HASH=$(sha256sum ${{ env.IMAGE_FILE }} | awk '{print $1}')
echo "HASH=$HASH" >> $GITHUB_ENV
- name: Create Release
id: create_release
uses: actions/create-release@v1.1.4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v1
release_name: Ubuntu Custom Image
body: |
## Customized Ubuntu Image
|Filename|sha256 hash|
|:--:|:--:|
|[ubuntu-custom-cloudimg.img](https://github.com/mu-ruU1/docker-kubernetes-tutorial/releases/download/v1/ubuntu-custom-cloudimg.img)|${{ env.HASH }}|
- Downloaded from ${{ env.URL_TO_OS_IMAGE }}
- Customized by virt-customize
- Installed qemu-guest-agent, avahi-daemon
- Set timezone to Asia/Tokyo
- Enabled password authentication for SSH
draft: false
prerelease: false
- name: Upload Artifact to Release
id: upload-release-asset
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./${{ env.IMAGE_FILE }}
asset_name: ${{ env.IMAGE_FILE }}
asset_content_type: application/octet-stream