Develop #5
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: CICD | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
permissions: | |
contents: read | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Install JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Create application-secret-deploy.yml | |
run: | | |
echo "Decoding SECRET and create application-secret-deploy.yml" | |
echo "${{ secrets.SECRET }}" | base64 -d > ./src/main/resources/application-secret-deploy.yml | |
- name: Build with Gradle | |
run: | | |
chmod 777 ./gradlew | |
./gradlew clean assemble -x test | |
- name: Login to DockerHub | |
if: github.event_name == 'push' | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Build Docker | |
if: github.event_name == 'push' | |
run: docker build --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPO_NAME }} . | |
- name: Push Docker | |
if: github.event_name == 'push' | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPO_NAME }}:latest | |
deploy: | |
needs: build | |
if: github.event_name == 'push' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set target IP | |
run: | | |
STATUS=$(curl -o /dev/null -w "%{http_code}" "http://${{ secrets.LIVE_SERVER_IP }}/env") | |
echo $STATUS | |
if [ $STATUS = 200 ]; then | |
CURRENT_UPSTREAM=$(curl -s "http://${{ secrets.LIVE_SERVER_IP }}/env") | |
else | |
CURRENT_UPSTREAM=green | |
fi | |
echo CURRENT_UPSTREAM=$CURRENT_UPSTREAM >> $GITHUB_ENV | |
if [ $CURRENT_UPSTREAM = blue ]; then | |
echo "CURRENT_PORT=8080" >> $GITHUB_ENV | |
echo "STOPPED_PORT=8081" >> $GITHUB_ENV | |
echo "TARGET_UPSTREAM=green" >> $GITHUB_ENV | |
elif [ $CURRENT_UPSTREAM = green ]; then | |
echo "CURRENT_PORT=8081" >> $GITHUB_ENV | |
echo "STOPPED_PORT=8080" >> $GITHUB_ENV | |
echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV | |
else | |
echo "error" | |
exit 1 | |
fi | |
- name: Login Server | |
uses: appleboy/ssh-action@master | |
with: | |
username: ${{ secrets.SERVER_USER_NAME }} | |
host: ${{ secrets.LIVE_SERVER_IP }} | |
key: ${{ secrets.SSH_KEY }} | |
script_stop: true | |
- name: Docker compose | |
uses: appleboy/ssh-action@master | |
with: | |
username: ${{ secrets.SERVER_USER_NAME }} | |
host: ${{ secrets.LIVE_SERVER_IP }} | |
key: ${{ secrets.SSH_KEY }} | |
script_stop: true | |
script: | | |
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPO_NAME }}:latest | |
sudo docker-compose -f docker-compose-${{env.TARGET_UPSTREAM}}.yml up -d | |
- name: Check deploy server URL | |
uses: jtalk/url-health-check-action@v3 | |
with: | |
url: http://${{ secrets.LIVE_SERVER_IP }}:${{env.STOPPED_PORT}}/env | |
max-attempts: 5 | |
retry-delay: 10s | |
- name: Change nginx upstream | |
uses: appleboy/ssh-action@master | |
with: | |
username: ${{ secrets.SERVER_USER_NAME }} | |
host: ${{ secrets.LIVE_SERVER_IP }} | |
key: ${{ secrets.SSH_KEY }} | |
script_stop: true | |
script: | | |
sudo docker exec -i nginxserver bash -c 'echo "set \$service_url ${{ env.TARGET_UPSTREAM }};" > /etc/nginx/conf.d/service-env.inc && nginx -s reload' | |
- name: Stop current server | |
uses: appleboy/ssh-action@master | |
with: | |
username: ${{ secrets.SERVER_USER_NAME }} | |
host: ${{ secrets.LIVE_SERVER_IP }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
if [ $(sudo docker ps -a -q -f name=${{env.CURRENT_UPSTREAM}}) ]; then | |
sudo docker stop ${{env.CURRENT_UPSTREAM}} | |
sudo docker rm ${{env.CURRENT_UPSTREAM}} | |
else | |
echo "Container ${{env.CURRENT_UPSTREAM}} does not exist, skipping stop and remove steps." | |
fi | |
- name: Delete old docker images | |
uses: appleboy/ssh-action@master | |
with: | |
username: ${{ secrets.SERVER_USER_NAME }} | |
host: ${{ secrets.LIVE_SERVER_IP }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
dangling_images=$(sudo docker images -f "dangling=true" -q) | |
if [ ! -z "$dangling_images" ]; then | |
sudo docker rmi $dangling_images | |
else | |
echo "No dangling images found" | |
fi |