|
| 1 | +name: TnT CD for dev with Gradle, Docker, AWS |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - develop |
| 7 | + |
| 8 | +jobs: |
| 9 | + move-files: |
| 10 | + name: move-files |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - name: Checkout with Submodule |
| 14 | + uses: actions/checkout@v4 |
| 15 | + with: |
| 16 | + submodules: true |
| 17 | + token: ${{ secrets.SUBMODULE_PAT }} |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Public IP |
| 21 | + |
| 22 | + |
| 23 | + - name: Configure AWS Credentials |
| 24 | + uses: aws-actions/configure-aws-credentials@v4 |
| 25 | + with: |
| 26 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} |
| 27 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }} |
| 28 | + aws-region: ${{ secrets.AWS_REGION }} |
| 29 | + |
| 30 | + - name: Github Actions IP 보안그룹 추가 |
| 31 | + run: | |
| 32 | + aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 |
| 33 | +
|
| 34 | + - name: AWS EC2 Create Directory |
| 35 | + uses: appleboy/ssh-action@master |
| 36 | + with: |
| 37 | + host: ${{ secrets.EC2_DEV_HOST }} |
| 38 | + username: ${{ secrets.EC2_DEV_USERNAME }} |
| 39 | + password: ${{ secrets.EC2_DEV_PASSWORD }} |
| 40 | + port: ${{ secrets.EC2_SSH_PORT }} |
| 41 | + timeout: 60s |
| 42 | + script: | |
| 43 | + mkdir -p /home/ubuntu/tnt/ |
| 44 | +
|
| 45 | + - name: Docker env File |
| 46 | + run: |
| 47 | + cp src/main/resources/config/dev.env ./infra/.env |
| 48 | + |
| 49 | + - name: AWS EC2 Copy Files |
| 50 | + uses: appleboy/scp-action@master |
| 51 | + with: |
| 52 | + host: ${{ secrets.EC2_DEV_HOST }} |
| 53 | + username: ${{ secrets.EC2_DEV_USERNAME }} |
| 54 | + password: ${{ secrets.EC2_DEV_PASSWORD }} |
| 55 | + port: ${{ secrets.EC2_SSH_PORT }} |
| 56 | + timeout: 60s |
| 57 | + source: "infra" |
| 58 | + target: "/home/ubuntu/tnt" |
| 59 | + |
| 60 | + - name: Github Actions IP 보안그룹 삭제 |
| 61 | + run: | |
| 62 | + aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 |
| 63 | +
|
| 64 | + deploy: |
| 65 | + runs-on: ubuntu-latest |
| 66 | + steps: |
| 67 | + - name: Checkout with Submodule |
| 68 | + uses: actions/checkout@v4 |
| 69 | + with: |
| 70 | + submodules: true |
| 71 | + token: ${{ secrets.SUBMODULE_PAT }} |
| 72 | + fetch-depth: 0 |
| 73 | + |
| 74 | + - name: Set up JDK 21 |
| 75 | + uses: actions/setup-java@v4 |
| 76 | + with: |
| 77 | + java-version: 21 |
| 78 | + distribution: 'corretto' |
| 79 | + |
| 80 | + - name: Gradle cache |
| 81 | + uses: actions/cache@v4 |
| 82 | + with: |
| 83 | + path: | |
| 84 | + ~/.gradle/caches |
| 85 | + ~/.gradle/wrapper |
| 86 | + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} |
| 87 | + restore-keys: | |
| 88 | + ${{ runner.os }}-gradle- |
| 89 | +
|
| 90 | + - name: Gradle 실행 권한 부여 |
| 91 | + run: chmod +x gradlew |
| 92 | + |
| 93 | + - name: Build without test |
| 94 | + run: ./gradlew build -x test |
| 95 | + |
| 96 | + - name: Docker Buildx setting |
| 97 | + |
| 98 | + |
| 99 | + - name: Dockerhub Login |
| 100 | + |
| 101 | + with: |
| 102 | + username: ${{ secrets.DOCKER_HUB_USERNAME }} |
| 103 | + password: ${{ secrets.DOCKER_HUB_TOKEN }} |
| 104 | + |
| 105 | + - name: Dockerhub build and push |
| 106 | + |
| 107 | + with: |
| 108 | + context: . |
| 109 | + file: ./infra/Dockerfile |
| 110 | + push: true |
| 111 | + tags: ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO }}:latest |
| 112 | + build-args: | |
| 113 | + "SPRING_ACTIVE_PROFILE=dev" |
| 114 | + platforms: | |
| 115 | + linux/amd64 |
| 116 | + linux/arm64 |
| 117 | +
|
| 118 | + - name: Public IP |
| 119 | + |
| 120 | + |
| 121 | + - name: Configure AWS Credentials |
| 122 | + uses: aws-actions/configure-aws-credentials@v4 |
| 123 | + with: |
| 124 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} |
| 125 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }} |
| 126 | + aws-region: ${{ secrets.AWS_REGION }} |
| 127 | + |
| 128 | + - name: Github Actions IP 보안그룹 추가 |
| 129 | + run: | |
| 130 | + aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 |
| 131 | +
|
| 132 | + - name: AWS EC2 Server Deploy |
| 133 | + uses: appleboy/ssh-action@master |
| 134 | + env: |
| 135 | + COMPOSE: "home/ubuntu/infra/docker-compose-dev.yml" |
| 136 | + with: |
| 137 | + envs: COMPOSE |
| 138 | + host: ${{ secrets.EC2_DEV_HOST }} |
| 139 | + username: ${{ secrets.EC2_DEV_USERNAME }} |
| 140 | + password: ${{ secrets.EC2_DEV_PASSWORD }} |
| 141 | + port: ${{ secrets.EC2_SSH_PORT }} |
| 142 | + script: | |
| 143 | + docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_TOKEN }} |
| 144 | + docker-compose -f $COMPOSE down --rmi all |
| 145 | + docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO }} |
| 146 | + docker-compose -f $COMPOSE up -d |
| 147 | + echo "##### EC2 배포 완료 #####" |
| 148 | +
|
| 149 | + - name: Github Actions IP 보안그룹 삭제 |
| 150 | + run: | |
| 151 | + aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_DEV_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 |
0 commit comments