Pack and Publish OCI Image to Docker Registry and GitHub Packages #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Pack and Publish OCI Image to Docker Registry and GitHub Packages | |
on: | |
push: | |
branches: | |
- main | |
- master | |
workflow_dispatch: | |
inputs: | |
workflow_choice: | |
description: "Choose Release Channel" | |
required: true | |
default: "edge" | |
type: choice | |
options: | |
- edge | |
- stable | |
- both | |
workflow_run: | |
workflows: ["Push new tag update to stable branch"] | |
types: | |
- completed | |
jobs: | |
build-rock: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Pack with Rockcraft | |
uses: canonical/craft-actions/rockcraft-pack@main | |
id: rockcraft | |
- name: Upload Rock Artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: cups-rock | |
path: ${{ steps.rockcraft.outputs.rock }} | |
publish-rock: | |
needs: build-rock | |
if: github.ref_name == 'main' || github.ref_name == 'master' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Download Rock Artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: cups-rock | |
- name: Install Dependencies | |
run: | | |
sudo snap install rockcraft --classic | |
sudo snap install docker | |
sudo snap install yq | |
- name: Ensure Docker Daemon is Running | |
run: | | |
sudo systemctl start docker | |
sudo systemctl enable docker | |
sudo systemctl is-active --quiet docker || sudo systemctl start docker | |
# - name: Log in to Docker Hub | |
# uses: docker/[email protected] | |
# with: | |
# username: ${{ secrets.DOCKER_USERNAME }} | |
# password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Log in to GitHub Packages | |
uses: docker/[email protected] | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and Push Docker Image (Edge & Latest Channel) | |
if: github.event.inputs.workflow_choice == 'edge' || github.event.inputs.workflow_choice == 'both' || github.event_name == 'push' || github.event_name == 'workflow_run' | |
env: | |
USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
ORG: ${{ github.repository_owner }} | |
run: | | |
IMAGE="$(yq '.name' rockcraft.yaml)" | |
VERSION="$(yq '.version' rockcraft.yaml)" | |
ROCK="$(ls *.rock | tail -n 1)" | |
ORG_NAME=$(echo "${ORG}" | tr '[:upper:]' '[:lower:]') | |
sudo rockcraft.skopeo --insecure-policy copy oci-archive:"${ROCK}" docker-daemon:"${ORG_NAME}/${IMAGE}:${VERSION}-edge" | |
# Push to Docker Hub | |
# docker tag ${ORG_NAME}/${IMAGE}:${VERSION}-edge ${USERNAME}:${VERSION}-edge | |
# docker push ${USERNAME}/${IMAGE}:${VERSION}-edge | |
# docker tag ${USERNAME}/${IMAGE}:${VERSION}-edge ${USERNAME}/${IMAGE}:latest | |
# docker push ${USERNAME}/${IMAGE}:latest | |
# Push to GitHub Packages | |
GITHUB_IMAGE="ghcr.io/${ORG_NAME}/${IMAGE}" | |
docker tag ${ORG_NAME}/${IMAGE}:${VERSION}-edge ${GITHUB_IMAGE}:${VERSION}-edge | |
docker push ${GITHUB_IMAGE}:${VERSION}-edge | |
docker tag ${GITHUB_IMAGE}:${VERSION}-edge ${GITHUB_IMAGE}:latest | |
docker push ${GITHUB_IMAGE}:latest | |
- name: Build and Push Docker Image (Stable Channel) | |
if: github.event.inputs.workflow_choice == 'stable' || github.event.inputs.workflow_choice == 'both' | |
env: | |
USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
ORG: ${{ github.repository_owner }} | |
run: | | |
IMAGE="$(yq '.name' rockcraft.yaml)" | |
VERSION="$(yq '.version' rockcraft.yaml)" | |
ROCK="$(ls *.rock | tail -n 1)" | |
ORG_NAME=$(echo "${ORG}" | tr '[:upper:]' '[:lower:]') | |
sudo rockcraft.skopeo --insecure-policy copy oci-archive:"${ROCK}" docker-daemon:"${ORG_NAME}/${IMAGE}:${VERSION}-stable" | |
# Push to Docker Hub | |
# docker tag ${ORG_NAME}/${IMAGE}:${VERSION}-stable ${USERNAME}:${VERSION}-stable | |
# docker push ${USERNAME}/${IMAGE}:${VERSION}-stable | |
# Push to GitHub Packages | |
GITHUB_IMAGE="ghcr.io/${ORG_NAME}/${IMAGE}" | |
docker tag ${ORG_NAME}/${IMAGE}:${VERSION}-stable ${GITHUB_IMAGE}:${VERSION}-stable | |
docker push ${GITHUB_IMAGE}:${VERSION}-stable |