๐ฆ Chore: ๋ง์ฝ ์ปจํ ์ด๋๊ฐ ์คํ์ค์ด๋ผ๋ฉด ์ค์ง ๋ฐ ์ญ์ ํ๋ ๋ก์ง ์ถ๊ฐ #2
Workflow file for this run
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
| # github repository actions ํ์ด์ง์ ๋ํ๋ ์ด๋ฆ | |
| name: CI/CD using github actions & docker | |
| # event trigger | |
| # main ๋ธ๋์น์ push๊ฐ ๋์์ ๋ ์คํ | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| permissions: | |
| contents: read | |
| jobs: | |
| CI-CD: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # JDK setting - github actions์์ ์ฌ์ฉํ JDK ์ค์ (ํ๋ก์ ํธ๋ AWS์ java ๋ฒ์ ๊ณผ ๋ฌ๋ผ๋ ๋ฌด๋ฐฉ) | |
| - uses: actions/checkout@v3 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| # gradle caching - ๋น๋ ์๊ฐ ํฅ์ | |
| - 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- | |
| # ํ๊ฒฝ๋ณ yml ํ์ผ ์์ฑ(1) - application.yml | |
| - name: make application.yml | |
| if: | | |
| contains(github.ref, 'main') | |
| run: | | |
| cd ./src/main/resources # resources ํด๋๋ก ์ด๋ | |
| touch ./application.yml # application.yml ์์ฑ | |
| echo "${{ secrets.YML }}" > ./application.yml # github actions์์ ์ค์ ํ ๊ฐ์ application.yml ํ์ผ์ ์ฐ๊ธฐ | |
| shell: bash | |
| # gradle build | |
| - name: Build with Gradle | |
| run: ./gradlew build -x test | |
| # docker build & push to production | |
| - name: Docker build & push to prod | |
| if: contains(github.ref, 'main') | |
| run: | | |
| docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
| docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/interview-be-service . | |
| docker push ${{ secrets.DOCKER_USERNAME }}/interview-be-service | |
| ## deploy to production | |
| - name: Deploy to prod | |
| uses: appleboy/ssh-action@master | |
| id: deploy-prod | |
| if: contains(github.ref, 'main') | |
| with: | |
| host: ${{ secrets.HOST_PROD }} # EC2 ํผ๋ธ๋ฆญ IPv4 DNS | |
| username: ubuntu | |
| key: ${{ secrets.PRIVATE_KEY }} | |
| envs: GITHUB_SHA | |
| script: | | |
| docker stop interview-be-app || true # ์ปจํ ์ด๋๊ฐ ์คํ ์ค์ด๋ฉด ์ค์ง | |
| docker rm interview-be-app || true # ๊ธฐ์กด ์ปจํ ์ด๋ ์ญ์ | |
| docker rmi ${{ secrets.DOCKER_USERNAME }}/interview-be-service || true # ๊ธฐ์กด ์ด๋ฏธ์ง ์ญ์ | |
| sudo docker pull ${{ secrets.DOCKER_USERNAME }}/interview-be-service # ์ต์ ์ด๋ฏธ์ง pull | |
| sudo docker run -d --name interview-be-app -p 8080:8080 ${{ secrets.DOCKER_USERNAME }}/interview-be-service # ์ปจํ ์ด๋ ์คํ | |
| sudo docker image prune -f |