Update dockerhub-ci.yml #82
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: docker-ci | |
| on: | |
| push: | |
| branches: [ "develop", "main" ] | |
| pull_request: | |
| branches: [ "main" ] | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: Checkout source | |
| uses: actions/checkout@v4 | |
| # Node 설치 (JS/CSS 빌드를 위한) | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| cache: "npm" | |
| - name: Install npm dependencies | |
| run: | | |
| if [ -f package.json ]; then | |
| npm install | |
| fi | |
| # JDK 설치 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: temurin | |
| java-version: '17' | |
| cache: gradle | |
| # Spring Boot 빌드 (JS/CSS 포함) | |
| - name: Build Spring Boot JAR | |
| run: ./gradlew clean bootJar --no-daemon | |
| # 🤝 Dockerfile에 COPY app.jar 하기 위해 파일명 통일 | |
| - name: Prepare JAR for docker build | |
| run: cp build/libs/*.jar app.jar | |
| # Docker metadata | |
| # - main push: prod + sha | |
| # - PR(main): pr-<num> + sha | |
| # - develop: push 안 하므로 태그만 생성돼도 업로드는 안 됨 | |
| - name: Docker metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: | | |
| ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.event.repository.name }} | |
| tags: | | |
| type=sha,format=short | |
| type=raw,value=prod,enable=${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} | |
| type=raw,value=pr-${{ github.event.number }},enable=${{ github.event_name == 'pull_request' && github.base_ref == 'main' }} | |
| # DockerHub 로그인 | |
| # - main push 또는 PR(main)일 때만 로그인/푸시 | |
| - name: Login to DockerHub | |
| if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || (github.event_name == 'pull_request' && github.base_ref == 'main') | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| # 이미지 빌드 & 푸시 | |
| # - main push 또는 PR(main)일 때만 push | |
| # - develop은 CI만 | |
| - name: Build and Push Docker Image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| push: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/main') || (github.event_name == 'pull_request' && github.base_ref == 'main') }} | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} |