|
| 1 | +name: Build & Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | +env: |
| 8 | + IMAGE: ghcr.io/leanderantony/ai_job_application_agent/api |
| 9 | + VPS_APP_DIR: /home/ubuntu/AI_Job_Application_Agent |
| 10 | + |
| 11 | +jobs: |
| 12 | + build-and-push: |
| 13 | + name: Build Docker image to GHCR |
| 14 | + runs-on: ubuntu-latest |
| 15 | + permissions: |
| 16 | + contents: read |
| 17 | + packages: write |
| 18 | + |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v5 |
| 22 | + |
| 23 | + - name: Log in to GHCR |
| 24 | + uses: docker/login-action@v4 |
| 25 | + with: |
| 26 | + registry: ghcr.io |
| 27 | + username: ${{ github.actor }} |
| 28 | + password: ${{ github.token }} |
| 29 | + |
| 30 | + - name: Set up Docker Buildx |
| 31 | + uses: docker/setup-buildx-action@v4 |
| 32 | + |
| 33 | + - name: Build and push |
| 34 | + uses: docker/build-push-action@v7 |
| 35 | + with: |
| 36 | + context: . |
| 37 | + push: true |
| 38 | + tags: ${{ env.IMAGE }}:latest,${{ env.IMAGE }}:${{ github.sha }} |
| 39 | + cache-from: type=registry,ref=${{ env.IMAGE }}:cache |
| 40 | + cache-to: type=registry,ref=${{ env.IMAGE }}:cache,mode=max |
| 41 | + |
| 42 | + deploy: |
| 43 | + name: Deploy to VPS |
| 44 | + runs-on: ubuntu-latest |
| 45 | + needs: build-and-push |
| 46 | + permissions: |
| 47 | + contents: read |
| 48 | + packages: read |
| 49 | + env: |
| 50 | + GITHUB_ACTOR: ${{ github.actor }} |
| 51 | + GITHUB_TOKEN: ${{ github.token }} |
| 52 | + |
| 53 | + steps: |
| 54 | + - name: Checkout |
| 55 | + uses: actions/checkout@v5 |
| 56 | + |
| 57 | + - name: Prepare VPS directory |
| 58 | + uses: appleboy/ssh-action@v1 |
| 59 | + with: |
| 60 | + host: ${{ secrets.VPS_HOST }} |
| 61 | + username: ${{ secrets.VPS_USER }} |
| 62 | + key: ${{ secrets.VPS_SSH_KEY }} |
| 63 | + port: ${{ secrets.VPS_PORT }} |
| 64 | + script: | |
| 65 | + mkdir -p "${{ env.VPS_APP_DIR }}/deploy/vps" |
| 66 | +
|
| 67 | + - name: Copy deployment files |
| 68 | + uses: appleboy/scp-action@v0.1.7 |
| 69 | + with: |
| 70 | + host: ${{ secrets.VPS_HOST }} |
| 71 | + username: ${{ secrets.VPS_USER }} |
| 72 | + key: ${{ secrets.VPS_SSH_KEY }} |
| 73 | + port: ${{ secrets.VPS_PORT }} |
| 74 | + source: deploy/vps/docker-compose.yml,deploy/vps/docker-compose.override.yml |
| 75 | + target: ${{ env.VPS_APP_DIR }}/deploy/vps |
| 76 | + strip_components: 2 |
| 77 | + |
| 78 | + - name: Deploy via SSH |
| 79 | + uses: appleboy/ssh-action@v1 |
| 80 | + env: |
| 81 | + IMAGE: ${{ env.IMAGE }} |
| 82 | + GITHUB_ACTOR: ${{ env.GITHUB_ACTOR }} |
| 83 | + GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }} |
| 84 | + with: |
| 85 | + host: ${{ secrets.VPS_HOST }} |
| 86 | + username: ${{ secrets.VPS_USER }} |
| 87 | + key: ${{ secrets.VPS_SSH_KEY }} |
| 88 | + port: ${{ secrets.VPS_PORT }} |
| 89 | + envs: IMAGE,GITHUB_TOKEN,GITHUB_ACTOR |
| 90 | + script: | |
| 91 | + echo "$GITHUB_TOKEN" | docker login ghcr.io -u "$GITHUB_ACTOR" --password-stdin |
| 92 | + docker pull "$IMAGE:latest" |
| 93 | + cd "${{ env.VPS_APP_DIR }}/deploy/vps" |
| 94 | + docker compose -p ai_job_application_agent up -d --no-deps --no-build --force-recreate api |
| 95 | + docker logout ghcr.io |
| 96 | +
|
| 97 | + - name: Health check |
| 98 | + uses: appleboy/ssh-action@v1 |
| 99 | + with: |
| 100 | + host: ${{ secrets.VPS_HOST }} |
| 101 | + username: ${{ secrets.VPS_USER }} |
| 102 | + key: ${{ secrets.VPS_SSH_KEY }} |
| 103 | + port: ${{ secrets.VPS_PORT }} |
| 104 | + script: | |
| 105 | + echo "Waiting for AI Job Application Agent API to be healthy..." |
| 106 | + for i in $(seq 1 12); do |
| 107 | + STATUS=$(docker inspect --format='{{.State.Health.Status}}' ai-job-application-agent-api 2>/dev/null) |
| 108 | + echo "Attempt $i: $STATUS" |
| 109 | + if [ "$STATUS" = "healthy" ]; then |
| 110 | + echo "Container is healthy" |
| 111 | + exit 0 |
| 112 | + fi |
| 113 | + sleep 5 |
| 114 | + done |
| 115 | + echo "Container did not become healthy in time" |
| 116 | + docker logs ai-job-application-agent-api --tail 30 |
| 117 | + exit 1 |
0 commit comments