Skip to content

Commit

Permalink
GCP Deployment + Fixing CI/CD and Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
sdikyarts committed Apr 24, 2024
1 parent 7788724 commit 3b8d8ba
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
67 changes: 55 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name: CI/CD Pipeline
name: Java CI/CD Pipeline

on:
push:
branches:
- main
pull_request:

jobs:
build:
Expand All @@ -19,27 +18,26 @@ jobs:
with:
distribution: "temurin"
java-version: "21"
cache: gradle
cache: "gradle"

- name: Cache Gradle dependencies
uses: actions/cache@v4
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Build with Gradle
run: |
./gradlew assemble
- name: Upload build artifacts
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: java-app
path: build/libs/*.jar

test:
name: Test
runs-on: ubuntu-latest
Expand All @@ -53,20 +51,65 @@ jobs:
with:
distribution: "temurin"
java-version: "21"
cache: gradle
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*') }}
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Make gradlew executable
run: chmod +x ./gradlew
- name: Tests with 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 -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 }}"
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM gradle:jdk21-alpine
ARG PRODUCTION
ARG JDBC_DATABASE_PASSWORD
ARG JDBC_DATABASE_URL
ARG JDBC_DATABASE_USERNAME

ENV PRODUCTION ${PRODUCTION}
ENV JDBC_DATABASE_PASSWORD ${JDBC_DATABASE_PASSWORD}
ENV JDBC_DATABASE_URL ${JDBC_DATABASE_URL}
ENV JDBC_DATABASE_USERNAME ${JDBC_DATABASE_USERNAME}

WORKDIR /app
COPY ./build/libs/subscription-admin-0.0.1.jar /app
EXPOSE 8080
CMD ["java","-jar","subscription-admin-0.0.1.jar"]
7 changes: 2 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ tasks.withType<Test>().configureEach() {
}

tasks.test {
filter {
excludeTestsMatching("*FunctionalTest")
}

finalizedBy(tasks.jacocoTestReport)
useJUnitPlatform()
finalizedBy(tasks.jacocoTestReport) // report is always generated after tests run
}

tasks.jacocoTestReport {
Expand Down

0 comments on commit 3b8d8ba

Please sign in to comment.