diff --git a/.env b/.env deleted file mode 100644 index 6d5f502b908a143939149a2e58f4a1823e7736c4..0000000000000000000000000000000000000000 --- a/.env +++ /dev/null @@ -1,3 +0,0 @@ -DB_URL=jdbc:postgresql://localhost:5432/be-authentication -DB_USERNAME=postgres -DB_PASSWORD=postgres diff --git a/.github/workflows/staging-ci-cd.yml b/.github/workflows/staging-ci-cd.yml index ccc2d24e7f4f9766978055dd6e464be9416947cc..b9f2245a1ecb24e75818e76ec49a7ccfddf507ff 100644 --- a/.github/workflows/staging-ci-cd.yml +++ b/.github/workflows/staging-ci-cd.yml @@ -40,16 +40,39 @@ jobs: uses: actions/checkout@v3 - name: Install the gcloud CLI - uses: google-github-actions/setup-gcloud@v0 + uses: google-github-actions/setup-gcloud@v2 with: project_id: ${{ secrets.GOOGLE_PROJECT }} service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }} export_default_credentials: true + - name: Authenticate with GCP + uses: google-github-actions/auth@v1 + with: + credentials_json: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }} + - name: Build and Push Docker Image env: + PRODUCTION: staging GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }} + JDBC_STAGING_DATABASE_PASSWORD: ${{ secrets.JDBC_STAGING_DATABASE_PASSWORD }} + JDBC_STAGING_DATABASE_URL: ${{ secrets.JDBC_STAGING_DATABASE_URL }} + JDBC_STAGING_DATABASE_USERNAME: ${{ secrets.JDBC_STAGING_DATABASE_USERNAME }} run: | gcloud auth configure-docker us-central1-docker.pkg.dev - docker build -t us-central1-docker.pkg.dev/$GOOGLE_PROJECT/my-repository/authentication:latest . - docker push us-central1-docker.pkg.dev/$GOOGLE_PROJECT/my-repository/authentication:latest \ No newline at end of file + docker build --build-arg PRODUCTION=$PRODUCTION --build-arg JDBC_STAGING_DATABASE_PASSWORD=$JDBC_STAGING_DATABASE_PASSWORD --build-arg JDBC_STAGING_DATABASE_URL=$JDBC_STAGING_DATABASE_URL --build-arg JDBC_STAGING_DATABASE_USERNAME=$JDBC_STAGING_DATABASE_USERNAME -t us-central1-docker.pkg.dev/$GOOGLE_PROJECT/staging-repository/authentication:latest . + docker push us-central1-docker.pkg.dev/$GOOGLE_PROJECT/staging-repository/authentication:latest + + - name: Install required components + run: | + gcloud components update + gcloud components install gke-gcloud-auth-plugin + + - name: Deploy to GKE + env: + GOOGLE_PROJECT: ${{ secrets.GOOGLE_PROJECT }} + GOOGLE_REPOSiTORY: staging-repository + run: | + gcloud container clusters get-credentials safetypin-staging --region asia-southeast2 + sed -i "s/GOOGLE_PROJECT/$GOOGLE_PROJECT/g" staging.yaml + kubectl apply -f staging.yaml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index a5403eff656f15272fcb7e6c9c10de0edac79cd4..9372156b8f55652b70d6d0af1f7bde92551459ba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,24 @@ RUN mvn clean package -DskipTests # Step 2: Use OpenJDK 21 to run the application FROM openjdk:21-jdk-slim + +# Setup envs +ARG PRODUCTION +ARG JDBC_DATABASE_PASSWORD +ARG JDBC_DATABASE_URL +ARG JDBC_DATABASE_USERNAME +ARG JDBC_STAGING_DATABASE_USERNAME +ARG JDBC_STAGING_DATABASE_URL +ARG JDBC_STAGING_DATABASE_URL + +ENV PRODUCTION ${PRODUCTION} +ENV JDBC_DATABASE_PASSWORD ${JDBC_DATABASE_PASSWORD} +ENV JDBC_DATABASE_URL ${JDBC_DATABASE_URL} +ENV JDBC_DATABASE_USERNAME ${JDBC_DATABASE_USERNAME} +ENV JDBC_STAGING_DATABASE_PASSWORD ${JDBC_STAGING_DATABASE_PASSWORD} +ENV JDBC_STAGING_DATABASE_URL ${JDBC_STAGING_DATABASE_URL} +ENV JDBC_STAGING_DATABASE_USERNAME ${JDBC_STAGING_DATABASE_USERNAME} + WORKDIR /app COPY --from=builder /app/target/authentication-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 diff --git a/pom.xml b/pom.xml index a35f43c5178bba8d8148b680251e9808c01c8150..feeca04bcf0d2e79d6426cfcf39d6c599e709f9b 100644 --- a/pom.xml +++ b/pom.xml @@ -155,16 +155,6 @@ <scope>test</scope> </dependency> - - <dependency> - <groupId>org.jacoco</groupId> - <artifactId>org.jacoco.agent</artifactId> - <version>0.8.11</version> - <scope>test</scope> - </dependency> - - - </dependencies> <build> @@ -212,6 +202,25 @@ <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> </plugin> + <plugin> + <groupId>org.jacoco</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <version>0.8.12</version> + <executions> + <execution> + <goals> + <goal>prepare-agent</goal> + </goals> + </execution> + <execution> + <id>report</id> + <phase>test</phase> + <goals> + <goal>report</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> </build> diff --git a/resources.yaml b/production.yaml similarity index 100% rename from resources.yaml rename to production.yaml diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index a0f2b58a7fb614b52250942544d45fe65ce53a0c..b7c904d50fe81e4261f1b3a6d8da9bc3c7e217cf 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -1,8 +1,8 @@ spring.application.name=authentication -spring.datasource.url=${DB_URL} -spring.datasource.username=${DB_PASSWORD} -spring.datasource.password=${DB_PASSWORD} +spring.datasource.url=jdbc:postgresql://localhost:5432/be-authentication +spring.datasource.username=postgres +spring.datasource.password=postgres spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect diff --git a/src/main/resources/application-staging.properties b/src/main/resources/application-staging.properties index c12917add41ed81cc9e56817d979dd07c7db729e..8491638fcdc38630d4c9b9b2ffafac01f9774c36 100644 --- a/src/main/resources/application-staging.properties +++ b/src/main/resources/application-staging.properties @@ -1 +1,12 @@ -spring.application.name=authentication \ No newline at end of file +spring.application.name=authentication + +spring.datasource.url=${JDBC_STAGING_DATABASE_URL} +spring.datasource.username=${JDBC_STAGING_DATABASE_USERNAME} +spring.datasource.password=${JDBC_STAGING_DATABASE_PASSWORD} + +spring.datasource.driver-class-name=org.postgresql.Driver +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect + +# Hibernate Properties +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c12917add41ed81cc9e56817d979dd07c7db729e..7087d052396c6c6e70c607393a2065ea43c6fec0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ -spring.application.name=authentication \ No newline at end of file +spring.application.name=authentication +spring.profiles.active=${PRODUCTION:dev} \ No newline at end of file diff --git a/staging.yaml b/staging.yaml new file mode 100644 index 0000000000000000000000000000000000000000..91457e76499c95f3a4b2718dfaf62f90a0dda88b --- /dev/null +++ b/staging.yaml @@ -0,0 +1,34 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: authentication +spec: + type: LoadBalancer + selector: + app: authentication + ports: + - port: 80 + targetPort: 8080 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: authentication + labels: + app: authentication +spec: + replicas: 1 + selector: + matchLabels: + app: authentication + template: + metadata: + labels: + app: authentication + spec: + containers: + - name: authentication + image: us-central1-docker.pkg.dev/GOOGLE_PROJECT/staging-repository/authentication:latest + ports: + - containerPort: 8080 \ No newline at end of file