Saltar al contenido
mypipelines
Java (Spring Boot)·Reusable workflow·on: workflow_call

Java Artifact Dependency Github

Java - Publish Dependency to GitHub Packages

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

.github/workflows/java-artifact-dependency-github.yml
name: Java - Publish Dependency to GitHub Packages
on:
workflow_call:
inputs:
runner:
description: 'Runner type'
required: false
type: string
default: 'ubuntu-latest'
container_image:
description: >-
Run the job inside this image (e.g.
ghcr.io/codehunters-io/ci-base-images:1.0.0). Empty = plain runner
with actions/setup-java. The image already carries the JDK, so the
per-run download is skipped and the version is pinned to the tag.
Gradle still comes from ./gradlew, and setup-gradle still runs for
its dependency cache.
required: false
type: string
default: ''
java_version:
description: 'Java version (ignored when container_image is set)'
required: false
type: string
default: '21'
java_distribution:
description: 'Java distribution'
required: false
type: string
default: 'temurin'
publish_task:
description: 'Gradle task to publish (e.g., publish, publishToGitHubPackages)'
required: false
type: string
default: 'publish'
outputs:
version:
description: 'Published artifact version'
value: ${{ jobs.publish.outputs.version }}
group:
description: 'Published artifact group'
value: ${{ jobs.publish.outputs.group }}
artifact_name:
description: 'Published artifact name'
value: ${{ jobs.publish.outputs.artifact_name }}
# Least privilege for what this workflow actually does. Without it the
# job inherits whatever the calling repository's default happens to be,
# which on older repositories is write-all.
permissions:
contents: read
packages: write
jobs:
publish:
name: Publish to GitHub Packages
runs-on: ${{ inputs.runner }}
# `container:` takes an object or nothing at all, and there is no way to
# write "no container" as a string. fromJSON('null') is how you say it.
container: ${{ inputs.container_image != '' && fromJSON(format('{{"image":"{0}"}}', inputs.container_image)) || fromJSON('null') }}
timeout-minutes: 15
outputs:
version: ${{ steps.info.outputs.version }}
group: ${{ steps.info.outputs.group }}
artifact_name: ${{ steps.info.outputs.artifact_name }}
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Setup JDK ${{ inputs.java_version }}
if: inputs.container_image == ''
uses: actions/setup-java@de7274f081f381c8f8158605e0321c36c376e2e6 # v6.0.1
with:
distribution: ${{ inputs.java_distribution }}
java-version: ${{ inputs.java_version }}
- name: Report toolchain (container)
if: inputs.container_image != ''
run: |
echo "Container: ${{ inputs.container_image }}"
java -version
- name: Setup Gradle
uses: gradle/actions/setup-gradle@9c971963bec38e04b3d30dcc455b5382be2fdbfb # v6.3.0
with:
cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/master' }}
- name: Get artifact info
id: info
run: |
VERSION=$(./gradlew properties --no-daemon -q | grep "^version:" | awk '{print $2}')
GROUP=$(./gradlew properties --no-daemon -q | grep "^group:" | awk '{print $2}')
NAME=$(./gradlew properties --no-daemon -q | grep "^name:" | awk '{print $2}')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "group=$GROUP" >> $GITHUB_OUTPUT
echo "artifact_name=$NAME" >> $GITHUB_OUTPUT
echo "Artifact: ${GROUP}:${NAME}:${VERSION}"
- name: Build
run: ./gradlew clean build -x test --no-daemon --build-cache --parallel
- name: Publish to GitHub Packages
env:
GH_PACKAGES_USERNAME: ${{ secrets.GH_PACKAGES_USERNAME }}
GH_PACKAGES_TOKEN: ${{ secrets.GH_PACKAGES_TOKEN }}
run: ./gradlew ${{ inputs.publish_task }} --no-daemon --build-cache
- name: Summary
env:
VERSION: ${{ steps.info.outputs.version }}
GROUP: ${{ steps.info.outputs.group }}
ARTIFACT_NAME: ${{ steps.info.outputs.artifact_name }}
run: |
echo "### Java Dependency Published to GitHub Packages" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Property | Value |" >> $GITHUB_STEP_SUMMARY
echo "|----------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| **Group** | $GROUP |" >> $GITHUB_STEP_SUMMARY
echo "| **Artifact** | $ARTIFACT_NAME |" >> $GITHUB_STEP_SUMMARY
echo "| **Version** | $VERSION |" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Usage (Gradle):**" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`gradle" >> $GITHUB_STEP_SUMMARY
echo "implementation '${GROUP}:${ARTIFACT_NAME}:${VERSION}'" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY