Merge pull request #5 from capgoing/feature/cicd-workflows #3
This file contains hidden or 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: CI/CD FOR DEVELOP | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - develop | |
| env: | |
| DOCKERHUB_REPOSITORY: ${{ secrets.DOCKER_REPOSITORY }} | |
| jobs: | |
| CI: | |
| name: Continuous Integration | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| services: | |
| mongo: | |
| image: mongo:6.0 | |
| ports: | |
| - 27017:27017 | |
| env: | |
| MONGO_INITDB_ROOT_USERNAME: test | |
| MONGO_INITDB_ROOT_PASSWORD: testPW | |
| steps: | |
| - name: Get short SHA | |
| id: slug | |
| run: echo "sha7=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| - name: Wait for MongoDB to start | |
| run: sleep 10 | |
| - name: Build and Test with Gradle Wrapper | |
| run: | | |
| export SPRING_DATA_MONGODB_URI=mongodb://test:testPW@localhost:27017/testdb | |
| ./gradlew build test | |
| - name: Upload jar file to Artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: jar_files | |
| path: build/libs/*.jar | |
| - name: Upload Dockerfile to Artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: Dockerfile | |
| path: ./Dockerfile | |
| CD_Delivery_to_DockerHub: | |
| name: CD_Delivery_to_DockerHub | |
| needs: CI | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| steps: | |
| - name: Download jar file from Artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: jar_files | |
| path: build/libs | |
| - name: Download Dockerfile file from Artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: Dockerfile | |
| path: ./ | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Get short SHA | |
| id: slug | |
| run: echo "sha7=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT | |
| - name: Build, tag, and push image to DockerHub | |
| id: build-image | |
| env: | |
| USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
| IMAGE_TAG: ${{ steps.slug.outputs.sha7 }} | |
| run: | | |
| docker build -t $USERNAME/$DOCKERHUB_REPOSITORY:$IMAGE_TAG -t $USERNAME/$DOCKERHUB_REPOSITORY:latest . | |
| docker push $USERNAME/$DOCKERHUB_REPOSITORY --all-tags | |
| echo "image=$USERNAME/$DOCKERHUB_REPOSITORY:$IMAGE_TAG&latest" >> $GITHUB_OUTPUT | |
| CD_Deploy: | |
| name: CD_Deploy | |
| needs: CD_Delivery_to_DockerHub | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Get short SHA | |
| id: slug | |
| run: echo "sha7=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT | |
| - name: Executing remote ssh commands | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.REMOTE_IP }} | |
| username: ${{ secrets.REMOTE_USER }} | |
| key: ${{ secrets.REMOTE_PRIVATE_KEY }} | |
| port: ${{ secrets.REMOTE_SSH_PORT }} | |
| script: | | |
| export DOCKER_IMAGE=${{ secrets.DOCKER_USERNAME }}/capgoing:latest | |
| export DOCKER_COMPOSE_PATH=${{ secrets.DOCKER_COMPOSE_PATH }} | |
| cd /home/ubuntu/scripts | |
| ./rolling-update.sh | |
| echo "Stopping current containers..." | |
| docker compose -f $DOCKER_COMPOSE_PATH down | |
| echo "Pulling the latest image..." | |
| docker compose -f $DOCKER_COMPOSE_PATH pull | |
| echo "Starting new deployment..." | |
| docker compose -f $DOCKER_COMPOSE_PATH up -d |