From f509fa377a393f7767a39a03226c33deada642c4 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 15 Feb 2020 19:12:37 -0600 Subject: [PATCH] break migrate into Jobs --- server/azure-pipelines.migrate.yaml | 293 +++++++++++++++------------- 1 file changed, 160 insertions(+), 133 deletions(-) diff --git a/server/azure-pipelines.migrate.yaml b/server/azure-pipelines.migrate.yaml index 64a2d4bc0..82653141d 100644 --- a/server/azure-pipelines.migrate.yaml +++ b/server/azure-pipelines.migrate.yaml @@ -1,6 +1,6 @@ # Adds an Entity Framework Core migration to a pull request that changes data in data/*.json -# TODO: sort/lint JSON and push as a part of this bot work +# TODO: sort/lint JSON # TODO: if data/* or server/src/FilterLists.Data.Migrations/* change in origin/master, re-trigger this Pipeline. trigger: none @@ -14,149 +14,176 @@ pr: pool: vmImage: ubuntu-latest -steps: - - checkout: none +jobs: + - job: IsMigrationRequired + displayName: is migration required + steps: + - checkout: none - # TODO: switch to using Azure Pipelines Repository Resource to use GitHub service connection rather than PAT - - bash: | - FORKURI=$(curl -X GET "https://api.github.com/repos/$BUILD_REPOSITORY_NAME/pulls/$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" | jq -r '.head.repo.clone_url') - git clone "$FORKURI" . - git checkout "$SYSTEM_PULLREQUEST_SOURCEBRANCH" - displayName: checkout source branch + # TODO: switch to using Azure Pipelines Repository Resource to use GitHub service connection rather than PAT + - task: Bash@3 + displayName: checkout source branch + inputs: + script: | + FORKURI=$(curl -X GET "https://api.github.com/repos/$BUILD_REPOSITORY_NAME/pulls/$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" | jq -r '.head.repo.clone_url') + git clone "$FORKURI" . + git checkout "$SYSTEM_PULLREQUEST_SOURCEBRANCH" - - bash: | - LASTAUTHOR=$(git log -n 1 --pretty=format:"%an") - LASTMSG=$(git log -n 1 --pretty=format:"%s") - if [[ $LASTAUTHOR == "$GITHUBNAME" ]] && [[ $LASTMSG == "migrate PR #$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" ]] ; then - echo "Just migrated. Aborting..." - echo "##vso[task.setvariable variable=aborted;isOutput=true]true" - fi - displayName: abort if just migrated - name: abortIfJustMigrated - env: - GITHUBNAME: $(GITHUBNAME) + - task: Bash@3 + displayName: abort if just migrated + env: + GITHUBNAME: $(GITHUBNAME) + inputs: + script: | + LASTAUTHOR=$(git log -n 1 --pretty=format:"%an") + LASTMSG=$(git log -n 1 --pretty=format:"%s") + if [[ $LASTAUTHOR == "$GITHUBNAME" ]] && [[ $LASTMSG == "migrate PR #$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" ]] ; then + echo "Just migrated. Aborting..." + else + echo "##vso[task.setvariable variable=required;isOutput=true]true" + fi - - task: UseDotNet@2 - displayName: use latest dotnet sdk - inputs: - version: 3.x - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), succeeded()) - - - script: dotnet tool install -g dotnet-ef - displayName: install ef dotnet tool - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), succeeded()) - - - bash: | - git config --global user.name "$GITHUBNAME" - git config --global user.email "$GITHUBEMAIL" - displayName: git config - env: - GITHUBNAME: $(GITHUBNAME) - GITHUBEMAIL: $(GITHUBEMAIL) - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), succeeded()) - - - bash: | - MIGLIST=$(dotnet ef migrations list -p FilterLists.Data.Migrations -s FilterLists.Api) - echo "$MIGLIST" - if [[ $MIGLIST == *$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER ]] ; then - echo "A migration already exists for PR #$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER. Reverting..." - REVERTHASH=$(git log -n 1 --author="$GITHUBNAME" --grep="migrate PR #$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" --pretty=format:"%H") - git revert --no-edit "$REVERTHASH" - fi - displayName: revert any existing migration for PR - workingDirectory: server/src - env: - GITHUBNAME: $(GITHUBNAME) - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), succeeded()) - - - bash: | - git remote add upstream https://github.com/collinbarrett/FilterLists.git - git fetch upstream - git checkout master 'data/*.json' 'server/src/FilterLists.Data.Migrations/*' - CONFLICTS=$(git ls-files -u | wc -l) - if [ "$CONFLICTS" -gt 0 ] ; then - echo "Data changes conflict with upstream master. Aborting..." - exit 1 - fi - if [[ $(git status -uno --porcelain) ]] ; then - git commit -m "merge upstream data" - fi - displayName: merge upstream data - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), succeeded()) - - - script: dotnet ef migrations add $(System.PullRequest.PullRequestNumber) -p FilterLists.Data.Migrations -s FilterLists.Api + - job: AddMigration displayName: add migration - workingDirectory: server/src - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), succeeded()) + dependsOn: IsMigrationRequired + condition: and(eq(variables['IsMigrationRequired.required'], 'true'), succeeded()) + steps: + - checkout: none - - bash: | - DIFF=$(git status -s | wc -l) - echo "$DIFF file(s) changed" - if (( $DIFF != 3 )) ; then - echo "No-op migration. Effective EF migrations add/change 3 files total (.Designer.cs, .cs, and *ModelSnapshot.cs). Abandoning..." - echo "##vso[task.setvariable variable=abandoned;isOutput=true]true" - else - git add . - git commit -m "migrate PR #$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" - fi - displayName: commit or abandon migration - name: abandonNoop - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), succeeded()) + - task: UseDotNet@2 + displayName: use latest dotnet sdk + inputs: + version: 3.x - - task: DockerCompose@0 - displayName: build test-data - inputs: - dockerComposeFile: docker-compose.data.tests.yml - dockerComposeCommand: build api - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), ne(variables['abandonNoop.abandoned'], 'true'), succeeded()) + - task: CmdLine@2 + displayName: install ef dotnet tool + inputs: + script: dotnet tool install -g dotnet-ef - - task: Docker@2 - displayName: create volume test-data-results - inputs: - command: volume - arguments: create test-data-results - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), ne(variables['abandonNoop.abandoned'], 'true'), succeeded()) + - task: Bash@3 + displayName: git config + env: + GITHUBNAME: $(GITHUBNAME) + GITHUBEMAIL: $(GITHUBEMAIL) + inputs: + script: | + git config --global user.name "$GITHUBNAME" + git config --global user.email "$GITHUBEMAIL" - - task: DockerCompose@0 - displayName: up test-data db - inputs: - dockerComposeFile: docker-compose.data.tests.yml - dockerComposeCommand: up -d mariadb - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), ne(variables['abandonNoop.abandoned'], 'true'), succeeded()) + - task: Bash@3 + displayName: revert any existing migration for PR + env: + GITHUBNAME: $(GITHUBNAME) + inputs: + workingDirectory: server/src + script: | + MIGLIST=$(dotnet ef migrations list -p FilterLists.Data.Migrations -s FilterLists.Api) + echo "$MIGLIST" + if [[ $MIGLIST == *$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER ]] ; then + echo "A migration already exists for PR #$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER. Reverting..." + REVERTHASH=$(git log -n 1 --author="$GITHUBNAME" --grep="migrate PR #$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" --pretty=format:"%H") + git revert --no-edit "$REVERTHASH" + fi - - task: DockerCompose@0 - displayName: run test-data - inputs: - dockerComposeFile: docker-compose.data.tests.yml - dockerComposeCommand: run api - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), ne(variables['abandonNoop.abandoned'], 'true'), succeeded()) + - task: Bash@3 + displayName: merge upstream data + inputs: + script: | + git remote add upstream https://github.com/collinbarrett/FilterLists.git + git fetch upstream + git checkout master 'data/*.json' 'server/src/FilterLists.Data.Migrations/*' + CONFLICTS=$(git ls-files -u | wc -l) + if [ "$CONFLICTS" -gt 0 ] ; then + echo "Data changes conflict with upstream master. Aborting..." + exit 1 + fi + if [[ $(git status -uno --porcelain) ]] ; then + git commit -m "merge upstream data" + fi - - task: Docker@2 - displayName: create container test-data-results - inputs: - command: container - arguments: create --name test-data-results -v test-data-results:/results hello-world - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), ne(variables['abandonNoop.abandoned'], 'true'), succeededOrFailed()) + - task: CmdLine@2 + displayName: add migration + inputs: + workingDirectory: server/src + script: dotnet ef migrations add $(System.PullRequest.PullRequestNumber) -p FilterLists.Data.Migrations -s FilterLists.Api - - task: Docker@2 - displayName: copy out test-data results - inputs: - command: cp - arguments: test-data-results:/results $(System.DefaultWorkingDirectory) - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), ne(variables['abandonNoop.abandoned'], 'true'), succeededOrFailed()) + - task: Bash@3 + displayName: commit or abandon migration + inputs: + script: | + DIFF=$(git status -s | wc -l) + echo "$DIFF file(s) changed" + if (( $DIFF != 3 )) ; then + echo "No-op migration. Effective EF migrations add/change 3 files total (.Designer.cs, .cs, and *ModelSnapshot.cs). Abandoning..." + else + git add . + git commit -m "migrate PR #$SYSTEM_PULLREQUEST_PULLREQUESTNUMBER" + echo "##vso[task.setvariable variable=added;isOutput=true]true" + fi - - task: PublishTestResults@2 - displayName: publish test results - inputs: - testResultsFormat: VSTest - testResultsFiles: "**/*.trx" - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), ne(variables['abandonNoop.abandoned'], 'true'), succeededOrFailed()) + - job: TestMigration + displayName: test migration + dependsOn: AddMigration + condition: and(eq(variables['AddMigration.added'], 'true'), succeeded()) + steps: + - checkout: none - - bash: | - git config --global credential.helper store - echo "https://$GITHUBPAT:x-oauth-basic@github.com" >> ~/.git-credentials - git push origin "$SYSTEM_PULLREQUEST_SOURCEBRANCH" + - task: DockerCompose@0 + displayName: build test-data + inputs: + dockerComposeFile: docker-compose.data.tests.yml + dockerComposeCommand: build api + + - task: Docker@2 + displayName: create volume test-data-results + inputs: + command: volume + arguments: create test-data-results + + - task: DockerCompose@0 + displayName: up test-data db + inputs: + dockerComposeFile: docker-compose.data.tests.yml + dockerComposeCommand: up -d mariadb + + - task: DockerCompose@0 + displayName: run test-data + inputs: + dockerComposeFile: docker-compose.data.tests.yml + dockerComposeCommand: run api + + - task: Docker@2 + displayName: create container test-data-results + inputs: + command: container + arguments: create --name test-data-results -v test-data-results:/results hello-world + condition: succeededOrFailed() + + - task: Docker@2 + displayName: copy out test-data results + inputs: + command: cp + arguments: test-data-results:/results $(System.DefaultWorkingDirectory) + condition: succeededOrFailed() + + - task: PublishTestResults@2 + displayName: publish test results + inputs: + testResultsFormat: VSTest + testResultsFiles: "**/*.trx" + condition: succeededOrFailed() + + - job: Push displayName: push - env: - GITHUBPAT: $(GITHUBPAT) - condition: and(ne(variables['abortIfJustMigrated.aborted'], 'true'), succeeded()) + steps: + - checkout: none + + - task: Bash@3 + displayName: push + env: + GITHUBPAT: $(GITHUBPAT) + inputs: + script: | + git config --global credential.helper store + echo "https://$GITHUBPAT:x-oauth-basic@github.com" >> ~/.git-credentials + git push origin "$SYSTEM_PULLREQUEST_SOURCEBRANCH"