refactor(everything)!: universal repo a. github actions #43
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
# SPDX-FileCopyrightText: 2023 Frederik Zorn <federdaemn@mail.de> | |
# | |
# SPDX-License-Identifier: Apache-2.0 | |
name: 'Build and push container' | |
# run workflow on every branch, only upload on main (allow manual trigger) | |
on: [push, pull_request, workflow_dispatch] | |
jobs: | |
build-push: | |
# only push branch main to registry | |
name: 'Build and push image' | |
# use ubuntu as runner | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
# build for every application | |
app: | |
- anki-sync-server | |
permissions: | |
packages: write | |
steps: | |
- name: Checkout the repository | |
uses: actions/checkout@v4 | |
- name: Setup yq | |
uses: mikefarah/yq@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Setup oras | |
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' | |
uses: oras-project/setup-oras@v1 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' | |
with: | |
registry: ghcr.io | |
username: federdaemn | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Login to DockerHub | |
uses: docker/login-action@v3 | |
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' | |
with: | |
username: federdaemn | |
password: ${{ secrets.DOCKER_TOKEN }} | |
- name: Set outputs for later use | |
id: set-op | |
run: | | |
# extract version from ./values.yml | |
echo "software-version=$(yq '.version.software-version' ./${{ matrix.app }}/values.yml)" \ | |
>> $GITHUB_OUTPUT | |
# get current date+time | |
echo "time=$(date --utc +'%FT%TZ')" >> $GITHUB_OUTPUT | |
# get current date+time docker tag compatible | |
echo "time-docker=$(date --utc +'%Y-%m-%dt%H-%M-%Sz')" >> $GITHUB_OUTPUT | |
- name: Build and push image in production | |
uses: docker/build-push-action@v5 | |
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' | |
with: | |
# directly push image | |
push: true | |
# use local repository | |
context: . | |
# which containerfile to build from | |
file: | | |
./${{ matrix.app }}/containerfile | |
# build args to pass to the build | |
build-args: | | |
software_version=${{ steps.set-op.outputs.software-version }} | |
# build for many platforms at the same time | |
platforms: | | |
linux/amd64 | |
linux/arm64 | |
linux/arm/v7 | |
linux/arm/v6 | |
# set general labels to append to all images | |
labels: | | |
# opencontainer labels specified | |
# https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys | |
org.opencontainers.image.title="${{ matrix.app }}" | |
org.opencontainers.image.authors=Frederik Zorn <federdaemn@mail.de> | |
org.opencontainers.image.created=${{ steps.set-op.outputs.time }} | |
org.opencontainers.image.version=${{ steps.set-op.outputs.software-version }} | |
org.opencontainers.image.source=https://github.com/federdaemn/container | |
# artfifacthub labels specified | |
# https://artifacthub.io/docs/topics/repositories/container-images/#image-metadata | |
io.artifacthub.package.readme-url=https://raw.githubusercontent.com/federdaemn/container/main/${{ matrix.app }}/README.md | |
io.artifacthub.package.maintainers=[{"name":"federdaemn","email":"federdaemn@mail.de"}] | |
io.artifacthub.package.logo-url=https://raw.githubusercontent.com/federdaemn/container/main/${{ matrix.app }}/imgs/ah-logo.png | |
io.artifacthub.package.alternative-locations=docker.io/federdaemn/${{ matrix.app }} | |
# tags to build the image with | |
tags: | | |
ghcr.io/federdaemn/${{ matrix.app }}:latest | |
ghcr.io/federdaemn/${{ matrix.app }}:${{ steps.set-op.outputs.software-version }} | |
ghcr.io/federdaemn/${{ matrix.app }}:${{ steps.set-op.outputs.time-docker }} | |
docker.io/federdaemn/${{ matrix.app }}:latest | |
docker.io/federdaemn/${{ matrix.app }}:${{ steps.set-op.outputs.software-version }} | |
docker.io/federdaemn/${{ matrix.app }}:${{ steps.set-op.outputs.time-docker }} | |
- name: Build image in testing only | |
uses: docker/build-push-action@v5 | |
if: github.event_name == 'pull_request' && github.ref != 'refs/heads/main' | |
with: | |
# do not push image | |
push: false | |
# use local repository | |
context: . | |
# which containerfile to build from | |
file: | | |
./${{ matrix.app }}/containerfile | |
# build args to pass to the build | |
build-args: | | |
software_version=${{ steps.set-op.outputs.software-version }} | |
# build for many platforms at the same time | |
platforms: | | |
linux/amd64 | |
linux/arm64 | |
linux/arm/v7 | |
linux/arm/v6 | |
# use oras to push artifacthub metadata to registries | |
# source: https://artifacthub.io/docs/topics/repositories/container-images/#repository-metadata | |
- name: Push metadata | |
if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' | |
run: | | |
oras push \ | |
ghcr.io/federdaemn/${{ matrix.app }}:artifacthub.io \ | |
--config /dev/null:application/vnd.cncf.artifacthub.config.v1+yaml \ | |
${{ matrix.app }}/artifacthub-repo.yml:application/vnd.cncf.artifacthub.repository-metadata.layer.v1.yaml | |
oras push \ | |
docker.io/federdaemn/${{ matrix.app }}:artifacthub.io \ | |
--config /dev/null:application/vnd.cncf.artifacthub.config.v1+yaml \ | |
${{ matrix.app }}/artifacthub-repo.yml:application/vnd.cncf.artifacthub.repository-metadata.layer.v1.yaml |