Build and Deploy #458
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: Build and Deploy | |
| on: | |
| push: | |
| branches: | |
| - main | |
| pull_request: | |
| types: [synchronize, opened] | |
| schedule: | |
| - cron: '15 2 * * *' # Reset demo daily at 2:15 UTC | |
| workflow_dispatch: | |
| inputs: | |
| environment: | |
| type: environment | |
| required: true | |
| default: production | |
| concurrency: | |
| group: build-${{ github.ref_name }} | |
| cancel-in-progress: ${{ github.ref_name != 'main' }} | |
| jobs: | |
| Build: | |
| runs-on: ubuntu-latest | |
| env: | |
| IMAGE_NAME: ghcr.io/alchemycms/alchemy-demo | |
| steps: | |
| - name: Check out the repo | |
| uses: actions/checkout@v5 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| install: true | |
| - name: Expose GitHub Runtime for cache | |
| uses: crazy-max/ghaction-github-runtime@v3 | |
| - name: Set Alchemy Version | |
| run: | | |
| version=$(awk '/^ alchemy_cms \(/ { gsub(/[()]/, "", $2); print $2 }' "Gemfile.lock") | |
| echo "ALCHEMY_VERSION=$version" >> "$GITHUB_ENV" | |
| - name: Show Alchemy Version | |
| run: echo "Current Alchemy Version ${{ env.ALCHEMY_VERSION }}" | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: $ | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build Docker image | |
| id: docker_build | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: Dockerfile | |
| pull: true | |
| push: ${{ github.event_name != 'pull_request' }} | |
| tags: | | |
| ${{ env.IMAGE_NAME }}:${{ env.ALCHEMY_VERSION }} | |
| ${{ env.IMAGE_NAME }}:${{ github.sha }} | |
| ${{ env.IMAGE_NAME }}:latest | |
| labels: | | |
| service=alchemy-demo | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| Deploy: | |
| runs-on: ubuntu-latest | |
| needs: Build | |
| if: ${{ github.event_name == 'push' && github.ref_name == 'main' }} | |
| environment: | |
| name: production | |
| url: https://demo.alchemy-cms.com | |
| env: | |
| KAMAL_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} | |
| SECRET_KEY_BASE: ${{ secrets.SECRET_KEY_BASE }} | |
| ADMIN_LOGIN: ${{ secrets.ADMIN_LOGIN }} | |
| ADMIN_EMAIL: ${{ secrets.ADMIN_EMAIL }} | |
| ADMIN_PASSWORD: ${{ secrets.ADMIN_PASSWORD }} | |
| CLOUDINARY_URL: ${{ secrets.CLOUDINARY_URL }} | |
| SENTRY_DSN: ${{ secrets.SENTRY_DSN }} | |
| VERSION: ${{ github.sha }} | |
| steps: | |
| - name: Check out the repo | |
| uses: actions/checkout@v5 | |
| - name: Set up Ruby | |
| uses: ruby/setup-ruby@v1 | |
| with: | |
| bundler-cache: true | |
| - name: Setup SSH | |
| uses: webfactory/ssh-agent@v0.9.1 | |
| with: | |
| ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| - name: Deploy with Kamal | |
| id: deploy | |
| continue-on-error: true | |
| run: | | |
| set -o pipefail | |
| bin/kamal deploy --skip-push --version=$VERSION 2>&1 | tee /tmp/kamal-deploy.log | |
| - name: Reboot kamal-proxy and retry deploy | |
| if: steps.deploy.outcome == 'failure' | |
| run: | | |
| if grep -q "kamal-proxy.*is too old" /tmp/kamal-deploy.log; then | |
| echo "kamal-proxy is outdated, rebooting..." | |
| bin/kamal proxy reboot -y | |
| bin/kamal deploy --skip-push --version=$VERSION | |
| else | |
| echo "Deploy failed for a different reason" | |
| exit 1 | |
| fi |