Repository navigation
Keep the staff caveats Astra flagged #32
Workflow file for this run
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: Preview | |
| # Every branch except main gets its own copy of the site at | |
| # https://<branch>.tfminecraft.net, built from that branch alone. The server | |
| # stops it 90 minutes after the last deploy: push again, or run this workflow | |
| # on the branch, to bring it back. Deleting the branch removes it at once. | |
| # | |
| # Pushes to main build the same images without deploying them, to refresh the | |
| # registry build cache every branch starts from. | |
| on: | |
| push: | |
| branches-ignore: ['dependabot/**'] | |
| workflow_dispatch: | |
| delete: | |
| permissions: | |
| contents: read | |
| concurrency: | |
| group: preview-${{ github.event_name == 'delete' && github.event.ref || github.ref_name }} | |
| cancel-in-progress: true | |
| env: | |
| IMAGE_PREFIX: ghcr.io/tf-minecraft/provincesystem-preview | |
| IS_MAIN: ${{ github.ref_name == 'main' }} | |
| jobs: | |
| build: | |
| if: github.event_name != 'delete' && github.ref_type == 'branch' | |
| runs-on: ubuntu-24.04 | |
| timeout-minutes: 30 | |
| permissions: | |
| contents: read | |
| packages: write | |
| strategy: | |
| matrix: | |
| service: [backend, frontend] | |
| steps: | |
| - uses: actions/checkout@v7 | |
| with: | |
| persist-credentials: false | |
| - id: slug | |
| if: env.IS_MAIN != 'true' | |
| run: python3 .github/scripts/preview_slug.py "$GITHUB_REF_NAME" | |
| - uses: docker/setup-buildx-action@v4 | |
| - uses: docker/login-action@v4 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Build ${{ matrix.service }} | |
| uses: docker/build-push-action@v7 | |
| with: | |
| context: . | |
| file: ${{ matrix.service }}/Dockerfile | |
| push: ${{ env.IS_MAIN != 'true' }} | |
| provenance: false | |
| # The browser calls the preview's own /api, which the router sends to its backend. | |
| build-args: ${{ matrix.service == 'frontend' && format('NEXT_PUBLIC_API_URL={0}/api', steps.slug.outputs.url) || '' }} | |
| tags: ${{ env.IS_MAIN != 'true' && format('{0}-{1}:{2}', env.IMAGE_PREFIX, matrix.service, steps.slug.outputs.slug) || '' }} | |
| labels: org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} | |
| # Branches cannot read each other's Actions cache, so main keeps a registry cache they all share. | |
| cache-from: type=registry,ref=${{ env.IMAGE_PREFIX }}-${{ matrix.service }}:buildcache | |
| cache-to: ${{ env.IS_MAIN == 'true' && format('type=registry,ref={0}-{1}:buildcache,mode=max', env.IMAGE_PREFIX, matrix.service) || '' }} | |
| deploy: | |
| needs: build | |
| if: github.ref_name != 'main' | |
| runs-on: ubuntu-24.04 | |
| timeout-minutes: 10 | |
| environment: | |
| name: preview | |
| url: ${{ steps.slug.outputs.url }} | |
| steps: | |
| - uses: actions/checkout@v7 | |
| with: | |
| persist-credentials: false | |
| - id: slug | |
| run: python3 .github/scripts/preview_slug.py "$GITHUB_REF_NAME" | |
| - name: Start the preview | |
| env: | |
| SSH_USER: psdeploy | |
| SSH_KEY: ${{ secrets.PREVIEW_SSH_KEY }} | |
| SSH_HOST: ${{ secrets.PREVIEW_SSH_HOST }} | |
| SSH_KNOWN_HOSTS: ${{ secrets.PREVIEW_SSH_KNOWN_HOSTS }} | |
| SLUG: ${{ steps.slug.outputs.slug }} | |
| run: | | |
| status=0 | |
| .github/scripts/server_ssh.sh up "$SLUG" "$GITHUB_SHA" || status=$? | |
| if [ "$status" -eq 3 ]; then | |
| echo "::error::Four previews are already running. Delete a finished branch, or stop one on the server with: sudo -u tfmc ps-preview down <slug>" | |
| fi | |
| exit "$status" | |
| - name: Summary | |
| env: | |
| URL: ${{ steps.slug.outputs.url }} | |
| run: | | |
| { | |
| echo "### Preview: $URL" | |
| echo | |
| echo "Built from \`${GITHUB_SHA::7}\`. It stops 90 minutes after this deploy; push again or re-run this workflow to restart it." | |
| echo "Discord sign-in is off on previews." | |
| } >> "$GITHUB_STEP_SUMMARY" | |
| teardown: | |
| if: github.event_name == 'delete' && github.event.ref_type == 'branch' | |
| runs-on: ubuntu-24.04 | |
| timeout-minutes: 5 | |
| steps: | |
| - uses: actions/checkout@v7 | |
| with: | |
| persist-credentials: false | |
| - id: slug | |
| # A branch that could not have a preview has nothing to remove. | |
| continue-on-error: true | |
| env: | |
| BRANCH: ${{ github.event.ref }} | |
| run: python3 .github/scripts/preview_slug.py "$BRANCH" | |
| - name: Remove the preview | |
| if: steps.slug.outcome == 'success' | |
| env: | |
| SSH_USER: psdeploy | |
| SSH_KEY: ${{ secrets.PREVIEW_SSH_KEY }} | |
| SSH_HOST: ${{ secrets.PREVIEW_SSH_HOST }} | |
| SSH_KNOWN_HOSTS: ${{ secrets.PREVIEW_SSH_KNOWN_HOSTS }} | |
| SLUG: ${{ steps.slug.outputs.slug }} | |
| run: .github/scripts/server_ssh.sh down "$SLUG" |