From 386c3e14d0d6d94983e9ee125ff22298e3d1399d Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Mon, 28 Sep 2020 16:43:35 -0700 Subject: [PATCH] Change workflow to use upstream branches --- eng/common/scripts/Queue-Pipeline.ps1 | 43 ++++++++ eng/common/scripts/Submit-PullRequest.ps1 | 2 +- eng/common/scripts/logging.ps1 | 4 +- eng/pipelines/eng-common-sync.yml | 101 ++++++++++++++---- .../templates/steps/sync-directory.yml | 30 +++--- 5 files changed, 140 insertions(+), 40 deletions(-) create mode 100644 eng/common/scripts/Queue-Pipeline.ps1 diff --git a/eng/common/scripts/Queue-Pipeline.ps1 b/eng/common/scripts/Queue-Pipeline.ps1 new file mode 100644 index 000000000000..b97a7aa65d4c --- /dev/null +++ b/eng/common/scripts/Queue-Pipeline.ps1 @@ -0,0 +1,43 @@ +[CmdletBinding(SupportsShouldProcess = $true)] +param( + [Parameter(Mandatory = $true)] + [string]$Organization, + + [Parameter(Mandatory = $true)] + [string]$Project, + + [Parameter(Mandatory = $true)] + [string]$SourceBranch, + + [Parameter(Mandatory = $true)] + [int]$DefinitionId, + + [Parameter(Mandatory = $true)] + [string]$AuthToken +) + +. "${PSScriptRoot}\logging.ps1" + +$encodedAuthToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes([string]::Format("{0}:{1}", "", $AuthToken))) +$headers = @{ + Authorization = "Basic $encodedAuthToken" +} + +$apiUrl = "https://dev.azure.com/$Organization/$Project/_apis/build/builds?api-version=6.0" + +$body = @{ + sourceBranch = $SourceBranch + definition = @{ id = $DefinitionId } +} + +Write-Verbose ($body | ConvertTo-Json) + +try { + $resp = Invoke-RestMethod -Method POST -Headers $headers $apiUrl -Body ($body | ConvertTo-Json) -ContentType application/json +} +catch { + Write-Error "Invoke-RestMethod $apiUrl failed with exception:`n$_" + exit 1 +} + +LogDebug "Pipeline [ $($resp.definition.name) ] queued at [ $($resp._links.web.href) ]" \ No newline at end of file diff --git a/eng/common/scripts/Submit-PullRequest.ps1 b/eng/common/scripts/Submit-PullRequest.ps1 index 7f3f0a544e9a..6bf26233b7eb 100644 --- a/eng/common/scripts/Submit-PullRequest.ps1 +++ b/eng/common/scripts/Submit-PullRequest.ps1 @@ -31,7 +31,7 @@ param( [string]$RepoName, [Parameter(Mandatory = $true)] - [string]$BaseBranch, + [string]$BaseBranch = "master", [Parameter(Mandatory = $true)] [string]$PROwner, diff --git a/eng/common/scripts/logging.ps1 b/eng/common/scripts/logging.ps1 index 19a6fa5bc36d..f5d01664047c 100644 --- a/eng/common/scripts/logging.ps1 +++ b/eng/common/scripts/logging.ps1 @@ -31,10 +31,10 @@ function LogDebug { if ($isDevOpsRun) { - Write-Host "##vso[task.LogIssue type=debug;]$args" + Write-Host "[debug]$args" } else { - Write-Debug "$args" + Write-Debug "$args" -Debug } } \ No newline at end of file diff --git a/eng/pipelines/eng-common-sync.yml b/eng/pipelines/eng-common-sync.yml index 7e6f458524f3..4411d79d327f 100644 --- a/eng/pipelines/eng-common-sync.yml +++ b/eng/pipelines/eng-common-sync.yml @@ -9,6 +9,18 @@ parameters: - name: DirectoryToSync type: string default: eng/common +- name: Repos + type: object + default: + - azure-sdk-for-android + - azure-sdk-for-c + - azure-sdk-for-cpp + - azure-sdk-for-go + - azure-sdk-for-ios + - azure-sdk-for-java + - azure-sdk-for-js + - azure-sdk-for-net + - azure-sdk-for-python trigger: none @@ -58,11 +70,7 @@ stages: CommitMessage: "Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools repository for Tools PR $(System.PullRequest.PullRequestNumber)" DirectoryToSync: ${{ parameters.DirectoryToSync }} PRBranchName: "sync-${{ parameters.DirectoryToSync }}-$(System.PullRequest.SourceBranch)-$(System.PullRequest.PullRequestNumber)" - BaseBranchName: $(system.pullRequest.targetBranch) - PRTitle: "Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools for PR $(System.PullRequest.PullRequestNumber)" - PRBody: > - Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools for PR https://github.com/Azure/azure-sdk-tools/pull/$(System.PullRequest.PullRequestNumber)
- See [eng/common workflow](https://github.com/Azure/azure-sdk-tools/blob/master/eng/common/README.md#workflow) + BaseBranchName: $(system.pullRequest.targetBranch) SkipCheckingForChanges: true ${{ if ne(variables['Build.Reason'], 'PullRequest') }}: CommitMessage: Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools repository @@ -72,29 +80,76 @@ stages: PRBody: > Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools repository.
See [eng/common workflow](https://github.com/Azure/azure-sdk-tools/blob/master/eng/common/README.md#workflow) - PRDataArtifactPath: $(Build.ArtifactStagingDirectory)/${{ parameters.PRDataFileName }} - Repos: - - azure-sdk-for-android - - azure-sdk-for-c - - azure-sdk-for-cpp - - azure-sdk-for-go - - azure-sdk-for-ios - - azure-sdk-for-java - - azure-sdk-for-js - - azure-sdk-for-net - - azure-sdk-for-python - - - task: PublishPipelineArtifact@1 - condition: succeeded() - inputs: - artifactName: ${{ parameters.ArtifactName }} - path: $(Build.ArtifactStagingDirectory)/${{ parameters.PRDataFileName }} + Repos: ${{ parameters.Repos }} + + - stage: CreateSyncPRs + jobs: + - deployment: CreateSyncPullRequests + displayName: Create Sync Pull Requests + environment: github + + pool: + vmImage: windows-2019 + + strategy: + runOnce: + deploy: + steps: + - checkout: self + + - pwsh: | + New-Item -Path $(Build.ArtifactStagingDirectory)/${{ parameters.PRDataFileName }} -ItemType File + displayName: Create PRData Artifact + + - ${{ each repo in parameters.Repos }}: + - task: PowerShell@2 + displayName: Create pull request + condition: succeeded() + inputs: + pwsh: true + workingDirectory: ${{ parameters.WorkingDirectory }} + filePath: /eng/common/scripts/Submit-PullRequest.ps1 + ${{ if eq(variables['Build.Reason'], 'PullRequest') }}: + arguments: > + -RepoOwner "Azure" + -RepoName "${{ repo }}" + -BaseBranch $(system.pullRequest.targetBranch) + -PROwner "Azure" + -AuthToken "$(azuresdk-github-pat)" + -PRTitle "Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools for PR $(System.PullRequest.PullRequestNumber)" + -PRBody "Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools for PR https://github.com/Azure/azure-sdk-tools/pull/$(System.PullRequest.PullRequestNumber) + See [eng/common workflow](https://github.com/Azure/azure-sdk-tools/blob/master/eng/common/README.md#workflow)" + -PRLabels "Central-EngSys" + ${{ if ne(variables['Build.Reason'], 'PullRequest') }}: + arguments: > + -RepoOwner "Azure" + -RepoName "${{ repo }}" + -BaseBranch $(system.pullRequest.targetBranch) + -PROwner "Azure" + -AuthToken "$(azuresdk-github-pat)" + -PRTitle "Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools for PR $(System.PullRequest.PullRequestNumber)" + -PRBody "Sync ${{ parameters.DirectoryToSync }} directory with azure-sdk-tools for PR https://github.com/Azure/azure-sdk-tools/pull/$(System.PullRequest.PullRequestNumber) + See [eng/common workflow](https://github.com/Azure/azure-sdk-tools/blob/master/eng/common/README.md#workflow)" + -PRLabels "Central-EngSys" + + - pwsh: | + $PRData = "Azure;${{ repo }};"${{ parameters.PRBranchName }}" + Add-Content -Path "$(Build.ArtifactStagingDirectory)/${{ parameters.PRDataFileName }}" -Value $PRData + displayName: Write Sync PR Data to Artifact File + condition: succeeded() + + - task: PublishPipelineArtifact@1 + condition: succeeded() + displayName: Publish ${{ parameters.PRDataFileName }} + inputs: + artifactName: ${{ parameters.ArtifactName }} + path: $(Build.ArtifactStagingDirectory)/${{ parameters.PRDataFileName }} - stage: VerifyAndMerge jobs: - deployment: VerifyandMergeSyncPrs displayName: Verify and Merge Sync PRs - environment: github + environment: githubmerges pool: vmImage: windows-2019 diff --git a/eng/pipelines/templates/steps/sync-directory.yml b/eng/pipelines/templates/steps/sync-directory.yml index 92681e964b64..ea0859067dd4 100644 --- a/eng/pipelines/templates/steps/sync-directory.yml +++ b/eng/pipelines/templates/steps/sync-directory.yml @@ -4,19 +4,13 @@ parameters: CommitMessage: commit-message-not-set PRBranchName: branch-name-not-set BaseBranchName: master - PRTitle: pr-title-not-set - PRBody: pr-body-not-set - PRDataArtifactPath: pr-data-artifact-path-not-set + Sync: pr-data-artifact-path-not-set SkipCheckingForChanges: false ScriptDirectory: eng/common/scripts PROwner: Azure - PushArgs: + PushArgs: -f steps: - - pwsh: | - New-Item -Path ${{ parameters.PRDataArtifactPath }} -ItemType File - displayName: Create PRData Artifact - - ${{ each repo in parameters.Repos }}: - pwsh: | Set-PsDebug -Trace 1 @@ -57,7 +51,7 @@ steps: condition: and(succeeded(), eq(variables['HasChanges'], 'true')) inputs: pwsh: true - workingDirectory: ${{ parameters.WorkingDirectory }} + workingDirectory: $(System.DefaultWorkingDirectory)/${{ repo }} filePath: ${{ parameters.ScriptDirectory }}/git-branch-push.ps1 arguments: > -PRBranchName "${{ parameters.PRBranchName }}" @@ -66,8 +60,16 @@ steps: -PushArgs "${{ parameters.PushArgs }}" -SkipCommit "${{parameters.SkipCheckingForChanges}}" - - pwsh: | - $PRData = "Azure;${{ repo }};$(Submitted.PullRequest.Number)" - Add-Content -Path ${{ parameters.PRDataArtifactPath }} -Value $PRData - displayName: Write Sync PR Data to Artifact File - condition: and(succeeded(), eq(variables['HasChanges'], 'true')) + - task: PowerShell@2 + displayName: Queue test pipeline + condition: and(succeeded(), ne(variables['${{repo}}-template-definition-id'], '')) + inputs: + pwsh: true + workingDirectory: ${{ parameters.WorkingDirectory }} + filePath: ${{ parameters.ScriptDirectory }}/Queue-Pipeline.ps1 + arguments: > + -Organization "azure-sdk" + -Project "internal" + -SourceBranch "${{ parameters.PRBranchName }}" + -DefinitionId "$(${{repo}}-template-definition-id)" + -AuthToken $(azuresdk-azure-sdk-devops-build-queuing-pat)