|
| 1 | +# Kamal deployment workflow for Staging |
| 2 | +# Deploys to staging after CI workflow passes on staging branch |
| 3 | + |
| 4 | +name: Deploy Staging |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: [CI] |
| 9 | + types: [completed] |
| 10 | + branches: [staging] |
| 11 | + workflow_dispatch: # Allow manual deployment |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: deploy-staging |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + deploy: |
| 19 | + name: Deploy to staging |
| 20 | + runs-on: ubuntu-latest |
| 21 | + environment: Staging |
| 22 | + # Only run if CI passed (or manual trigger) |
| 23 | + if: > |
| 24 | + github.event_name == 'workflow_dispatch' || |
| 25 | + (github.event.workflow_run.conclusion == 'success' && |
| 26 | + github.event.workflow_run.head_branch == 'staging') |
| 27 | +
|
| 28 | + steps: |
| 29 | + - name: Checkout repository |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Set up Docker Buildx |
| 33 | + uses: docker/setup-buildx-action@v3 |
| 34 | + |
| 35 | + - name: Login to GitHub Container Registry |
| 36 | + uses: docker/login-action@v3 |
| 37 | + with: |
| 38 | + registry: ghcr.io |
| 39 | + username: ${{ github.actor }} |
| 40 | + password: ${{ secrets.KAMAL_REGISTRY_PASSWORD }} |
| 41 | + |
| 42 | + - name: Build and push Docker image |
| 43 | + uses: docker/build-push-action@v6 |
| 44 | + with: |
| 45 | + context: . |
| 46 | + push: true |
| 47 | + tags: | |
| 48 | + ghcr.io/zainfathoni/kelas.rumahberbagi.com:staging-${{ github.sha }} |
| 49 | + ghcr.io/zainfathoni/kelas.rumahberbagi.com:staging-latest |
| 50 | + cache-from: type=gha |
| 51 | + cache-to: type=gha,mode=max |
| 52 | + platforms: linux/amd64 |
| 53 | + |
| 54 | + - name: Set up Ruby for Kamal |
| 55 | + uses: ruby/setup-ruby@v1 |
| 56 | + with: |
| 57 | + ruby-version: '3.3' |
| 58 | + |
| 59 | + - name: Install Kamal |
| 60 | + run: gem install kamal |
| 61 | + |
| 62 | + - name: Set up SSH agent |
| 63 | + uses: webfactory/ssh-agent@v0.9.0 |
| 64 | + with: |
| 65 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 66 | + |
| 67 | + - name: Add VPS to known hosts |
| 68 | + run: ssh-keyscan -t rsa,ecdsa,ed25519 103.235.75.227 >> ~/.ssh/known_hosts |
| 69 | + |
| 70 | + - name: Create Kamal secrets file |
| 71 | + run: | |
| 72 | + mkdir -p .kamal |
| 73 | + cat > .kamal/secrets << EOF |
| 74 | + KAMAL_REGISTRY_PASSWORD=${{ secrets.KAMAL_REGISTRY_PASSWORD }} |
| 75 | + SESSION_SECRET=${{ secrets.SESSION_SECRET }} |
| 76 | + MAGIC_LINK_SECRET=${{ secrets.MAGIC_LINK_SECRET }} |
| 77 | + MAILGUN_SENDING_KEY=${{ secrets.MAILGUN_SENDING_KEY }} |
| 78 | + MAILGUN_DOMAIN=${{ secrets.MAILGUN_DOMAIN }} |
| 79 | + EOF |
| 80 | +
|
| 81 | + - name: Deploy with Kamal |
| 82 | + run: kamal deploy -c config/deploy.staging.yml --skip-push --version staging-${{ github.sha }} |
| 83 | + env: |
| 84 | + KAMAL_REGISTRY_PASSWORD: ${{ secrets.KAMAL_REGISTRY_PASSWORD }} |
0 commit comments