From 1ab0d41a6ea9a6a3eb308612b747a814e74cfb92 Mon Sep 17 00:00:00 2001 From: wjddn2165 Date: Mon, 29 Jul 2024 20:56:10 +0900 Subject: [PATCH] docker cicd test --- .github/workflows/deploy.yml | 51 +++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 65236055..1846b848 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,4 +1,4 @@ -name: Deploy to EC2 +name: Deploy to EC2 with Docker Compose on: push: @@ -7,13 +7,11 @@ on: env: AWS_REGION: ap-northeast-2 - AWS_S3_BUCKET: spring-boot-code - AWS_CODE_DEPLOY_APPLICATION: spring-boot-deploy - AWS_CODE_DEPLOY_GROUP: spring-boot-CD-group + DOCKER_HUB_REPOSITORY: baeeejw/jgs-spring-boot working-directory: ./Server jobs: - deploy: + build-and-deploy: runs-on: ubuntu-latest steps: @@ -30,28 +28,39 @@ jobs: run: chmod +x gradlew working-directory: ${{ env.working-directory }} - - - name: gradlew init - run: ./gradlew init - working-directory: ${{ env.working-directory }} - - - name: 프로젝트 빌드 run: ./gradlew clean build -x test working-directory: ${{ env.working-directory }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 - - name: AWS credential 설정 - uses: aws-actions/configure-aws-credentials@v1 + - name: Cache Docker layers + uses: actions/cache@v2 with: - aws-region: ${{ env.AWS_REGION }} - aws-access-key-id: ${{ secrets.CICD_ACCESS_KEY }} - aws-secret-access-key: ${{ secrets.CICD_SECRET_KEY }} + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- - - name: S3에 업로드 - run: aws deploy push --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} --ignore-hidden-files --s3-location s3://$AWS_S3_BUCKET/spring-boot-code/$GITHUB_SHA.zip --source . - working-directory: ${{ env.working-directory }} + - name: Log in to Docker Hub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_TOKEN }} - - name: EC2에 배포 - run: aws deploy create-deployment --application-name ${{ env.AWS_CODE_DEPLOY_APPLICATION }} --deployment-config-name CodeDeployDefault.AllAtOnce --deployment-group-name ${{ env.AWS_CODE_DEPLOY_GROUP }} --s3-location bucket=$AWS_S3_BUCKET,key=spring-boot-code/$GITHUB_SHA.zip,bundleType=zip + - name: Build and push Docker image + run: | + docker buildx build --push --tag ${{ env.DOCKER_HUB_REPOSITORY }}:latest . working-directory: ${{ env.working-directory }} + + - name: SSH into EC2 and deploy with Docker Compose + uses: appleboy/ssh-action@v0.1.8 + with: + host: ${{ secrets.EC2_HOST }} + username: ubuntu + key: ${{ secrets.EC2_KEY }} + script: | + cd /home/ubuntu/spring-boot + docker-compose pull + docker-compose up -d