From 75decea66caa5ae45dfa2dfd2fa8897e74f66599 Mon Sep 17 00:00:00 2001 From: choi seung keun Date: Sat, 20 Jul 2024 14:56:52 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20GitHub=20Action=20Workflow=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 빌드 작업과 배포 작업을 분리 - 기존 workflow 빌드, 배포 업무 개선 --- .github/workflows/photosmap-ci-cd.yml | 87 +++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 .github/workflows/photosmap-ci-cd.yml diff --git a/.github/workflows/photosmap-ci-cd.yml b/.github/workflows/photosmap-ci-cd.yml new file mode 100644 index 00000000..011a3574 --- /dev/null +++ b/.github/workflows/photosmap-ci-cd.yml @@ -0,0 +1,87 @@ +name: PhotosMap CI/CD Workflow + +on: + push: + branches: [ "develop" ] + pull_request: + branches: [ "develop" ] + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Create application-base-addi.yml + run: echo "${{ secrets.BASE_ADDI }}" > ./src/main/resources/application-base-addi.yml + + - name: Verify application-base-addi.yml + run: ls -la ./src/main/resources/ + + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + distribution: 'adopt' + java-version: '17' + + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + - name: Build with Gradle + run: ./gradlew bootJar + + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Build Docker image + run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/photosmap:latest --build-arg PROFILES=prod . + + - name: Push Docker image to Docker Hub + run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/photosmap:latest + + deploy: + runs-on: ubuntu-latest + needs: build + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up SSH + uses: webfactory/ssh-agent@v0.5.3 + with: + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} + + - name: Create docker-compose-prod.yml + run: echo "${{ secrets.DOCKER_COMPOSE_PROD }}" > ./docker-compose-prod.yml + + - name: Verify docker-compose-prod.yml + run: ls -la ./ + + - name: Set file permissions for application-base-addi.yml + run: chmod 644 ./src/main/resources/application-base-addi.yml + + - name: Copy files to EC2 + run: | + scp -P ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no ./docker-compose-prod.yml ${{ secrets.SSH_USER }}@${{ secrets.EC2_HOST }}:${{ secrets.EC2_PATH }}/ + scp -P ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no ./src/main/resources/application-base-addi.yml ${{ secrets.SSH_USER }}@${{ secrets.EC2_HOST }}:${{ secrets.EC2_PATH }}/ + + - name: Deploy on EC2 + run: | + ssh -p ${{ secrets.SSH_PORT }} -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.EC2_HOST }} << 'EOF' + docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} + docker pull ${{ secrets.DOCKERHUB_USERNAME }}/photosmap:latest + docker-compose -f ${{ secrets.EC2_PATH }}/docker-compose-prod.yml down + docker-compose -f ${{ secrets.EC2_PATH }}/docker-compose-prod.yml up -d + EOF + env: + DOCKER_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} + DOCKER_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}