From 3abd616c9ec024b25ca29d8164a59102c9d6fba8 Mon Sep 17 00:00:00 2001 From: Collin Barrett Date: Mon, 10 Jun 2024 15:48:22 -0500 Subject: [PATCH] =?UTF-8?q?feat(dir):=20=E2=9C=A8=20migrate=20Apply=20Data?= =?UTF-8?q?=20Change=20action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/README.md | 2 +- .github/workflows/directory-migrate.yml | 116 ++++++++++++++++++++++++ .github/workflows/directory.yml | 49 ++++++---- services/Directory/data/lint.sh | 0 4 files changed, 149 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/directory-migrate.yml mode change 100644 => 100755 services/Directory/data/lint.sh diff --git a/.github/README.md b/.github/README.md index d73bf4d95..373b185aa 100644 --- a/.github/README.md +++ b/.github/README.md @@ -81,7 +81,7 @@ Modify the database schema or seed data by adding an EF Core migration. 3. Execute the command below in the `services/Directory` directory replacing `` with a meaningful name. ```bash -dotnet ef migrations add --project FilterLists.Directory.Infrastructure.Migrations/FilterLists.Directory.Infrastructure.Migrations.csproj --startup-project FilterLists.Directory.Infrastructure.MigrationService/FilterLists.Directory.Infrastructure.MigrationService.csproj +dotnet ef migrations add -p FilterLists.Directory.Infrastructure.Migrations -s FilterLists.Directory.Api ``` ## Deploying to Production diff --git a/.github/workflows/directory-migrate.yml b/.github/workflows/directory-migrate.yml new file mode 100644 index 000000000..749a005c4 --- /dev/null +++ b/.github/workflows/directory-migrate.yml @@ -0,0 +1,116 @@ +name: Directory API - Apply Data Changes + +on: + pull_request: + branches: + - main + - aspire # TODO: rm after testing passes + paths: + - .github/workflows/directory-migrate.yml + - services/Directory/data/** + - services/Directory/FilterLists.Directory.Infrastructure.Migrations/** + +jobs: + add_migration: + name: Add EF Core Migration + + runs-on: ubuntu-latest + + permissions: + contents: write + + env: + USER_NAME: github-actions[bot] + USER_EMAIL: github-actions[bot]@users.noreply.github.com + LINT_MESSAGE: 'chore(dir-data): 🎨 lint PR #${{ github.event.number }}' + MIGRATION_MESSAGE: 'chore(dir-data): ♻️ migrate PR #${{ github.event.number }}' + TEST_MSSQL_IMAGE: mcr.microsoft.com/mssql/server:2022-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Lint + run: ./lint.sh + working-directory: ./services/Directory/data + + - name: git config + run: | + git config --global user.name "$USER_NAME" + git config --global user.email "$USER_EMAIL" + + - name: git commit + run: | + LINT_PENDING_CHANGES=$(git status -s -- .) + if [ -n "$LINT_PENDING_CHANGES" ]; then + git commit -am "$LINT_MESSAGE" + fi + + - name: Sync with Upstream + run: | + git remote add upstream https://github.com/${{ github.repository }}.git + git fetch upstream + git merge upstream/${{ github.event.pull_request.base.ref }} + + - name: Revert Any Previous Migration for PR + run: | + PREVIOUS_MIGRATION_HASH=$(git log --grep="$MIGRATION_MESSAGE" --pretty=format:"%H" -n 1) + if [ -n "$PREVIOUS_MIGRATION_HASH" ]; then + git revert --no-edit $PREVIOUS_MIGRATION_HASH + fi + + - name: Install dotnet-ef + run: dotnet tool install --global dotnet-ef + + - name: Add Migration + run: > + dotnet ef migrations add ${{ github.event.number }} + -p FilterLists.Directory.Infrastructure.Migrations + -s FilterLists.Directory.Api + working-directory: ./services/Directory + + - name: Commit Effective Migration + run: | + MIGRATION_CHANGED_FILE_COUNT=$(git status -s -- . | wc -l) + if [ "$MIGRATION_CHANGED_FILE_COUNT" -eq 3 ]; then + git add . + git commit -m "$MIGRATION_MESSAGE" + else + git clean -fd + fi + + - name: Run Test SQL Server + run: > + docker run --name sql-server + -e 'ACCEPT_EULA=Y' + -e 'SA_PASSWORD=Your_password123' + -p 1433:1433 + -d $TEST_MSSQL_IMAGE + + - name: Wait for Test SQL Server + run: | + echo "Waiting for SQL Server to start..." + for i in {1..30}; do + if docker exec sql-server /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Your_password123 -Q "SELECT 1" &> /dev/null; then + echo "SQL Server is up and running." + exit 0 + fi + echo "Waiting..." + sleep 2 + done + echo "SQL Server did not start in time." + exit 1 + + - name: Test Migrations + run: > + dotnet ef database update + --connection "Server=localhost,1433;Database=DirectoryDb;User Id=sa;Password=Your_password123;TrustServerCertificate=True;" + -p FilterLists.Directory.Infrastructure.Migrations + -s FilterLists.Directory.Api + working-directory: ./services/Directory + + - name: git push + run: git push \ No newline at end of file diff --git a/.github/workflows/directory.yml b/.github/workflows/directory.yml index 88639dd00..15c03754c 100644 --- a/.github/workflows/directory.yml +++ b/.github/workflows/directory.yml @@ -1,4 +1,4 @@ -name: Directory API CI/CD +name: Directory API - Build & Deploy on: push: @@ -6,30 +6,35 @@ on: - main - aspire # TODO: rm when merging aspire to main paths: - - '.github/workflows/directory.yml' - - 'services/Directory/**' - - 'services/FilterLists.ServiceDefaults/**' + - .github/workflows/directory.yml + - services/Directory/** + - services/FilterLists.ServiceDefaults/** # TODO: uncomment when merging aspire to main # pull_request: # branches: # - main # paths: - # - '.github/workflows/directory.yml' - # - 'services/Directory/**' - # - 'services/FilterLists.ServiceDefaults/**' + # - .github/workflows/directory.yml + # - services/Directory/** + # - services/FilterLists.ServiceDefaults/** env: CONTAINER_REGISTRY: ghcr.io CONTAINER_REPOSITORY: collinbarrett/filterlists-directory-api CONTAINER_IMAGE_TAG_UNIQUE: ${{ github.run_id }} - CONTAINER_IMAGE_TAG_STABLE: latest-aspire # TODO: drop '-aspire' when merging aspire to main - AZURE_WEBAPP_NAME: app-filterlists-directory-prod jobs: build: + name: Build + runs-on: ubuntu-latest + permissions: packages: write + + env: + CONTAINER_IMAGE_TAG_STABLE: latest-aspire # TODO: drop '-aspire' when merging aspire to main + steps: - name: Checkout uses: actions/checkout@v4 @@ -45,26 +50,36 @@ jobs: run: dotnet build -c Release working-directory: ./services/Directory/FilterLists.Directory.Api + # TODO: run automated tests + - name: Publish & Push if: github.event_name == 'push' run: > - dotnet publish --sc \ - -p:PublishProfile=DefaultContainer \ - -p:ContainerRegistry=${{ env.CONTAINER_REGISTRY }} \ - -p:ContainerRepository=${{ env.CONTAINER_REPOSITORY }} \ - -p:ContainerImageTags='"${{ env.CONTAINER_IMAGE_TAG_UNIQUE }};${{ env.CONTAINER_IMAGE_TAG_STABLE }}"' + dotnet publish --sc + -p:PublishProfile=DefaultContainer + -p:ContainerRegistry=${{ env.CONTAINER_REGISTRY }} + -p:ContainerRepository=${{ env.CONTAINER_REPOSITORY }} + -p:ContainerImageTags='"${{ env.CONTAINER_IMAGE_TAG_UNIQUE }};${{ env.CONTAINER_IMAGE_TAG_STABLE }}"' working-directory: ./services/Directory/FilterLists.Directory.Api deploy: + name: Deploy + runs-on: ubuntu-latest + needs: build + if: github.event_name == 'push' + + env: + AZURE_WEBAPP_NAME: app-filterlists-directory-prod + environment: name: production - url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}/swagger + url: https://api.filterlists.com/swagger/index.html + steps: - - name: Deploy to Azure Web App - id: deploy-to-webapp + - name: Deploy to Azure uses: azure/webapps-deploy@v3 with: app-name: ${{ env.AZURE_WEBAPP_NAME }} diff --git a/services/Directory/data/lint.sh b/services/Directory/data/lint.sh old mode 100644 new mode 100755