Update docker-publish.yml #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: Build and push Docker image | |
run: | | |
if [[ "${{ github.ref_name }}" == "master" ]]; then | |
tag="latest" | |
echo "Running on default branch 'master': tag = 'latest'" | |
else | |
tag="${{ github.ref_name }}" | |
echo "Running on branch '${{ github.ref_name }}': tag = '${{ github.ref_name }}'" | |
fi | |
docker build --pull -t ${{ secrets.CI_REGISTRY_IMAGE }}:$tag . | |
docker push ${{ secrets.CI_REGISTRY_IMAGE }}:$tag | |
- name: Clean up Docker images | |
run: | | |
docker rmi ${{ secrets.CI_REGISTRY_IMAGE }}:$tag || true |