Deploy #3
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: Deploy | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| packages: write | |
| env: | |
| IMAGE_NAME: ghcr.io/jobdri-developer/backend | |
| jobs: | |
| build-and-push: | |
| name: Build and Push Image | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build and push | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| push: true | |
| tags: | | |
| ${{ env.IMAGE_NAME }}:latest | |
| ${{ env.IMAGE_NAME }}:${{ github.sha }} | |
| deploy: | |
| name: Deploy to Server | |
| runs-on: ubuntu-latest | |
| needs: build-and-push | |
| env: | |
| HAS_DEPLOY_SECRETS: ${{ secrets.DEPLOY_HOST != '' && secrets.DEPLOY_USER != '' && secrets.DEPLOY_SSH_KEY != '' && secrets.DEPLOY_PATH != '' && secrets.GHCR_USERNAME != '' && secrets.GHCR_TOKEN != '' }} | |
| steps: | |
| - name: Skip deploy when server secrets are missing | |
| if: env.HAS_DEPLOY_SECRETS != 'true' | |
| run: echo "Deployment skipped because required server secrets are not configured." | |
| - name: Deploy with Docker Compose | |
| if: env.HAS_DEPLOY_SECRETS == 'true' | |
| uses: appleboy/ssh-action@v1.2.0 | |
| env: | |
| IMAGE_NAME: ${{ env.IMAGE_NAME }} | |
| IMAGE_TAG: latest | |
| GHCR_USERNAME: ${{ secrets.GHCR_USERNAME }} | |
| GHCR_TOKEN: ${{ secrets.GHCR_TOKEN }} | |
| with: | |
| host: ${{ secrets.DEPLOY_HOST }} | |
| username: ${{ secrets.DEPLOY_USER }} | |
| key: ${{ secrets.DEPLOY_SSH_KEY }} | |
| port: ${{ secrets.DEPLOY_PORT || 22 }} | |
| envs: IMAGE_NAME,IMAGE_TAG,GHCR_USERNAME,GHCR_TOKEN | |
| script: | | |
| cd "${{ secrets.DEPLOY_PATH }}" | |
| echo "$GHCR_TOKEN" | docker login ghcr.io -u "$GHCR_USERNAME" --password-stdin | |
| export IMAGE_NAME="$IMAGE_NAME" | |
| export IMAGE_TAG="$IMAGE_TAG" | |
| docker compose -f docker-compose.prod.yml pull api | |
| docker compose -f docker-compose.prod.yml up -d api | |
| docker image prune -f |