Skip to content

Build and Push Container images #876

Build and Push Container images

Build and Push Container images #876

name: Build and Push Container images
env:
IMAGE_BASE_NAME: "quay.io/quarkus-super-heroes"
MANDREL_IMAGE: "quay.io/quarkus/ubi-quarkus-mandrel-builder-image"
LATEST_IMAGE_TAG: "latest"
DEFAULT_TIMEFRAME: "24 hours"
on:
# workflow_run:
# workflows:
# - "Basic build and test"
# branches:
# - main
# types:
# - completed
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
concurrency:
group: "workflow = ${{ github.workflow }}, ref = ${{ github.event.workflow_run.head_branch || github.event.ref || github.ref }}"
cancel-in-progress: false
defaults:
run:
shell: bash
jobs:
calculate-refs:
runs-on: ubuntu-latest
outputs:
ref: ${{ steps.calculate_branch.outputs.ref}}
branch: ${{ steps.calculate_branch.outputs.branch}}
steps:
- name: Calculate Branch
id: calculate_branch
run: |
if [[ ${{ github.event_name }} == 'workflow_run' ]]; then
echo "ref=${{ github.event.workflow_run.head_commit.id }}" >> $GITHUB_OUTPUT
echo "branch=${{ github.event.workflow_run.head_branch }}" >> $GITHUB_OUTPUT
elif [[ ${{ github.event_name}} == 'workflow_dispatch' ]]; then
echo "ref=${{ github.sha }}" >> $GITHUB_OUTPUT
echo "branch=${{ github.ref_name }}" >> $GITHUB_OUTPUT
elif [[ ${{ github.event_name }} == 'schedule' ]]; then
echo "ref=${{ github.sha }}" >> $GITHUB_OUTPUT
echo "branch=${{ github.ref_name }}" >> $GITHUB_OUTPUT
fi
check-commit-count:
needs: calculate-refs
runs-on: ubuntu-latest
outputs:
has_commits: ${{ steps.calculate_commits.outputs.has_commits}}
ref: ${{ needs.calculate-refs.outputs.ref}}
branch: ${{ needs.calculate-refs.outputs.branch}}
steps:
- name: Print inputs
run: |
echo "ref = ${{ needs.calculate-refs.outputs.ref }}"
echo "branch = ${{ needs.calculate-refs.outputs.branch }}"
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ needs.calculate-refs.outputs.branch }}
- name: Calculate whether there are commits
id: calculate_commits
run: |
if [[ ${{ github.event_name }} == 'schedule' ]]; then
if [[ $(git log --oneline --since '${{ env.DEFAULT_TIMEFRAME }} ago' | wc -l) -gt 0 ]]; then
echo "has_commits=true" >> $GITHUB_OUTPUT
else
echo "has_commits=false" >> $GITHUB_OUTPUT
fi
else
echo "has_commits=true" >> $GITHUB_OUTPUT
fi
calculate-modules:
needs: check-commit-count
runs-on: ubuntu-latest
if: needs.check-commit-count.outputs.has_commits == 'true'
outputs:
projects_matrix: ${{ steps.calculate_projects.outputs.projects_matrix }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ needs.check-commit-count.outputs.branch }}
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: 21
distribution: temurin
- name: Calculate projects
id: calculate_projects
run: |
json=$(curl -Ls https://sh.jbang.dev | bash -s - .github/calculateModules.java "${{ env.DEFAULT_TIMEFRAME }}")
echo "modules = ${json}"
echo "projects_matrix=${json}" >> $GITHUB_OUTPUT
build-jvm-images:
needs:
- check-commit-count
- calculate-modules
if: (needs.check-commit-count.outputs.has_commits == 'true') && (needs.calculate-modules.outputs.projects_matrix != '[]') && (github.repository == 'quarkusio/quarkus-super-heroes') && (contains(fromJSON('["workflow_dispatch", "schedule"]'), github.event_name) || ((github.event_name == 'worflow_run') && ((github.event.workflow_run.event == 'push') || (github.event.workflow_run.event == 'workflow_dispatch') && (github.event.workflow_run.conclusion == 'success') && (github.event.workflow_run.head_branch == 'main'))))
runs-on: ubuntu-latest
strategy:
matrix:
java:
- '17'
project: ${{ fromJson(needs.calculate-modules.outputs.projects_matrix) }}
arch:
- amd64
- arm64
name: "Build JVM images (${{ matrix.arch }}-${{ matrix.project.name }}-java${{ matrix.java }}-${{ matrix.project.openai-type }})"
steps:
- name: Checkout from ${{ needs.check-commit-count.outputs.ref }}
uses: actions/checkout@v4
with:
ref: ${{ needs.check-commit-count.outputs.ref }}
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java }}
distribution: temurin
cache: maven
- name: Create env vars
working-directory: ${{ matrix.project.name }}
run: |
if [[ ${{ matrix.java }} == '17' ]]; then
echo "JVM_DOCKERFILE=Dockerfile.jvm" >> "$GITHUB_ENV"
else
echo "JVM_DOCKERFILE=Dockerfile.jvm${{ matrix.java }}" >> "$GITHUB_ENV"
fi
- name: Create OpenAI env vars
if: matrix.project.openai-type
working-directory: ${{ matrix.project.name }}
run: echo "QUARKUS_PROFILE=${{ matrix.project.openai-type }}" >> $GITHUB_ENV
- name: Create CONTAINER_TAG
working-directory: .github/workflows/scripts
run: echo "CONTAINER_TAG=$(./compute-container-tag.sh "" ${{ matrix.java }} ${{ env.LATEST_IMAGE_TAG }} ${{ needs.check-commit-count.outputs.branch }} "${{ matrix.project.openai-type }}")" >> $GITHUB_ENV
- name: Set up QEMU
if: matrix.arch == 'arm64'
uses: docker/setup-qemu-action@v3
with:
platforms: ${{ matrix.arch }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Build JVM image (${{ matrix.project.name }}-java${{ matrix.java }}-${{ matrix.arch }}-${{ matrix.project.openai-type }})
working-directory: ${{ matrix.project.name }}
run: |
./mvnw -B clean package -DskipTests \
-Dmaven.compiler.release=${{ matrix.java }} \
-Dquarkus.http.host=0.0.0.0 \
-Dquarkus.container-image.build=true \
-Dquarkus.container-image.push=false \
-Dquarkus.container-image.tag=${{ env.CONTAINER_TAG }}-${{ matrix.arch }} \
-Dquarkus.docker.dockerfile-jvm-path=src/main/docker/${{ env.JVM_DOCKERFILE }} \
-Dquarkus.docker.buildx.platform=linux/${{ matrix.arch }}
- name: Save JVM Image (${{ matrix.project.name }}-java${{ matrix.java }}-${{ matrix.arch }}-${{ matrix.project.openai-type }})
uses: ishworkh/[email protected]
with:
image: "${{ env.IMAGE_BASE_NAME }}/${{ matrix.project.name }}:${{ env.CONTAINER_TAG }}-${{ matrix.arch }}"
build-native-images:
needs:
- check-commit-count
- calculate-modules
if: (needs.check-commit-count.outputs.has_commits == 'true') && (needs.calculate-modules.outputs.projects_matrix != '[]') && (github.repository == 'quarkusio/quarkus-super-heroes') && (contains(fromJSON('["workflow_dispatch", "schedule"]'), github.event_name) || ((github.event_name == 'worflow_run') && ((github.event.workflow_run.event == 'push') || (github.event.workflow_run.event == 'workflow_dispatch') && (github.event.workflow_run.conclusion == 'success') && (github.event.workflow_run.head_branch == 'main'))))
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
java:
- { jvm: '17', mandrel: '21' }
project: ${{ fromJson(needs.calculate-modules.outputs.projects_matrix) }}
arch:
- amd64
- arm64
name: "Build Native images (${{ matrix.arch }}-${{ matrix.project.name }}-java${{ matrix.java.jvm }}-${{ matrix.project.openai-type }})"
steps:
- name: Checkout from ${{ needs.check-commit-count.outputs.ref }}
uses: actions/checkout@v4
with:
ref: ${{ needs.check-commit-count.outputs.ref }}
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.java.jvm }}
distribution: temurin
cache: maven
- name: Create OpenAI env vars
if: matrix.project.openai-type
working-directory: ${{ matrix.project.name }}
run: echo "QUARKUS_PROFILE=${{ matrix.project.openai-type }}" >> $GITHUB_ENV
- name: Create CONTAINER_TAG
working-directory: .github/workflows/scripts
run: echo "CONTAINER_TAG=$(./compute-container-tag.sh "native-" ${{ matrix.java.jvm }} ${{ env.LATEST_IMAGE_TAG }} ${{ needs.check-commit-count.outputs.branch }} "${{ matrix.project.openai-type }}")" >> $GITHUB_ENV
- name: Set up QEMU
if: matrix.arch == 'arm64'
uses: docker/setup-qemu-action@v3
with:
platforms: ${{ matrix.arch }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
install: true
- name: Build native image (${{ matrix.project.name }}-java${{ matrix.java.jvm }}-${{ matrix.arch }}-${{ matrix.project.openai-type }})
working-directory: ${{ matrix.project.name }}
run: |
./mvnw -B clean package -DskipTests -Pnative \
-Dmaven.compiler.release=${{ matrix.java.jvm }} \
-Dquarkus.http.host=0.0.0.0 \
-Dquarkus.native.container-build=true \
-Dquarkus.native.builder-image=${{ env.MANDREL_IMAGE }}:jdk-${{ matrix.java.mandrel }} \
-Dquarkus.native.container-runtime-options=--platform=linux/${{ matrix.arch }} \
-Dquarkus.container-image.build=true \
-Dquarkus.container-image.push=false \
-Dquarkus.container-image.tag=${{ env.CONTAINER_TAG }}-${{ matrix.arch }} \
-Dquarkus.docker.buildx.platform=linux/${{ matrix.arch }}
- name: Save native Image (${{ matrix.project.name }}-java${{ matrix.java.jvm }}-${{ matrix.arch }}-${{ matrix.project.openai-type }})
uses: ishworkh/[email protected]
with:
image: "${{ env.IMAGE_BASE_NAME }}/${{ matrix.project.name }}:${{ env.CONTAINER_TAG }}-${{ matrix.arch }}"
push-app-images:
if: (needs.check-commit-count.outputs.has_commits == 'true') && (needs.calculate-modules.outputs.projects_matrix != '[]') && (github.repository == 'quarkusio/quarkus-super-heroes') && (contains(fromJSON('["workflow_dispatch", "schedule"]'), github.event_name) || ((github.event_name == 'worflow_run') && ((github.event.workflow_run.event == 'push') || (github.event.workflow_run.event == 'workflow_dispatch') && (github.event.workflow_run.conclusion == 'success') && (github.event.workflow_run.head_branch == 'main'))))
needs:
- check-commit-count
- calculate-modules
- build-jvm-images
- build-native-images
runs-on: ubuntu-latest
strategy:
matrix:
java:
- '17'
kind:
- ""
- "native-"
project: ${{ fromJson(needs.calculate-modules.outputs.projects_matrix) }}
arch:
- amd64
- arm64
name: "Push app images (${{ matrix.arch }}-${{ matrix.project.name }}-${{ matrix.kind }}java${{ matrix.java }}-${{ matrix.project.openai-type }})"
steps:
- name: Checkout from ${{ needs.check-commit-count.outputs.ref }}
uses: actions/checkout@v4
with:
ref: ${{ needs.check-commit-count.outputs.ref }}
- name: Create CONTAINER_TAG
working-directory: .github/workflows/scripts
run: echo "CONTAINER_TAG=$(./compute-container-tag.sh "${{ matrix.kind }}" ${{ matrix.java }} ${{ env.LATEST_IMAGE_TAG }} ${{ needs.check-commit-count.outputs.branch }} "${{ matrix.project.openai-type }}")" >> $GITHUB_ENV
- name: Get saved images (${{ matrix.project.name }}:${{ env.CONTAINER_TAG }}-${{ matrix.arch }})
uses: ishworkh/[email protected]
with:
image: "${{ env.IMAGE_BASE_NAME }}/${{ matrix.project.name }}:${{ env.CONTAINER_TAG }}-${{ matrix.arch }}"
- name: Login to quay
uses: docker/login-action@v3
with:
registry: quay.io
username: ${{ secrets.QUAY_REPO_USERNAME }}
password: ${{ secrets.QUAY_REPO_TOKEN }}
- name: Push images
working-directory: ${{ matrix.project.name }}
run: "docker push -a ${{ env.IMAGE_BASE_NAME }}/${{ matrix.project.name }}"
create-app-multiarch-manifests:
if: (needs.check-commit-count.outputs.has_commits == 'true') && (needs.calculate-modules.outputs.projects_matrix != '[]') && (github.repository == 'quarkusio/quarkus-super-heroes') && (contains(fromJSON('["workflow_dispatch", "schedule"]'), github.event_name) || ((github.event_name == 'worflow_run') && ((github.event.workflow_run.event == 'push') || (github.event.workflow_run.event == 'workflow_dispatch') && (github.event.workflow_run.conclusion == 'success') && (github.event.workflow_run.head_branch == 'main'))))
needs:
- check-commit-count
- calculate-modules
- push-app-images
runs-on: ubuntu-latest
strategy:
matrix:
java:
- '17'
kind:
- ""
- "native-"
project: ${{ fromJson(needs.calculate-modules.outputs.projects_matrix) }}
name: Create app multiarch manifests (${{ matrix.project.name }}-${{ matrix.kind }}java${{ matrix.java }}-${{ matrix.project.openai-type }})
steps:
- name: Checkout from ${{ needs.check-commit-count.outputs.ref }}
uses: actions/checkout@v4
with:
ref: ${{ needs.check-commit-count.outputs.ref }}
- name: Create CONTAINER_TAG
working-directory: .github/workflows/scripts
run: echo "CONTAINER_TAG=$(./compute-container-tag.sh "${{ matrix.kind }}" ${{ matrix.java }} ${{ env.LATEST_IMAGE_TAG }} ${{ needs.check-commit-count.outputs.branch }} "${{ matrix.project.openai-type }}")" >> $GITHUB_ENV
- name: Login to quay
uses: docker/login-action@v3
with:
registry: quay.io
username: ${{ secrets.QUAY_REPO_USERNAME }}
password: ${{ secrets.QUAY_REPO_TOKEN }}
- name: Create and push multi-arch manifests
run: |
docker manifest create ${{ env.IMAGE_BASE_NAME }}/${{ matrix.project.name }}:${{ env.CONTAINER_TAG }} \
-a ${{ env.IMAGE_BASE_NAME }}/${{ matrix.project.name }}:${{ env.CONTAINER_TAG }}-amd64 \
-a ${{ env.IMAGE_BASE_NAME }}/${{ matrix.project.name }}:${{ env.CONTAINER_TAG }}-arm64
docker manifest push ${{ env.IMAGE_BASE_NAME }}/${{ matrix.project.name }}:${{ env.CONTAINER_TAG }}
deploy-resources:
if: (needs.check-commit-count.outputs.has_commits == 'true') && (github.repository == 'quarkusio/quarkus-super-heroes') && (contains(fromJSON('["workflow_dispatch", "schedule"]'), github.event_name) || ((github.event_name == 'worflow_run') && ((github.event.workflow_run.event == 'push') || (github.event.workflow_run.event == 'workflow_dispatch') && (github.event.workflow_run.conclusion == 'success') && (github.event.workflow_run.head_branch == 'main'))))
needs:
- check-commit-count
- create-app-multiarch-manifests
uses: quarkusio/quarkus-super-heroes/.github/workflows/create-deploy-resources.yml@main
secrets: inherit
with:
commit_id: ${{ needs.check-commit-count.outputs.ref }}
branch: ${{ needs.check-commit-count.outputs.branch }}
cleanup-artifacts:
needs: deploy-resources
if: always()
runs-on: ubuntu-latest
steps:
- name: Delete artifacts
env:
WEBHOOK_SECRET: ${{ secrets.WEBHOOK_SECRET }}
run: |
echo "::add-mask::$WEBHOOK_SECRET"
curl --verbose --fail --show-error --location --request POST "https://api.github.com/repos/$GITHUB_REPOSITORY/dispatches" --header "Authorization: token $WEBHOOK_SECRET" --header 'Content-Type: application/json' --header 'Accept: application/vnd.github.everest-preview+json' --data-raw "{ \"event_type\": \"delete_all_artifacts\", \"client_payload\": {\"parent_runid\": \"$GITHUB_RUN_ID\", \"parent_repo\": \"$GITHUB_REPOSITORY\"} }"