Saltar al contenido
mypipelines
Pipelines Actions Gradle Buscar
Java (Spring Boot)· Reusable workflow ·on: workflow_call

Java Artifact Docker Github

Java - Docker Artifact to GitHub Registry

.github/workflows/java-artifact-docker-github.yml

.github/workflows/java-artifact-docker-github.yml
name: Java - Docker Artifact to GitHub Registry
on:
workflow_call:
inputs:
runner:
description: 'Runner type'
required: false
type: string
default: 'ubuntu-latest'
java_version:
description: 'Java version'
required: false
type: string
default: '21'
java_distribution:
description: 'Java distribution'
required: false
type: string
default: 'temurin'
image_tag:
description: 'Docker image tag. If empty, uses short commit SHA (7 chars)'
required: false
type: string
default: ''
push_latest:
description: 'Also push :latest tag'
required: false
type: boolean
default: false
dockerfile_path:
description: 'Path to Dockerfile'
required: false
type: string
default: '.'
docker_platform:
description: 'Docker platform (linux/amd64, linux/arm64, linux/amd64,linux/arm64)'
required: false
type: string
default: 'linux/arm64'
artifact_name:
description: 'Name of pre-built JAR artifact to download. If empty, builds from source.'
required: false
type: string
default: ''
jar_path:
description: 'Path where JAR files are expected by the Dockerfile'
required: false
type: string
default: 'bootstrap/build/libs'
environment:
description: 'GitHub environment (develop, prod)'
required: false
type: string
default: 'develop'
outputs:
image_tag:
description: 'Docker image tag pushed'
value: ${{ jobs.artifact.outputs.image_tag }}
image_uri:
description: 'Full Docker image URI'
value: ${{ jobs.artifact.outputs.image_uri }}
commit_sha:
description: 'Short commit SHA used'
value: ${{ jobs.artifact.outputs.commit_sha }}
jobs:
artifact:
name: Build and Push to GitHub Registry
runs-on: ${{ inputs.runner }}
environment: ${{ inputs.environment }}
timeout-minutes: 20
outputs:
image_tag: ${{ steps.set-tag.outputs.tag }}
image_uri: ${{ steps.push-ghcr.outputs.image_uri }}
commit_sha: ${{ steps.set-tag.outputs.commit_sha }}
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Determine image tag
id: set-tag
run: |
COMMIT_SHA="${{ github.sha }}"
SHORT_SHA="${COMMIT_SHA:0:7}"
if [ -n "${{ inputs.image_tag }}" ]; then
TAG="${{ inputs.image_tag }}"
else
TAG="$SHORT_SHA"
fi
echo "tag=$TAG" >> $GITHUB_OUTPUT
echo "commit_sha=$SHORT_SHA" >> $GITHUB_OUTPUT
echo "Using image tag: $TAG"
# Download pre-built JAR if artifact_name is provided
- name: Download build artifact
if: inputs.artifact_name != ''
uses: actions/download-artifact@v5
with:
name: ${{ inputs.artifact_name }}
path: ${{ inputs.jar_path }}
# Build from source if no artifact provided
- name: Setup JDK ${{ inputs.java_version }}
if: inputs.artifact_name == ''
uses: actions/setup-java@v5
with:
distribution: ${{ inputs.java_distribution }}
java-version: ${{ inputs.java_version }}
- name: Setup Gradle
if: inputs.artifact_name == ''
uses: gradle/actions/setup-gradle@v4
with:
cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/master' }}
- name: Build JAR
if: inputs.artifact_name == ''
env:
GH_PACKAGES_USERNAME: ${{ secrets.GH_PACKAGES_USERNAME }}
GH_PACKAGES_TOKEN: ${{ secrets.GH_PACKAGES_TOKEN }}
run: ./gradlew clean build -x test --no-daemon --build-cache --parallel
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push to GitHub Registry
id: push-ghcr
env:
GHCR_REGISTRY: ghcr.io/${{ github.repository_owner }}
GHCR_REPOSITORY: ${{ github.event.repository.name }}
IMAGE_TAG: ${{ steps.set-tag.outputs.tag }}
run: |
# GitHub Registry requires lowercase
GHCR_REGISTRY=$(echo "$GHCR_REGISTRY" | tr '[:upper:]' '[:lower:]')
GHCR_REPOSITORY=$(echo "$GHCR_REPOSITORY" | tr '[:upper:]' '[:lower:]')
IMAGE_URI="$GHCR_REGISTRY/$GHCR_REPOSITORY:$IMAGE_TAG"
TAGS="-t $IMAGE_URI"
if [ "${{ inputs.push_latest }}" = "true" ]; then
TAGS="$TAGS -t $GHCR_REGISTRY/$GHCR_REPOSITORY:latest"
fi
echo "Building and pushing Docker image: $IMAGE_URI"
echo "Platform: ${{ inputs.docker_platform }}"
docker buildx build \
--platform ${{ inputs.docker_platform }} \
--no-cache \
$TAGS \
--label "org.opencontainers.image.source=https://github.com/${{ github.repository }}" \
--label "org.opencontainers.image.revision=${{ github.sha }}" \
--label "org.opencontainers.image.created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
--label "org.opencontainers.image.title=$GHCR_REPOSITORY" \
--label "org.opencontainers.image.version=$IMAGE_TAG" \
--label "org.opencontainers.image.vendor=codehunters" \
--label "build.github.run-id=${{ github.run_id }}" \
--label "build.github.workflow=${{ github.workflow }}" \
--label "observability.service=$GHCR_REPOSITORY" \
--label "observability.environment=${{ inputs.environment }}" \
--label "observability.domain=${GHCR_REPOSITORY%%-*}" \
--push \
${{ inputs.dockerfile_path }}
echo "image_uri=$IMAGE_URI" >> $GITHUB_OUTPUT
echo "### Docker Image Pushed to GitHub Registry" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Property | Value |" >> $GITHUB_STEP_SUMMARY
echo "|----------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| **Registry** | $GHCR_REGISTRY |" >> $GITHUB_STEP_SUMMARY
echo "| **Repository** | $GHCR_REPOSITORY |" >> $GITHUB_STEP_SUMMARY
echo "| **Tag** | $IMAGE_TAG |" >> $GITHUB_STEP_SUMMARY
echo "| **Platform** | ${{ inputs.docker_platform }} |" >> $GITHUB_STEP_SUMMARY
echo "| **Full URI** | $IMAGE_URI |" >> $GITHUB_STEP_SUMMARY