Create docker-publish.yml #1
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: 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 |