Create github-action.yml #1
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: CI/CD with Docker | |
| on: | |
| push: | |
| branches: [ "deploy" ] | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: 21 | |
| distribution: 'zulu' | |
| - name: Make application.yml | |
| run: | | |
| mkdir -p ./bookduck/src/main/resources # 디렉토리가 없으면 생성 | |
| cd ./bookduck/src/main/resources | |
| touch ./application.yml | |
| echo "${{ secrets.APPLICATION }}" > ./application.yml | |
| - name: Gradle Caching | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| - name: Build with Gradle | |
| run: | | |
| cd ./bookduck | |
| chmod +x ./gradlew | |
| ./gradlew build -x test | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Docker build & push | |
| uses: docker/build-push-action@v2 | |
| with: | |
| context: ./bookduck | |
| file: ./bookduck/Dockerfile | |
| push: true | |
| platforms: linux/amd64 # EC2 아키텍쳐에 맞게 (x86 = amd64, arm = arm64) | |
| tags: ${{ secrets.DOCKER_REPO }}:latest | |
| - name: Deploy to Server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.HOST }} | |
| username: ${{ secrets.USERNAME }} | |
| key: ${{ secrets.KEY }} | |
| envs: GITHUB_SHA | |
| script: | | |
| sudo docker rm -f $(sudo docker ps -qa) | |
| sudo docker pull ${{ secrets.DOCKER_REPO }}:latest | |
| sudo docker run -d -p 8080:8080 ${{ secrets.DOCKER_REPO }}:latest | |
| sudo docker image prune -f |