Backend CD Production #3
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: Backend CD Production | |
on: | |
workflow_dispatch | |
concurrency: | |
group: ${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
environment: test | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: 21 | |
distribution: temurin | |
cache: gradle | |
- name: Cache Gradle | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/build.gradle') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Add execute permissions | |
working-directory: ./backend | |
run: chmod +x ./gradlew | |
- name: Build | |
working-directory: ./backend | |
run: ./gradlew build -x test -x bootJar | |
- name: Test | |
env: | |
JASYPT_PASSWORD: ${{ secrets.JASYPT_PASSWORD }} | |
working-directory: ./backend | |
run: ./gradlew test | |
- name: Create bootJar | |
working-directory: ./backend | |
run: ./gradlew bootJar | |
- name: Build Docker image | |
working-directory: ./backend | |
run: | | |
docker build \ | |
-f Dockerfile-prod \ | |
-t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:prod \ | |
. | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Push Docker image | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:prod | |
deploy-a: | |
needs: build | |
runs-on: [self-hosted, prod-a] | |
environment: prod | |
steps: | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Run toggle script | |
run: | | |
cd ~/${{ secrets.DOCKERHUB_IMAGE_NAME }} | |
./run-toggle.sh | |
- name: Clean up unused Docker images | |
run: docker image prune -af | |
deploy-b: | |
needs: build | |
runs-on: [self-hosted, prod-b] | |
environment: prod | |
steps: | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Run toggle script | |
run: | | |
cd ~/${{ secrets.DOCKERHUB_IMAGE_NAME }} | |
./run-toggle.sh | |
- name: Clean up unused Docker images | |
run: docker image prune -af |