feat(dir): migrate Apply Data Change action

This commit is contained in:
Collin Barrett 2024-06-10 15:48:22 -05:00
parent 89f15c99f0
commit 3abd616c9e
4 changed files with 149 additions and 18 deletions

2
.github/README.md vendored
View file

@ -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 `<MigrationName>` with a meaningful name.
```bash
dotnet ef migrations add <MigrationName> --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 <MigrationName> -p FilterLists.Directory.Infrastructure.Migrations -s FilterLists.Directory.Api
```
## Deploying to Production

116
.github/workflows/directory-migrate.yml vendored Normal file
View file

@ -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

View file

@ -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 }}

0
services/Directory/data/lint.sh Normal file → Executable file
View file