Deploy production Dashboard #27
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 production Dashboard | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| tag: | |
| description: 'Tag to deploy' | |
| required: true | |
| default: 'main' | |
| jobs: | |
| start-notification: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Discord notification | |
| env: | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| uses: Ilshidur/action-discord@master | |
| with: | |
| args: "Deploying production Dashboard with tag `${{ github.event.inputs.tag }}`" | |
| check-migrations: | |
| needs: start-notification | |
| runs-on: ubuntu-latest | |
| outputs: | |
| has_new_migrations: ${{ steps.check.outputs.has_new_migrations }} | |
| migration_list: ${{ steps.check.outputs.migration_list }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check for new Django migrations | |
| id: check | |
| run: | | |
| # Compare against the previous tag; fall back to HEAD~1 if no tags exist | |
| PREV_TAG=$(git describe --abbrev=0 --tags HEAD^ 2>/dev/null || echo "") | |
| if [ -z "$PREV_TAG" ]; then | |
| echo "No previous tag found, comparing against HEAD~1" | |
| COMPARE_REF="HEAD~1" | |
| else | |
| echo "Comparing migrations against tag: $PREV_TAG" | |
| COMPARE_REF="$PREV_TAG" | |
| fi | |
| NEW_MIGRATIONS=$(git diff --name-only "$COMPARE_REF" HEAD -- '**/migrations/*.py' \ | |
| | grep -v '__init__.py' || true) | |
| if [ -n "$NEW_MIGRATIONS" ]; then | |
| echo "has_new_migrations=true" >> $GITHUB_OUTPUT | |
| # Encode newlines for the output so it can be passed between jobs | |
| ENCODED=$(echo "$NEW_MIGRATIONS" | tr '\n' '|') | |
| echo "migration_list=$ENCODED" >> $GITHUB_OUTPUT | |
| echo "New migration files detected:" | |
| echo "$NEW_MIGRATIONS" | |
| else | |
| echo "has_new_migrations=false" >> $GITHUB_OUTPUT | |
| echo "migration_list=" >> $GITHUB_OUTPUT | |
| echo "No new migration files found" | |
| fi | |
| notify-new-migrations: | |
| needs: check-migrations | |
| if: needs.check-migrations.outputs.has_new_migrations == 'true' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Discord notification - new migrations detected | |
| env: | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| uses: Ilshidur/action-discord@master | |
| with: | |
| args: "New Django migrations detected in production deployment of `${{ github.event.inputs.tag }}`:\n`${{ needs.check-migrations.outputs.migration_list }}`" | |
| notify-no-migrations: | |
| needs: check-migrations | |
| if: needs.check-migrations.outputs.has_new_migrations == 'false' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Discord notification - no migrations detected | |
| env: | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| uses: Ilshidur/action-discord@master | |
| with: | |
| args: "No new Django migrations detected in production deployment of `${{ github.event.inputs.tag }}`" | |
| deploy-production: | |
| needs: start-notification | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Configure host authenticity | |
| run: | | |
| mkdir -p ~/.ssh/ && chmod 700 ~/.ssh/ | |
| touch ~/.ssh/known_hosts && chmod 600 ~/.ssh/known_hosts | |
| echo "$SSH_HOSTKEY" > ~/.ssh/known_hosts | |
| env: | |
| SSH_HOSTKEY: ${{ secrets.STAGING_HOSTKEY }} | |
| - name: Deploy production | |
| run: | | |
| eval $(ssh-agent -s) | |
| echo "$SSH_KEY" | ssh-add - >/dev/null | |
| ssh "${SSH_USER}@${SSH_HOST}" " | |
| rm -rf dashboard-production && | |
| git clone --depth 1 --branch main https://github.com/kernelci/dashboard.git dashboard-production && | |
| cp ~/.env-production dashboard-production/.env && | |
| cd dashboard-production && | |
| git checkout ${GITHUB_SHA} && | |
| docker compose -f docker-compose-next.yml pull && | |
| docker compose -f docker-compose-next.yml up -d | |
| " | |
| env: | |
| SSH_USER: ${{ secrets.STAGING_USER }} | |
| SSH_HOST: ${{ secrets.STAGING_HOST }} | |
| SSH_KEY: ${{ secrets.STAGING_KEY }} | |
| - name: Discord notification on success | |
| if: success() | |
| env: | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| uses: Ilshidur/action-discord@master | |
| with: | |
| args: "Production Dashboard deployment successful with tag `${{ github.event.inputs.tag }}`" | |
| - name: Discord notification on failure | |
| if: failure() | |
| env: | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| uses: Ilshidur/action-discord@master | |
| with: | |
| args: "Production Dashboard deployment failed with tag `${{ github.event.inputs.tag }}`" |