|
| 1 | +name: CICD |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "main" ] |
| 6 | + pull_request: |
| 7 | + branches: [ "main" ] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v3 |
| 17 | + - name: Install JDK 17 |
| 18 | + uses: actions/setup-java@v3 |
| 19 | + with: |
| 20 | + java-version: '17' |
| 21 | + distribution: 'temurin' |
| 22 | + |
| 23 | + - name: Create application-secret-deploy.yml |
| 24 | + run: | |
| 25 | + echo "Decoding SECRET_FILE secret and create application-secret-deploy.yml" |
| 26 | + echo "${{ secrets.SECRET_FILE }}" | base64 -d > ./src/main/resources/application-secret-deploy.yml |
| 27 | +
|
| 28 | +
|
| 29 | + - name: Build with Gradle |
| 30 | + run: | |
| 31 | + chmod 777 ./gradlew |
| 32 | + ./gradlew clean assemble -x test |
| 33 | +
|
| 34 | + - name: Login to DockerHub |
| 35 | + if: github.event_name == 'push' |
| 36 | + uses: docker/login-action@v1 |
| 37 | + with: |
| 38 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 39 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 40 | + |
| 41 | + - name: Build Docker |
| 42 | + if: github.event_name == 'push' |
| 43 | + run: docker build --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPO_NAME }} . |
| 44 | + |
| 45 | + - name: Push Docker |
| 46 | + if: github.event_name == 'push' |
| 47 | + run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPO_NAME }}:latest |
| 48 | + |
| 49 | + deploy: |
| 50 | + needs: build |
| 51 | + if: github.event_name == 'push' |
| 52 | + runs-on: ubuntu-latest |
| 53 | + steps: |
| 54 | + - name: Set target IP |
| 55 | + run: | |
| 56 | + STATUS=$(curl -o /dev/null -w "%{http_code}" "http://${{ secrets.LIVE_SERVER_IP }}/env") |
| 57 | + echo $STATUS |
| 58 | + if [ $STATUS = 200 ]; then |
| 59 | + CURRENT_UPSTREAM=$(curl -s "http://${{ secrets.LIVE_SERVER_IP }}/env") |
| 60 | + else |
| 61 | + CURRENT_UPSTREAM=green |
| 62 | + fi |
| 63 | + echo CURRENT_UPSTREAM=$CURRENT_UPSTREAM >> $GITHUB_ENV |
| 64 | + if [ $CURRENT_UPSTREAM = blue ]; then |
| 65 | + echo "CURRENT_PORT=8080" >> $GITHUB_ENV |
| 66 | + echo "STOPPED_PORT=8081" >> $GITHUB_ENV |
| 67 | + echo "TARGET_UPSTREAM=green" >> $GITHUB_ENV |
| 68 | + elif [ $CURRENT_UPSTREAM = green ]; then |
| 69 | + echo "CURRENT_PORT=8081" >> $GITHUB_ENV |
| 70 | + echo "STOPPED_PORT=8080" >> $GITHUB_ENV |
| 71 | + echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV |
| 72 | + else |
| 73 | + echo "error" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +
|
| 77 | + - name: Login Server |
| 78 | + uses: appleboy/ssh-action@master |
| 79 | + with: |
| 80 | + username: ${{ secrets.SERVER_USER_NAME }} |
| 81 | + host: ${{ secrets.LIVE_SERVER_IP }} |
| 82 | + key: ${{ secrets.SSH_KEY }} |
| 83 | + script_stop: true |
| 84 | + |
| 85 | + - name: Docker compose |
| 86 | + uses: appleboy/ssh-action@master |
| 87 | + with: |
| 88 | + username: ${{ secrets.SERVER_USER_NAME }} |
| 89 | + host: ${{ secrets.LIVE_SERVER_IP }} |
| 90 | + key: ${{ secrets.SSH_KEY }} |
| 91 | + script_stop: true |
| 92 | + script: | |
| 93 | + sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPO_NAME }}:latest |
| 94 | + sudo docker-compose -f docker-compose-${{env.TARGET_UPSTREAM}}.yml up -d |
| 95 | +
|
| 96 | + - name: Check deploy server URL |
| 97 | + uses: jtalk/url-health-check-action@v3 |
| 98 | + with: |
| 99 | + url: http://${{ secrets.LIVE_SERVER_IP }}:${{env.STOPPED_PORT}}/env |
| 100 | + max-attempts: 5 |
| 101 | + retry-delay: 10s |
| 102 | + |
| 103 | + - name: Change nginx upstream |
| 104 | + uses: appleboy/ssh-action@master |
| 105 | + with: |
| 106 | + username: ${{ secrets.SERVER_USER_NAME }} |
| 107 | + host: ${{ secrets.LIVE_SERVER_IP }} |
| 108 | + key: ${{ secrets.SSH_KEY }} |
| 109 | + script_stop: true |
| 110 | + script: | |
| 111 | + sudo docker exec -i nginxserver bash -c 'echo "set \$service_url ${{ env.TARGET_UPSTREAM }};" > /etc/nginx/conf.d/service-env.inc && nginx -s reload' |
| 112 | +
|
| 113 | + - name: Stop current server |
| 114 | + uses: appleboy/ssh-action@master |
| 115 | + with: |
| 116 | + username: ${{ secrets.SERVER_USER_NAME }} |
| 117 | + host: ${{ secrets.LIVE_SERVER_IP }} |
| 118 | + key: ${{ secrets.SSH_KEY }} |
| 119 | + script_stop: true |
| 120 | + script: | |
| 121 | + if [ $(sudo docker ps -a -q -f name=${{env.CURRENT_UPSTREAM}}) ]; then |
| 122 | + sudo docker stop ${{env.CURRENT_UPSTREAM}} |
| 123 | + sudo docker rm ${{env.CURRENT_UPSTREAM}} |
| 124 | + else |
| 125 | + echo "Container ${{env.CURRENT_UPSTREAM}} does not exist, skipping stop and remove steps." |
| 126 | + fi |
| 127 | +
|
| 128 | + - name: Delete old docker images |
| 129 | + uses: appleboy/ssh-action@master |
| 130 | + with: |
| 131 | + username: ${{ secrets.SERVER_USER_NAME }} |
| 132 | + host: ${{ secrets.LIVE_SERVER_IP }} |
| 133 | + key: ${{ secrets.SSH_KEY }} |
| 134 | + script: | |
| 135 | + dangling_images=$(sudo docker images -f "dangling=true" -q) |
| 136 | + if [ ! -z "$dangling_images" ]; then |
| 137 | + sudo docker rmi $dangling_images |
| 138 | + else |
| 139 | + echo "No dangling images found" |
| 140 | + fi |
0 commit comments