build(github): 👷 combine build and test workflows… #178
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: Build a container | |
env: | |
REGISTRY: ghcr.io | |
REGISTRY_USER: ${{ github.actor }} | |
REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} | |
IMAGE: ${{ github.repository }} | |
on: | |
release: | |
types: [ created ] | |
push: | |
branches: [ main ] | |
permissions: | |
contents: read | |
packages: write | |
jobs: | |
build-push-image: | |
runs-on: ubuntu-20.04 | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Install/Cache dependencies | |
uses: awalsh128/cache-apt-pkgs-action@latest | |
with: | |
packages: qemu-user-static | |
- name: Get Docker metadata | |
id: docker_metadata | |
uses: docker/metadata-action@v5 | |
with: | |
images: | | |
${{ env.REGISTRY }}/${{ env.IMAGE }} | |
tags: | | |
type=raw,value=latest | |
type=edge | |
type=sha | |
type=ref,event=branch | |
type=ref,event=pr | |
type=schedule | |
type=semver,pattern={{version}} | |
type=semver,pattern={{major}}.{{minor}} | |
type=semver,pattern={{major}},enable=${{ !startsWith(github.ref, 'refs/tags/v0.') }} | |
- uses: cardinalby/git-get-release-action@v1 | |
id: release_metadata | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
latest: true | |
- name: Build container image | |
id: build_image | |
uses: redhat-actions/buildah-build@v2 | |
with: | |
archs: amd64 | |
# context: build/package | |
image: ${{ env.IMAGE }} | |
tags: ${{ steps.docker_metadata.outputs.tags }} ${{ env.REGISTRY }}/${{ env.IMAGE }}:${{ steps.release_metadata.outputs.name }} | |
labels: ${{ steps.docker_metadata.outputs.labels }} | |
containerfiles: Dockerfile | |
layers: false | |
oci: true | |
- name: Push to Container Registry | |
id: push_image | |
uses: redhat-actions/push-to-registry@v2 | |
if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
with: | |
username: ${{ env.REGISTRY_USER }} | |
password: ${{ env.REGISTRY_PASSWORD }} | |
tags: ${{ steps.build_image.outputs.tags }} | |
extra-args: | | |
--disable-content-trust | |
- name: Login to Container Registry | |
uses: redhat-actions/podman-login@v1 | |
if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ env.REGISTRY_USER }} | |
password: ${{ env.REGISTRY_PASSWORD }} | |
- name: Check and install cosign | |
uses: sigstore/[email protected] | |
if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
with: | |
cosign-release: 'v2.2.2' | |
- name: Sign image with a key | |
if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
env: | |
DIGEST: ${{ steps.push_image.outputs.digest }} | |
TAGS: ${{ steps.docker_metadata.outputs.tags }} ${{ env.REGISTRY }}/${{ env.IMAGE }}:${{ steps.release_metadata.outputs.name }} | |
COSIGN_PASSWORD: ${{ secrets.COSIGN_PWD }} | |
run: | | |
images="" | |
for tag in ${TAGS}; do | |
images+="${tag}@${DIGEST} " | |
done | |
cosign --verbose=true sign --yes --key cosign.key \ | |
-a "repo=${{ github.repository }}" \ | |
-a "ref=${{ github.ref }}" \ | |
${images} |