빌드 및 배포 (GHCR + Compose) #128
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: 빌드 및 배포 (GHCR + Compose) | |
| on: | |
| push: | |
| branches: ["develop"] | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| packages: write | |
| env: | |
| IMAGE_NAME: ghcr.io/ssasinsa/wearagain-backend | |
| concurrency: | |
| group: deploy-develop | |
| cancel-in-progress: true | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ secrets.GHCR_USERNAME }} | |
| password: ${{ secrets.GHCR_TOKEN }} | |
| - name: Build & Push to GHCR (with cache) | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: true | |
| platforms: linux/amd64 | |
| tags: | | |
| ${{ env.IMAGE_NAME }}:latest | |
| ${{ env.IMAGE_NAME }}:${{ github.sha }} | |
| labels: | | |
| org.opencontainers.image.revision=${{ github.sha }} | |
| org.opencontainers.image.source=${{ github.repository }} | |
| cache-from: | | |
| type=gha | |
| type=registry,ref=${{ env.IMAGE_NAME }}:latest | |
| cache-to: type=gha,mode=max | |
| provenance: false | |
| sbom: false | |
| deploy: | |
| runs-on: self-hosted | |
| needs: build | |
| steps: | |
| - name: Login to GHCR (runner) | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ secrets.GHCR_USERNAME }} | |
| password: ${{ secrets.GHCR_TOKEN }} | |
| - name: Ensure deploy dir | |
| run: mkdir -p /opt/wearagain | |
| - name: Update .env and detect changes | |
| id: env | |
| working-directory: /opt/wearagain | |
| shell: bash | |
| run: | | |
| set -e | |
| TMP_ENV="$(mktemp)" | |
| printf '%s' "${{ secrets.ENV }}" > "$TMP_ENV" | |
| if [ -f .env ] && cmp -s "$TMP_ENV" .env; then | |
| echo "env_changed=false" >> $GITHUB_OUTPUT | |
| rm -f "$TMP_ENV" | |
| echo "No .env change" | |
| else | |
| umask 077 | |
| mv -f "$TMP_ENV" .env | |
| echo "env_changed=true" >> $GITHUB_OUTPUT | |
| echo ".env updated" | |
| fi | |
| - name: Ensure nginx up | |
| working-directory: /opt/wearagain | |
| run: docker compose up -d nginx | |
| - name: Pull latest images | |
| working-directory: /opt/wearagain | |
| run: | | |
| docker compose pull app || true | |
| - name: Recreate app if needed | |
| working-directory: /opt/wearagain | |
| shell: bash | |
| run: | | |
| set -e | |
| FORCE="" | |
| if [ "${{ steps.env.outputs.env_changed }}" = "true" ]; then | |
| FORCE="--force-recreate" | |
| fi | |
| docker compose up -d $FORCE --no-deps --pull always app | |
| docker image prune -f |