Skip to content

docker image building and pushing to GitHub Packages #47

docker image building and pushing to GitHub Packages

docker image building and pushing to GitHub Packages #47

name: docker image building and pushing to GitHub Packages
on:
push:
branches:
- 'main'
schedule:
- cron: '30 4,16 * * *'
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
DOCKER_BUILDKIT: 1
GHCR_USERNAME: ${{ github.repository_owner }}
GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }}
jobs:
docker:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
packages: write
steps:
- name: Check if PR is from a fork
run: echo "IS_FORK=$(if [ '${{ github.event.pull_request.head.repo.full_name }}' != '${{ github.repository }}' ]; then echo 'true'; else echo 'false'; fi)" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'
cache: 'maven'
- name: Resolve Maven cache path
run: echo "MAVEN_CACHE_PATH=$(realpath ~/.m2/repository)" >> $GITHUB_ENV
- name: Cache Maven dependencies
uses: actions/cache@v3
with:
path: ${{ env.MAVEN_CACHE_PATH }}
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Build with Maven and install dependencies in the Docker context
run: mvn clean install && cp -vraxu ~/.m2 .m2
env:
MAVEN_CACHE: ${{ env.MAVEN_CACHE_PATH }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: push
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
MAVEN_CACHE=.m2
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
show-summary: true