1.0.9 #126
Workflow file for this run
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: CI/CD | |
on: | |
pull_request: | |
types: [opened, synchronize, reopened, closed] | |
branches: | |
- main | |
release: | |
types: [published] | |
branches: | |
- main | |
concurrency: | |
group: ${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
prepare: | |
runs-on: ubuntu-latest | |
outputs: | |
tag_version: ${{ steps.set-tag-version.outputs.TAG_VERSION }} | |
push_image: ${{ steps.set-push-image.outputs.PUSH_IMAGE }} | |
deploy: ${{ steps.set-deploy.outputs.DEPLOY }} | |
environment: ${{ steps.set-deploy.outputs.ENVIRONMENT }} | |
steps: | |
- name: Validate release target branch | |
if: ${{ github.event_name == 'release' }} | |
run: | | |
if [[ "${{ github.event.release.target_commitish }}" != "${{ github.event.repository.default_branch }}" ]]; then | |
echo "Release target branch is not the default branch" | |
exit 1 | |
fi | |
- name: Set tag version | |
id: set-tag-version | |
shell: bash | |
run: | | |
if [ -n "${{ github.event.release.tag_name }}" ]; then | |
echo "Setting TAG_VERSION to ${{ github.event.release.tag_name }}" | |
echo "TAG_VERSION=${{ github.event.release.tag_name }}" >> "$GITHUB_OUTPUT" | |
else | |
echo "Setting TAG_VERSION to ${{ github.sha }}" | |
echo "TAG_VERSION=${{ github.sha }}" >> "$GITHUB_OUTPUT" | |
fi | |
- name: Set push image | |
id: set-push-image | |
shell: bash | |
run: | | |
if [[ "${{ github.event_name }}" == "pull_request" && "${{ github.event.action }}" == "closed" && "${{ github.event.pull_request.merged }}" == "true" ]] || | |
[[ "${{ github.event_name }}" == "release" && "${{ github.event.action }}" == "published" ]]; then | |
echo "Setting PUSH_IMAGE to true" | |
echo "PUSH_IMAGE=true" >> "$GITHUB_OUTPUT" | |
else | |
echo "Setting PUSH_IMAGE to false" | |
echo "PUSH_IMAGE=false" >> "$GITHUB_OUTPUT" | |
fi | |
- name: Set deploy | |
id: set-deploy | |
shell: bash | |
run: | | |
if [[ "${{ github.event_name }}" == "pull_request" && "${{ github.event.action }}" == "closed" && "${{ github.event.pull_request.merged }}" == "true" ]]; then | |
echo "Setting DEPLOY to true" | |
echo "Setting ENVIRONMENT to staging" | |
echo "DEPLOY=true" >> "$GITHUB_OUTPUT" | |
echo "ENVIRONMENT=STAGING" >> "$GITHUB_OUTPUT" | |
elif [[ "${{ github.event_name }}" == "release" && "${{ github.event.action }}" == "published" ]]; then | |
echo "Setting DEPLOY to true" | |
echo "Setting ENVIRONMENT to production" | |
echo "DEPLOY=true" >> "$GITHUB_OUTPUT" | |
echo "ENVIRONMENT=PRODUCTION" >> "$GITHUB_OUTPUT" | |
else | |
echo "Setting DEPLOY to false" | |
echo "DEPLOY=false" >> "$GITHUB_OUTPUT" | |
fi | |
build-and-test: | |
runs-on: ubuntu-latest | |
needs: prepare | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v2 | |
with: | |
distribution: 'adopt' | |
java-version: '17' | |
- name: Compile | |
run: | | |
echo "Compiling the code" | |
./mvnw compile | |
- name: Test | |
run: | | |
echo "Running the tests" | |
./mvnw test | |
build-image-push: | |
runs-on: ubuntu-latest | |
needs: [prepare, build-and-test] | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Extract metadata (tags, labels) for Docker | |
uses: docker/metadata-action@v3 | |
with: | |
images: ${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }} | |
- name: Build and push image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
file: Dockerfile | |
push: ${{ needs.prepare.outputs.push_image }} | |
tags: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }} | |
deploy: | |
runs-on: ubuntu-latest | |
needs: [prepare, build-and-test, build-image-push] | |
if: ${{ needs.prepare.outputs.deploy == 'true' }} | |
steps: | |
- name: Deploy | |
run: | | |
echo "Deploying the app to ${{ needs.prepare.outputs.environment }} using ${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }} image" |