Merge pull request #21 from ADPRO-C11/staging #38
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: Java CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- main | |
- staging | |
jobs: | |
build: | |
name: Build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: "temurin" | |
java-version: "21" | |
cache: "gradle" | |
- name: Cache Gradle dependencies | |
uses: actions/cache@v4 | |
with: | |
path: ~/.gradle/caches | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Replace placeholders in application.properties | |
run: | | |
sed -i "s|JDBC_DATABASE_URL=.*|JDBC_DATABASE_URL=${{ secrets.JDBC_DATABASE_URL }}|g" src/main/resources/application.properties | |
sed -i "s|JDBC_DATABASE_USERNAME=.*|JDBC_DATABASE_USERNAME=${{ secrets.JDBC_DATABASE_USERNAME }}|g" src/main/resources/application.properties | |
sed -i "s|JDBC_DATABASE_PASSWORD=.*|JDBC_DATABASE_PASSWORD=${{ secrets.JDBC_DATABASE_PASSWORD }}|g" src/main/resources/application.properties | |
sed -i "s|PRODUCTION=.*|PRODUCTION=${{ secrets.PRODUCTION }}|g" src/main/resources/application.properties | |
sed -i 's|${JWT_SECRET}|'"${{ secrets.JWT_SECRET }}"'|g' src/main/resources/application-prod.properties | |
- name: Make gradlew executable | |
run: chmod +x ./gradlew | |
- name: Build with Gradle | |
run: | | |
./gradlew assemble | |
- name: Upload Artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: java-app | |
path: build/libs/*.jar | |
test: | |
name: Test | |
runs-on: ubuntu-latest | |
needs: build | |
services: | |
postgres: | |
image: postgres:latest | |
env: | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: super | |
POSTGRES_DB: subscription-admin | |
ports: | |
- 5432:5432 | |
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: "temurin" | |
java-version: "21" | |
cache: "gradle" | |
- name: Make gradlew executable | |
run: chmod +x ./gradlew | |
- name: Cache Gradle dependencies | |
uses: actions/cache@v4 | |
with: | |
path: ~/.gradle/caches | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Test with Gradle | |
run: | | |
./gradlew check --info --stacktrace | |
./gradlew test | |
./gradlew jacocoTestReport | |
publish: | |
name: Publish Docker Image | |
runs-on: ubuntu-latest | |
needs: test | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: check directory | |
run: ls -al | |
- name: Download Artifact | |
uses: actions/download-artifact@v4 | |
with: | |
name: java-app | |
- name: check directory | |
run: ls -al | |
- name: Docker login | |
run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin docker.io | |
- name: Set Docker BuildKit | |
run: export DOCKER_BUILDKIT=1 | |
- name: Build Docker Image | |
run: | | |
docker build --build-arg PRODUCTION=$PRODUCTION --build-arg JDBC_DATABASE_PASSWORD=$JDBC_DATABASE_PASSWORD --build-arg JDBC_DATABASE_URL=$JDBC_DATABASE_URL --build-arg JDBC_DATABASE_USERNAME=$JDBC_DATABASE_USERNAME --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} -t ${{ secrets.REGISTRY_USER }}/${{ secrets.IMAGE_NAME }}:${{ secrets.IMAGE_TAG }} . | |
docker push ${{ secrets.REGISTRY_USER }}/${{ secrets.IMAGE_NAME }}:${{ secrets.IMAGE_TAG }} | |
deploy: | |
name: Deploy to GCP | |
runs-on: ubuntu-latest | |
needs: publish | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Install SSH client | |
run: sudo apt-get install openssh-client | |
- name: create ssh key | |
run: echo "${{ secrets.SSH_KEY }}" > ssh-key.pem && cat ssh-key.pem | |
- name: update permission | |
run: chmod 400 ssh-key.pem | |
- name: Deploy to GCP | |
run: | | |
ssh -o StrictHostKeyChecking=no -i ssh-key.pem ${{ secrets.GCP_USERNAME }}@${{ secrets.GCP_STATIC_IP }} " | |
sudo docker container rm -f ${{ secrets.CONTAINER_NAME }} || true && | |
sudo docker image rm -f ${{ secrets.REGISTRY_USER }}/${{ secrets.IMAGE_NAME }}:${{ secrets.IMAGE_TAG }} || true && | |
sudo docker run --name ${{ secrets.CONTAINER_NAME }} -d -p 80:8080 ${{ secrets.REGISTRY_USER }}/${{ secrets.IMAGE_NAME }}:${{ secrets.IMAGE_TAG }}" |