CI: CICD 설정 #2
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: DecodEat CI/CD with Gradle and Docker | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| pull_request: | |
| branches: [ "main" ] | |
| permissions: | |
| contents: read | |
| jobs: | |
| CI-CD: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| steps: | |
| # 1. 코드 체크아웃 | |
| - uses: actions/checkout@v4 | |
| # 2. JDK 21 설정 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| # 3. Gradle 캐시 | |
| - name: Setup Gradle | |
| uses: gradle/actions/setup-gradle@v3 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| # 4. application.yml 생성 | |
| - name: Make application.yml | |
| if: contains(github.ref, 'main') || contains(github.ref, 'refs/pull/') | |
| run: | | |
| mkdir -p ./src/main/resources | |
| echo "${{ secrets.APPLICATION_DEV_YML }}" > ./src/main/resources/application.yml | |
| # 5. gradlew 실행 권한 부여 | |
| - name: Grant Execute Permission For Gradlew | |
| run: chmod +x gradlew | |
| # 6. Gradle로 JAR 빌드 | |
| - name: Build JAR | |
| run: ./gradlew clean build | |
| # 7. Docker 빌드 및 푸시 | |
| - name: Docker Build & Push | |
| run: | | |
| docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
| docker build -t ${{ secrets.DOCKER_USERNAME }}/decodeat:latest . | |
| docker push ${{ secrets.DOCKER_USERNAME }}/decodeat:latest | |
| # 8. Docker 배포 | |
| - name: Deploy to Dev | |
| uses: appleboy/ssh-action@master | |
| if: contains(github.ref,'main') | |
| with: | |
| key: ${{ secrets.PRIVATE_KEY }} | |
| host: ${{ secrets.HOST }} | |
| username: ${{ secrets.USERNAME }} | |
| port: 22 | |
| script: | | |
| docker rm -f $(docker ps -qa) || true | |
| docker pull ${{ secrets.DOCKER_USERNAME }}/decodeat:latest | |
| docker-compose up -d | |
| docker image prune -f | |
| # 9. 현재 시간 출력 | |
| current-time: | |
| needs: CI-CD | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Get Current Time | |
| uses: 1466587594/get-current-time@v2 | |
| id: current-time | |
| with: | |
| format: YYYY-MM-DDTHH:mm:ss | |
| utcOffset: "+09:00" | |
| - name: Print Current Time | |
| run: echo "Current Time=${{steps.current-time.outputs.formattedTime}}" |