Skip to content

Commit

Permalink
Create docker-publish.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo authored Sep 14, 2024
1 parent 4169567 commit 62504a7
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: Build and Docker Package

on:
push:
branches:
- master
- homolog

jobs:
build:
name: Build Jar
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v3

- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '21'

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Refresh Gradle dependencies
run: ./gradlew --refresh-dependencies

- name: Cache Gradle dependencies
uses: actions/cache@v3
with:
path: |
~/.gradle/wrapper
~/.gradle/caches
~/.m2
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}-java-${{ matrix.java-version }}
restore-keys: |
${{ runner.os }}-gradle-java-${{ matrix.java-version }}-
${{ runner.os }}-gradle-
- name: Build the project
run: ./gradlew bootJar

- name: Save artifact for master branch
if: github.ref == 'refs/heads/master'
uses: actions/upload-artifact@v3
with:
name: jar-artifact
path: build/libs/*.jar
retention-days: 5

- name: Save artifact for other branches
if: github.ref != 'refs/heads/master'
uses: actions/upload-artifact@v3
with:
name: jar-artifact
path: build/libs/*.jar
retention-days: 2

docker-build:
name: Build and Push Docker Image
runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/master' || github.ref == 'refs/heads/homolog'
steps:
- name: Check out the repository
uses: actions/checkout@v3

- name: Download the JAR artifact
uses: actions/download-artifact@v3
with:
name: jar-artifact
path: ./build/libs

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to Docker registry
run: echo "${{ secrets.CI_REGISTRY_PASSWORD }}" | docker login ${{ secrets.CI_REGISTRY }} -u "${{ secrets.CI_REGISTRY_USER }}" --password-stdin

- name: Set branch-specific tag
id: tag
run: |
if [[ "${{ github.ref_name }}" == "master" ]]; then
echo "Running on default branch 'master': tag = 'latest'"
echo "tag=latest" >> $GITHUB_ENV
else
echo "Running on branch '${{ github.ref_name }}': tag = '${{ github.ref_name }}'"
echo "tag=${{ github.ref_name }}" >> $GITHUB_ENV
fi
- name: Display Docker image tag
run: echo "Docker image will be pushed with tag: ${{ env.tag }}"

- name: Build and push Docker image
run: |
docker build --pull -t ${{ secrets.CI_REGISTRY_IMAGE }}:${{ env.tag }} .
docker push ${{ secrets.CI_REGISTRY_IMAGE }}:${{ env.tag }}
- name: Clean up Docker images
run: docker rmi ${{ secrets.CI_REGISTRY_IMAGE }}:${{ env.tag }} || true

0 comments on commit 62504a7

Please sign in to comment.