diff --git a/.github/workflows/annotation_build.yml b/.github/workflows/annotation_build.yml new file mode 100644 index 00000000..0de8f936 --- /dev/null +++ b/.github/workflows/annotation_build.yml @@ -0,0 +1,46 @@ +name: Annotation CI/CD + +on: + #workflow_dispatch: + push: + branches: [ develop ] + paths: + - 'app/annotation-service/**' + +jobs: + + build-and-push: + runs-on: ubuntu-latest + + env: + working-directory: ./app/annotation-service + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Build Docker image + working-directory: ${{env.working-directory}} + run: | + docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/gamelounge-annotation:${{ github.run_number }} . + + - name: Login to Docker Hub + run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin + + - name: Push Docker image + run: | + docker push ${{ secrets.DOCKERHUB_USERNAME }}/gamelounge-annotation:${{ github.run_number }} + + # SSH and Deploy to Server + - name: Setup SSH Agent + uses: webfactory/ssh-agent@v0.5.3 + with: + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} + + - name: Deploy to Server + run: | + ssh -o StrictHostKeyChecking=no root@${{ secrets.SERVER_IP }} \ + "docker pull ${{ secrets.DOCKERHUB_USERNAME }}/gamelounge-annotation:${{ github.run_number }} && \ + docker stop gamelounge-annotation || true && \ + docker rm gamelounge-annotation || true && \ + docker run -d --name gamelounge-annotation -p 8081:8080 -e SPRING_DATASOURCE_URL="jdbc:postgresql://${{ secrets.SERVER_IP }}:5433/annotation" -e SPRING_DATASOURCE_USERNAME="annotation" -e SPRING_DATASOURCE_PASSWORD="${{ secrets.DOCKERHUB_PASSWORD }}" ${{ secrets.DOCKERHUB_USERNAME }}/gamelounge-annotation:${{ github.run_number }}" \ No newline at end of file diff --git a/app/annotation-service/Dockerfile b/app/annotation-service/Dockerfile new file mode 100644 index 00000000..dbf47c86 --- /dev/null +++ b/app/annotation-service/Dockerfile @@ -0,0 +1,29 @@ +# Build stage +FROM amazoncorretto:17-alpine3.15 AS TEMP_BUILD_IMAGE +ENV APP_HOME=/game-lounge-annotation-service/ +WORKDIR $APP_HOME +COPY . $APP_HOME + +# Copy the gradle files and wrapper +COPY build.gradle.kts settings.gradle.kts gradlew $APP_HOME +COPY gradle $APP_HOME/gradle + + +# Set the script to be executable +RUN chmod +x ./gradlew + +RUN ./gradlew build -x test + +# Final stage +FROM amazoncorretto:17-alpine3.15 +ENV ARTIFACT_NAME=annotation-service-0.0.1-SNAPSHOT.jar +ENV APP_HOME=/game-lounge-annotation-service/ +WORKDIR $APP_HOME +EXPOSE 8080 +RUN mkdir /annotation + +# Copy the JAR from the build stage to the final image +COPY --from=TEMP_BUILD_IMAGE $APP_HOME/build/libs/$ARTIFACT_NAME ./spring-boot-application.jar + +# Run the application +ENTRYPOINT ["java", "-jar", "spring-boot-application.jar"] diff --git a/app/annotation-service/src/main/resources/application.properties b/app/annotation-service/src/main/resources/application.properties index 696f19c3..5a0ab422 100644 --- a/app/annotation-service/src/main/resources/application.properties +++ b/app/annotation-service/src/main/resources/application.properties @@ -1,8 +1,8 @@ # Database properties -spring.datasource.url=jdbc:postgresql://localhost:5432/annotation-db -spring.datasource.username=postgres -spring.datasource.password=0000 +spring.datasource.url=${SPRING_DATASOURCE_URL} +spring.datasource.username=${SPRING_DATASOURCE_USERNAME} +spring.datasource.password=${SPRING_DATASOURCE_PASSWORD} # Hibernate properties spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect