diff --git a/eng/common/pipelines/templates/steps/create-pull-request.yml b/eng/common/pipelines/templates/steps/create-pull-request.yml index ecce673e31a4..940d4672c867 100644 --- a/eng/common/pipelines/templates/steps/create-pull-request.yml +++ b/eng/common/pipelines/templates/steps/create-pull-request.yml @@ -23,49 +23,17 @@ parameters: OpenAsDraft: false steps: - -- pwsh: | - echo "git add -A" - git add -A - - echo "git diff --name-status --cached --exit-code" - git diff --name-status --cached --exit-code - - if ($LastExitCode -ne 0) { - echo "##vso[task.setvariable variable=HasChanges]$true" - echo "Changes detected so setting HasChanges=true" - } - else { - echo "##vso[task.setvariable variable=HasChanges]$false" - echo "No changes so skipping code push" - } - displayName: Check for changes - condition: and(succeeded(), eq(${{ parameters.SkipCheckingForChanges }}, false)) - workingDirectory: ${{ parameters.WorkingDirectory }} - ignoreLASTEXITCODE: true - -- pwsh: | - # Remove the repo owner from the front of the repo name if it exists there - $repoName = "${{ parameters.RepoName }}" -replace "^${{ parameters.RepoOwner }}/", "" - echo "##vso[task.setvariable variable=RepoNameWithoutOwner]$repoName" - echo "RepoName = $repoName" - displayName: Remove Repo Owner from Repo Name - condition: succeeded() - workingDirectory: ${{ parameters.WorkingDirectory }} - -- task: PowerShell@2 - displayName: Push changes - condition: and(succeeded(), eq(variables['HasChanges'], 'true')) - inputs: - pwsh: true - workingDirectory: ${{ parameters.WorkingDirectory }} - filePath: ${{ parameters.ScriptDirectory }}/git-branch-push.ps1 - arguments: > - -PRBranchName "${{ parameters.PRBranchName }}" - -CommitMsg "${{ parameters.CommitMsg }}" - -GitUrl "https://$(azuresdk-github-pat)@github.com/${{ parameters.PROwner }}/$(RepoNameWithoutOwner).git" - -PushArgs "${{ parameters.PushArgs }}" - -SkipCommit $${{ parameters.SkipCheckingForChanges }} +- template: /eng/common/pipelines/templates/steps/git-push-changes.yml + parameters: + BaseRepoBranch: ${{ parameters.PRBranchName }} + BaseRepoOwner: ${{ parameters.PROwner }} + CommitMsg: ${{ parameters.CommitMsg }} + TargetRepoOwner: ${{ parameters.RepoOwner }} + TargetRepoName: ${{ parameters.RepoName }} + PushArgs: ${{ parameters.PushArgs }} + WorkingDirectory: ${{ parameters.WorkingDirectory }} + ScriptDirectory: ${{ parameters.ScriptDirectory }} + SkipCheckingForChanges: ${{ parameters.SkipCheckingForChanges }} - task: PowerShell@2 displayName: Create pull request diff --git a/eng/common/pipelines/templates/steps/docs-metadata-release.yml b/eng/common/pipelines/templates/steps/docs-metadata-release.yml index a01b197035db..9756ad30a151 100644 --- a/eng/common/pipelines/templates/steps/docs-metadata-release.yml +++ b/eng/common/pipelines/templates/steps/docs-metadata-release.yml @@ -9,7 +9,6 @@ parameters: TargetDocRepoName: '' TargetDocRepoOwner: '' PRBranchName: 'master-rdme' - SourceBranchName: 'master' PRLabels: 'auto-merge' ArtifactName: '' Language: '' @@ -34,18 +33,11 @@ steps: - pwsh: | git clone https://github.com/${{ parameters.TargetDocRepoOwner }}/${{ parameters.TargetDocRepoName }} ${{ parameters.WorkingDirectory }}/repo - - try { - Push-Location ${{ parameters.WorkingDirectory }}/repo - - Write-Host "git checkout ${{ parameters.SourceBranchName }}" - git checkout ${{ parameters.SourceBranchName }} - } finally { - Pop-Location - } displayName: Clone Documentation Repository ignoreLASTEXITCODE: false - +- template: /eng/common/pipelines/templates/steps/set-default-branch.yml + parameters: + WorkingDirectory: ${{ parameters.WorkingDirectory }}/repo - task: PowerShell@2 displayName: 'Apply Documentation Updates From Artifact' inputs: @@ -82,25 +74,19 @@ steps: env: GH_TOKEN: $(azuresdk-github-pat) -- template: /eng/common/pipelines/templates/steps/create-pull-request.yml +- template: /eng/common/pipelines/templates/steps/git-push-changes.yml parameters: - RepoName: ${{ parameters.TargetDocRepoName }} - RepoOwner: ${{ parameters.TargetDocRepoOwner }} - PRBranchName: ${{ parameters.PRBranchName }} + BaseRepoBranch: $(DefaultBranch) + BaseRepoOwner: ${{ parameters.TargetDocRepoOwner }} CommitMsg: "Update docs metadata and targeting for release of ${{ parameters.ArtifactName }}" - PRTitle: "Docs.MS Release Updates for ${{ parameters.ArtifactName }}" - BaseBranchName: ${{ parameters.SourceBranchName }} + TargetRepoName: ${{ parameters.TargetDocRepoName }} + TargetRepoOwner: ${{ parameters.TargetDocRepoOwner }} WorkingDirectory: ${{ parameters.WorkingDirectory }}/repo ScriptDirectory: ${{ parameters.WorkingDirectory }}/${{ parameters.ScriptDirectory }} - GHReviewersVariable: ${{ parameters.GHReviewersVariable }} - GHTeamReviewersVariable: ${{ parameters.GHTeamReviewersVariable }} - PRLabels: ${{ parameters.PRLabels }} - CloseAfterOpenForTesting: ${{ parameters.CloseAfterOpenForTesting }} - ${{if ne( parameters['OnboardingBranch'], '')}}: - pwsh: | Push-Location ${{ parameters.WorkingDirectory }}/repo - git reset --hard HEAD git remote rm azure-sdk-fork git checkout ${{ parameters.OnboardingBranch}} @@ -123,17 +109,12 @@ steps: pwsh: true env: GH_TOKEN: $(azuresdk-github-pat) - - - template: /eng/common/pipelines/templates/steps/create-pull-request.yml + - template: /eng/common/pipelines/templates/steps/git-push-changes.yml parameters: - RepoName: ${{ parameters.TargetDocRepoName }} - RepoOwner: ${{ parameters.TargetDocRepoOwner }} - PRBranchName: ${{ parameters.PRBranchName }}-ci - CommitMsg: "CI Update for release of ${{ parameters.ArtifactName }}" - PRTitle: "Docs.MS CI Updates for ${{ parameters.ArtifactName }}" - BaseBranchName: ${{ parameters.OnboardingBranch }} + BaseRepoBranch: ${{ parameters.OnboardingBranch}} + BaseRepoOwner: ${{ parameters.TargetDocRepoOwner }} + CommitMsg: "Update docs metadata and targeting for release of ${{ parameters.ArtifactName }}" + TargetRepoName: ${{ parameters.TargetDocRepoName }} + TargetRepoOwner: ${{ parameters.TargetDocRepoOwner }} WorkingDirectory: ${{ parameters.WorkingDirectory }}/repo ScriptDirectory: ${{ parameters.WorkingDirectory }}/${{ parameters.ScriptDirectory }} - GHReviewersVariable: ${{ parameters.GHReviewersVariable }} - GHTeamReviewersVariable: ${{ parameters.GHTeamReviewersVariable }} - CloseAfterOpenForTesting: ${{ parameters.CloseAfterOpenForTesting }} diff --git a/eng/common/pipelines/templates/steps/git-push-changes.yml b/eng/common/pipelines/templates/steps/git-push-changes.yml new file mode 100644 index 000000000000..a922b203a9b1 --- /dev/null +++ b/eng/common/pipelines/templates/steps/git-push-changes.yml @@ -0,0 +1,54 @@ +parameters: + BaseRepoBranch: not-specified + BaseRepoOwner: azure-sdk + CommitMsg: not-specified + TargetRepoOwner: Azure + TargetRepoName: $(Build.Repository.Name) + PushArgs: + WorkingDirectory: $(System.DefaultWorkingDirectory)' + ScriptDirectory: eng/common/scripts + SkipCheckingForChanges: false + +steps: +- pwsh: | + echo "git add -A" + git add -A + + echo "git diff --name-status --cached --exit-code" + git diff --name-status --cached --exit-code + + if ($LastExitCode -ne 0) { + echo "##vso[task.setvariable variable=HasChanges]$true" + echo "Changes detected so setting HasChanges=true" + } + else { + echo "##vso[task.setvariable variable=HasChanges]$false" + echo "No changes so skipping code push" + } + displayName: Check for changes + condition: and(succeeded(), eq(${{ parameters.SkipCheckingForChanges }}, false)) + workingDirectory: ${{ parameters.WorkingDirectory }} + ignoreLASTEXITCODE: true + +- pwsh: | + # Remove the repo owner from the front of the repo name if it exists there + $repoName = "${{ parameters.TargetRepoName }}" -replace "^${{ parameters.TargetRepoOwner }}/", "" + echo "##vso[task.setvariable variable=RepoNameWithoutOwner]$repoName" + echo "RepoName = $repoName" + displayName: Remove Repo Owner from Repo Name + condition: succeeded() + workingDirectory: ${{ parameters.WorkingDirectory }} + +- task: PowerShell@2 + displayName: Push changes + condition: and(succeeded(), eq(variables['HasChanges'], 'true')) + inputs: + pwsh: true + workingDirectory: ${{ parameters.WorkingDirectory }} + filePath: ${{ parameters.ScriptDirectory }}/git-branch-push.ps1 + arguments: > + -PRBranchName "${{ parameters.BaseRepoBranch }}" + -CommitMsg "${{ parameters.CommitMsg }}" + -GitUrl "https://$(azuresdk-github-pat)@github.com/${{ parameters.BaseRepoOwner }}/$(RepoNameWithoutOwner).git" + -PushArgs "${{ parameters.PushArgs }}" + -SkipCommit $${{ parameters.SkipCheckingForChanges }} diff --git a/eng/common/pipelines/templates/steps/set-default-branch.yml b/eng/common/pipelines/templates/steps/set-default-branch.yml index b4949e93bbfa..b66478975a6c 100644 --- a/eng/common/pipelines/templates/steps/set-default-branch.yml +++ b/eng/common/pipelines/templates/steps/set-default-branch.yml @@ -8,4 +8,3 @@ steps: echo "##vso[task.setvariable variable=DefaultBranch]$setDefaultBranch" displayName: "Setup Default Branch" workingDirectory: ${{ parameters.workingDirectory }} - condition: eq(variables['DefaultBranch'], '') diff --git a/eng/common/scripts/git-branch-push.ps1 b/eng/common/scripts/git-branch-push.ps1 index 72c0df0f6b63..e99a8edbd59f 100644 --- a/eng/common/scripts/git-branch-push.ps1 +++ b/eng/common/scripts/git-branch-push.ps1 @@ -27,6 +27,7 @@ param( [Parameter(Mandatory = $false)] [string] $PushArgs = "", + [Parameter(Mandatory = $false)] [string] $RemoteName = "azure-sdk-fork", [Parameter(Mandatory = $false)] @@ -41,27 +42,36 @@ param( # would fail the first time git wrote command output. $ErrorActionPreference = "Continue" -if (!(git remote | ? {$_ -eq $RemoteName})) +if ((git remote) -contains $RemoteName) { - Write-Host "git remote add $RemoteName $GitUrl" - git remote add $RemoteName $GitUrl - if ($LASTEXITCODE -ne 0) - { - Write-Error "Unable to add remote LASTEXITCODE=$($LASTEXITCODE), see command output above." - exit $LASTEXITCODE - } + Write-Host "git remote get-url $RemoteName" + $remoteUrl = git remote get-url $RemoteName + if ($remoteUrl -ne $GitUrl) + { + Write-Error "Remote with name $RemoteName already exists with an incompatible url [$remoteUrl] which should be [$GitUrl]." + exit 1 + } } - -Write-Host "git fetch $RemoteName" -git fetch $RemoteName -if ($LASTEXITCODE -ne 0) +else { - Write-Error "Unable to fetch remote LASTEXITCODE=$($LASTEXITCODE), see command output above." + Write-Host "git remote add $RemoteName $GitUrl" + git remote add $RemoteName $GitUrl + if ($LASTEXITCODE -ne 0) + { + Write-Error "Unable to add remote LASTEXITCODE=$($LASTEXITCODE), see command output above." exit $LASTEXITCODE + } +} +# Checkout to $PRBranch, create new one if not exists. +git show-ref --verify --quiet refs/heads/$PRBranchName +if ($LASTEXITCODE -eq 0) { + Write-Host "git checkout $PRBranchName." + git checkout $PRBranchName +} +else { + Write-Host "git checkout -b $PRBranchName." + git checkout -b $PRBranchName } - -Write-Host "git checkout -b $PRBranchName" -git checkout -b $PRBranchName if ($LASTEXITCODE -ne 0) { Write-Error "Unable to create branch LASTEXITCODE=$($LASTEXITCODE), see command output above." @@ -105,6 +115,7 @@ do { $needsRetry = $true Write-Host "Git push failed with LASTEXITCODE=$($LASTEXITCODE) Need to fetch and rebase: attempt number=$($tryNumber)" + Write-Host "git fetch $RemoteName" git fetch $RemoteName if ($LASTEXITCODE -ne 0) @@ -168,8 +179,12 @@ do } } while($needsRetry -and $tryNumber -le $numberOfRetries) -if ($LASTEXITCODE -ne 0) +if ($LASTEXITCODE -ne 0 -or $tryNumber -gt $numberOfRetries) { Write-Error "Unable to push commit after $($tryNumber) retries LASTEXITCODE=$($LASTEXITCODE), see command output above." + if (0 -eq $LASTEXITCODE) + { + exit 1 + } exit $LASTEXITCODE }