From 72a5b5af6b57338472e4e8651dc2998233449443 Mon Sep 17 00:00:00 2001 From: Matt White Date: Thu, 16 Jul 2020 17:21:24 +0100 Subject: [PATCH 01/33] Support Azure DevOps (#19) * AzOps pipeline [skip ci] * Move pipeline * Merged PR 20: Use ADO APIs Related work items: #85, #86, #87, #88 * Merged PR 21: Revert 'Use ADO APIs' Reverts !20 * Ado (#2) * Reset AzOps pipeline * Merged PR 18: Support for SCMPlatform Prepares the ground for working with ADO Related work items: #85 * Merged PR 19: AzOps-ContainerBuildAndPush Related work items: #86 * Use ADO APIs to raise PRs/comments/labels * remove unused var * Update *.parameters.json location (#7) * Update *.parameters.json location * Update ubuntu-latest.yml * Update ubuntu-latest.yml * Update ubuntu-latest.yml * Update AzOpsDockerBuild.yml for Azure Pipelines * Update AzOpsDockerBuild.yml for Azure Pipelines * Devug azure creds * Update AzOpsDockerBuild.yml for Azure Pipelines * Remove debugging * debugthisshiz * Update triggers * debug git * is git installed? * moar * Remove debugging * Enable parallel jobs in pipeline (#10) * Update pipeline to allow parallel test runs * Add variables for important values * Added dependsOn for 2nd stage jobs * Update timeoutInMinutes variable * Remove quotes from PSVersionTable * Move environment configuration to script * Move Initialize-AzOpsTestEnvironment to /test/scripts/ folder * Add query params for get PR * Add verbose output * Add token output * Change git merge to git reset * Add force push * Move token output outside if block * Attempt response.count for REST response * Add active check for PR query * removing azops reference folder (#12) * removing azopsreference * changing variable name to AzOpsReferenceFolder * changing Write-Ouput to Write-AzOpsLog * adding AzOps Log * Move pipeline * Reset AzOps pipeline * AzOps pipeline [skip ci] * Merged PR 18: Support for SCMPlatform Prepares the ground for working with ADO Related work items: #85 * Merged PR 19: AzOps-ContainerBuildAndPush Related work items: #86 * Use ADO APIs to raise PRs/comments/labels * remove unused var * Update AzOpsDockerBuild.yml for Azure Pipelines * Update AzOpsDockerBuild.yml for Azure Pipelines * Devug azure creds * Update AzOpsDockerBuild.yml for Azure Pipelines * Remove debugging * debugthisshiz * Update triggers * debug git * is git installed? * moar * Remove debugging * Add query params for get PR * Add verbose output * Add token output * Change git merge to git reset * Add force push * Move token output outside if block * Attempt response.count for REST response * Add active check for PR query * removing stong type from save-azopsmg (#13) * Update container tag * Moar variables * Create ado specific comment * Add replaceme tags Co-authored-by: Matt White Co-authored-by: Matt White Co-authored-by: Lyon J Till <158992+ljtill@users.noreply.github.com> Co-authored-by: Kevin Rowlandson Co-authored-by: uday31in <14359777+uday31in@users.noreply.github.com> * AzOps pipeline [skip ci] * Move pipeline * Merged PR 20: Use ADO APIs Related work items: #85, #86, #87, #88 * Merged PR 21: Revert 'Use ADO APIs' Reverts !20 * Ado (#2) * Reset AzOps pipeline * Merged PR 18: Support for SCMPlatform Prepares the ground for working with ADO Related work items: #85 * Merged PR 19: AzOps-ContainerBuildAndPush Related work items: #86 * Use ADO APIs to raise PRs/comments/labels * remove unused var * Update *.parameters.json location (#7) * Update *.parameters.json location * Update ubuntu-latest.yml * Update ubuntu-latest.yml * Update ubuntu-latest.yml * Update AzOpsDockerBuild.yml for Azure Pipelines * Update AzOpsDockerBuild.yml for Azure Pipelines * Devug azure creds * Update AzOpsDockerBuild.yml for Azure Pipelines * Remove debugging * debugthisshiz * Update triggers * debug git * is git installed? * moar * Remove debugging * Enable parallel jobs in pipeline (#10) * Update pipeline to allow parallel test runs * Add variables for important values * Added dependsOn for 2nd stage jobs * Update timeoutInMinutes variable * Remove quotes from PSVersionTable * Move environment configuration to script * Move Initialize-AzOpsTestEnvironment to /test/scripts/ folder * Add query params for get PR * Add verbose output * Add token output * Change git merge to git reset * Add force push * Move token output outside if block * Attempt response.count for REST response * Add active check for PR query * removing azops reference folder (#12) * removing azopsreference * changing variable name to AzOpsReferenceFolder * changing Write-Ouput to Write-AzOpsLog * adding AzOps Log * Move pipeline * Reset AzOps pipeline * AzOps pipeline [skip ci] * Merged PR 18: Support for SCMPlatform Prepares the ground for working with ADO Related work items: #85 * Merged PR 19: AzOps-ContainerBuildAndPush Related work items: #86 * Use ADO APIs to raise PRs/comments/labels * remove unused var * Update AzOpsDockerBuild.yml for Azure Pipelines * Update AzOpsDockerBuild.yml for Azure Pipelines * Devug azure creds * Update AzOpsDockerBuild.yml for Azure Pipelines * Remove debugging * debugthisshiz * Update triggers * debug git * is git installed? * moar * Remove debugging * Add query params for get PR * Add verbose output * Add token output * Change git merge to git reset * Add force push * Move token output outside if block * Attempt response.count for REST response * Add active check for PR query * removing stong type from save-azopsmg (#13) * Update container tag * Moar variables * Create ado specific comment * Add replaceme tags Co-authored-by: Matt White Co-authored-by: Matt White Co-authored-by: Lyon J Till <158992+ljtill@users.noreply.github.com> Co-authored-by: Kevin Rowlandson Co-authored-by: uday31in <14359777+uday31in@users.noreply.github.com> * title & & body for ADO PR * Remove INPUT prefix from SCMPLATFORM variable name * Use $(Build.SourceBranchName) for docker tag * Updated AzOpsDockerBuild.yml * Fix env var name * Update AzOpsDockerBuild.yml for Azure Pipelines * Update AzOpsDockerBuild.yml for Azure Pipelines * Add replaceme tags * Correct GH references * Remove as this is now included upstream * Remove explicit -Verbose * Add Out-Nulls to REST requests * Remove null redirects Co-authored-by: Matt White Co-authored-by: Matt White Co-authored-by: Lyon J Till <158992+ljtill@users.noreply.github.com> Co-authored-by: Kevin Rowlandson Co-authored-by: uday31in <14359777+uday31in@users.noreply.github.com> --- src/Comments-ado.md | 17 ++++ src/public/Invoke-AzOpsGitPull.ps1 | 145 +++++++++++++++++++++-------- src/public/Invoke-AzOpsGitPush.ps1 | 48 ++++++++-- 3 files changed, 160 insertions(+), 50 deletions(-) create mode 100644 src/Comments-ado.md diff --git a/src/Comments-ado.md b/src/Comments-ado.md new file mode 100644 index 00000000..a05bda92 --- /dev/null +++ b/src/Comments-ado.md @@ -0,0 +1,17 @@ +**AzOps** + +Status: _Out of Sync_ + +Description: + +_The repository does not contain the latest Azure Resource Manager state, remediation is required before merging of the Pull Request can complete._ + +Remediation: + +You can [re-initialize](https://github.com/Azure/Enterprise-Scale/blob/main/docs/Deploy/setup-azuredevops.md#discover-environment) your repository to pull latest changes from Azure by invoking the Azure Pipeline. You can monitor the status of the Pipeline in `Pipelines` section. Upon successful completion, this will create a new `system` branch and Pull Request containing changes with latest configuration. Name of the Pull Request will be `Azure Change Notification`. + +- 1. Please merge Pull Request from `system` branch in to your `main` branch. +- 2. Update you feature branch from main `git pull origin/main` +- 3. Push your branch to `origin` by running following command `git push` + +Please manually run the AzOps pipeline, setting the `DoPull` variable to `true` diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index eaa28d3e..a547af7d 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -21,7 +21,7 @@ function Invoke-AzOpsGitPull { Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing branch (system)" Start-AzOpsNativeExecution { git checkout system - git merge origin/main --strategy-option theirs --allow-unrelated-histories + git reset --hard origin/main } | Out-Host } else { @@ -56,53 +56,116 @@ function Invoke-AzOpsGitPull { Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin" Start-AzOpsNativeExecution { - git push origin system + git push --force origin system } | Out-Null - Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if label (system) exists" - $params = @{ - Uri = ($env:GITHUB_API_URL + "/repos/" + $env:GITHUB_REPOSITORY + "/labels") - Headers = @{ - "Authorization" = ("Bearer " + $env:GITHUB_TOKEN) + switch ($env:SCMPLATFORM) { + #region SCMPlatform GitHub + "GitHub" { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if label (system) exists" + $params = @{ + Uri = ($env:GITHUB_API_URL + "/repos/" + $env:GITHUB_REPOSITORY + "/labels") + Headers = @{ + "Authorization" = ("Bearer " + $env:GITHUB_TOKEN) + } + } + $response = Invoke-RestMethod -Method "Get" @params | Where-Object -FilterScript { $_.name -like "system" } + + if (!$response) { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new label (system)" + $params = @{ + Uri = ($env:GITHUB_API_URL + "/repos/" + $env:GITHUB_REPOSITORY + "/labels") + Headers = @{ + "Authorization" = ("Bearer " + $env:GITHUB_TOKEN) + "Content-Type" = "application/json" + } + Body = (@{ + "name" = "system" + "description" = "[AzOps] Do not delete" + "color" = "db9436" + } | ConvertTo-Json) + } + $response = Invoke-RestMethod -Method "Post" @params + } + + Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" + + + $params = @{ + Uri = ($env:GITHUB_API_URL + "/repos/" + $env:GITHUB_REPOSITORY + ("/pulls?state=open&head=") + $env:GITHUB_REPOSITORY + ":system") + Headers = @{ + "Authorization" = ("Bearer " + $env:GITHUB_TOKEN) + } + } + $response = Invoke-RestMethod -Method "Get" @params + + if (!$response) { + Write-AzOpsLog -Level Information -Topic "gh" -Message "Creating new pull request" + Start-AzOpsNativeExecution { + gh pr create --title $env:GITHUB_PULL_REQUEST --body "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" --label "system" --repo $env:GITHUB_REPOSITORY + } | Out-Host + } + else { + Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request creation" + } } - } - $response = Invoke-RestMethod -Method "Get" @params | Where-Object -FilterScript { $_.name -like "system" } - - if (!$response) { - Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new label (system)" - $params = @{ - Uri = ($env:GITHUB_API_URL + "/repos/" + $env:GITHUB_REPOSITORY + "/labels") - Headers = @{ - "Authorization" = ("Bearer " + $env:GITHUB_TOKEN) - "Content-Type" = "application/json" + #endregion + #region SCMPlatform AzureDevOps + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" + + $params = @{ + Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?searchCriteria.sourceRefName=refs/heads/system&searchCriteria.targetRefName=refs/heads/main&searchCriteria.status=active&api-version=5.1" + Method = "Get" + Headers = @{ + "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + "Content-Type" = "application/json" + } + } + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "URI: $($params.Uri)" + $response = Invoke-RestMethod @params + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Pull request response count: $($response.count)" + + if ($response.count -eq 0) { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new pull request" + + $params = @{ + Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?api-version=5.1" + Method = "Post" + Headers = @{ + "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + "Content-Type" = "application/json" + } + Body = (@{ + "sourceRefName" = "refs/heads/system" + "targetRefName" = "refs/heads/main" + "title" = "$env:GITHUB_PULL_REQUEST" + "description" = "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" + } | ConvertTo-Json -Depth 5) + } + $response = Invoke-RestMethod @params + + Write-AzOpsLog -Level Information -Topic "rest" -Message "Assigning pull request label" + + $params = @{ + Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($response.pullRequestId)/labels?api-version=5.1-preview.1" + Method = "Post" + Headers = @{ + "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + "Content-Type" = "application/json" + } + Body = (@{ + "name" = "system" + } | ConvertTo-Json -Depth 5) + } + Invoke-RestMethod @params } - Body = (@{ - "name" = "system" - "description" = "[AzOps] Do not delete" - "color" = "db9436" - } | ConvertTo-Json) } - $response = Invoke-RestMethod -Method "Post" @params - } - - Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" - $params = @{ - Uri = ($env:GITHUB_API_URL + "/repos/" + $env:GITHUB_REPOSITORY + ("/pulls?state=open&head=") + $env:GITHUB_REPOSITORY + ":system") - Headers = @{ - "Authorization" = ("Bearer " + $env:GITHUB_TOKEN) + #endregion + Default { + Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" } } - $response = Invoke-RestMethod -Method "Get" @params - - if (!$response) { - Write-AzOpsLog -Level Information -Topic "gh" -Message "Creating new pull request" - Start-AzOpsNativeExecution { - gh pr create --title $env:GITHUB_PULL_REQUEST --body "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" --label "system" --repo $env:GITHUB_REPOSITORY - } | Out-Host - } - else { - Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request creation" - } } } diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 7bb9cfab..b65ea535 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -22,17 +22,47 @@ function Invoke-AzOpsGitPush { } Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" - Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $env:GITHUB_COMMENTS" - $params = @{ - Headers = @{ - "Authorization" = ("Bearer " + $env:GITHUB_TOKEN ) + + switch ($env:SCMPLATFORM) { + "GitHub" { + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $env:GITHUB_COMMENTS" + $params = @{ + Headers = @{ + "Authorization" = ("Bearer " + $env:GITHUB_TOKEN ) + } + Body = (@{ + "body" = "$(Get-Content -Path "$PSScriptRoot/../Comments.md" -Raw) `n Changes: `n`n$output" + } | ConvertTo-Json) + } + Invoke-RestMethod -Method "POST" -Uri $env:GITHUB_COMMENTS @params | Out-Null + exit 1 + } + "AzureDevOps" { + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $env:INPUT_ADO_COMMENTS" + $params = @{ + Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/threads?api-version=5.1" + Method = "Post" + Headers = @{ + "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + "Content-Type" = "application/json" + } + Body = (@{ + comments = @( + (@{ + "parentCommentId" = 0 + "content" = "$(Get-Content -Path "$PSScriptRoot/../Comments-ado.md" -Raw)" + "commentType" = 1 + }) + ) + } | ConvertTo-Json -Depth 5) + } + Invoke-RestMethod @params | Out-Null + exit 1 + } + Default { + Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" } - Body = (@{ - "body" = "$(Get-Content -Path "$PSScriptRoot/../Comments.md" -Raw) `n Changes: `n`n$output" - } | ConvertTo-Json) } - Invoke-RestMethod -Method "POST" -Uri $env:GITHUB_COMMENTS @params | Out-Null - exit 1 } else { Write-AzOpsLog -Level Information -Topic "git" -Message "Branch is in sync with Azure" From 883ebd546d33d2eb7049ce85af7eb9d58419d55a Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 30 Jul 2020 12:46:28 +0100 Subject: [PATCH 02/33] Update public/ --- .../Initialize-AzOpsGlobalVariables.ps1 | 1 + src/public/Invoke-AzOpsGitPull.ps1 | 225 +++++++++++------- src/public/Invoke-AzOpsGitPush.ps1 | 79 ++++-- 3 files changed, 196 insertions(+), 109 deletions(-) diff --git a/src/public/Initialize-AzOpsGlobalVariables.ps1 b/src/public/Initialize-AzOpsGlobalVariables.ps1 index d56fd8c5..44dc390d 100644 --- a/src/public/Initialize-AzOpsGlobalVariables.ps1 +++ b/src/public/Initialize-AzOpsGlobalVariables.ps1 @@ -69,6 +69,7 @@ function Initialize-AzOpsGlobalVariables { GITHUB_COMMENTS = @{ GitHubComments = $null } GITHUB_HEAD_REF = @{ GitHubHeadRef = $null } GITHUB_BASE_REF = @{ GitHubBaseRef = $null } + SCM_PLATFORM = @{ SCMPlatform = $null } } # Iterate through each variable and take appropriate action foreach ($AzOpsEnv in $AzOpsEnvVariables.Keys) { diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index a570d603..f9db6046 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -72,106 +72,163 @@ function Invoke-AzOpsGitPull { git push origin system -f } | Out-Null - # GitHub Labels - Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if label (system) exists" - # TODO: Replace REST call when GH CLI paging support is available - $params = @{ - Uri = ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/labels") - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) - } - } - $response = Invoke-RestMethod -Method "Get" @params | Where-Object -FilterScript { $_.name -like "system" } - - if (-not $response) { - # GitHub Labels - Create - Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new label (system)" - # TODO: Replace REST call when GH CLI paging support is available - $params = @{ - Uri = ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/labels") - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) - "Content-Type" = "application/json" + switch ($global:SCMPlatform) { + "GitHub" { + # GitHub Labels + Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if label (system) exists" + # TODO: Replace REST call when GH CLI paging support is available + $params = @{ + Uri = ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/labels") + Headers = @{ + "Authorization" = ("Bearer " + $global:GitHubToken) + } + } + $response = Invoke-RestMethod -Method "Get" @params | Where-Object -FilterScript { $_.name -like "system" } + + if (-not $response) { + # GitHub Labels - Create + Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new label (system)" + # TODO: Replace REST call when GH CLI paging support is available + $params = @{ + Uri = ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/labels") + Headers = @{ + "Authorization" = ("Bearer " + $global:GitHubToken) + "Content-Type" = "application/json" + } + Body = (@{ + "name" = "system" + "description" = "[AzOps] Do not delete" + "color" = "db9436" + } | ConvertTo-Json) + } + $response = Invoke-RestMethod -Method "Post" @params } - Body = (@{ - "name" = "system" - "description" = "[AzOps] Do not delete" - "color" = "db9436" - } | ConvertTo-Json) - } - $response = Invoke-RestMethod -Method "Post" @params - } - - Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" - # TODO: Replace REST call when GH CLI paging support is available - $params = @{ - Uri = ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + ("/pulls?state=open&head=") + $global:GitHubRepository + ":system") - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) - } - } - $response = Invoke-RestMethod -Method "Get" @params - - # GitHub Pull Request - Create - if (-not $response) { - Write-AzOpsLog -Level Information -Topic "gh" -Message "Creating new pull request" - Start-AzOpsNativeExecution { - gh pr create --title $global:GitHubPullRequest --body "Auto-generated PR triggered by Azure Resource Manager" --label "system" --repo $global:GitHubRepository - } | Out-Host - } - else { - Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request creation" - } - # GitHub Pull Request - Merge - if ($global:GitHubAutoMerge -eq 1) { - Write-AzOpsLog -Level Information -Topic "rest" -Message "Retrieving new pull request" - $params = @{ - Uri = ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + ("/pulls?state=open&head=") + $global:GitHubRepository + ":system") - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) + Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" + # TODO: Replace REST call when GH CLI paging support is available + $params = @{ + Uri = ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + ("/pulls?state=open&head=") + $global:GitHubRepository + ":system") + Headers = @{ + "Authorization" = ("Bearer " + $global:GitHubToken) + } } - } - $response = Invoke-RestMethod -Method "Get" @params - - Write-AzOpsLog -Level Information -Topic "gh" -Message "Merging new pull request" - $attempt = 1 - $retryCount = 3 - $unmerged = $true - do { - try { + $response = Invoke-RestMethod -Method "Get" @params + + # GitHub Pull Request - Create + if (-not $response) { + Write-AzOpsLog -Level Information -Topic "gh" -Message "Creating new pull request" Start-AzOpsNativeExecution { - gh pr merge $response[0].number --squash --delete-branch -R $global:GitHubRepository + gh pr create --title $global:GitHubPullRequest --body "Auto-generated PR triggered by Azure Resource Manager" --label "system" --repo $global:GitHubRepository } | Out-Host - $unmerged = $false } - catch { - if ($attempt -gt $retryCount) { - $unmerged = $true + else { + Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request creation" + } + + # GitHub Pull Request - Merge + if ($global:GitHubAutoMerge -eq 1) { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Retrieving new pull request" + $params = @{ + Uri = ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + ("/pulls?state=open&head=") + $global:GitHubRepository + ":system") + Headers = @{ + "Authorization" = ("Bearer " + $global:GitHubToken) + } + } + $response = Invoke-RestMethod -Method "Get" @params + + Write-AzOpsLog -Level Information -Topic "gh" -Message "Merging new pull request" + $attempt = 1 + $retryCount = 3 + $unmerged = $true + do { + try { + Start-AzOpsNativeExecution { + gh pr merge $response[0].number --squash --delete-branch -R $global:GitHubRepository + } | Out-Host + $unmerged = $false + } + catch { + if ($attempt -gt $retryCount) { + $unmerged = $true + } + else { + Write-AzOpsLog -Level Warning -Topic "gh" -Message "Retrying pull request merge" + Start-Sleep -Seconds 5 + $attempt = $attempt + 1 + } + } } - else { - Write-AzOpsLog -Level Warning -Topic "gh" -Message "Retrying pull request merge" - Start-Sleep -Seconds 5 - $attempt = $attempt + 1 + while ($unmerged) + + if ($unmerged -eq $true) { + $params = @{ + Headers = @{ + "Authorization" = ("Bearer " + $global:GitHubToken) + } + Body = (@{ + "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/merge/README.md" -Raw)" + } | ConvertTo-Json) + } + Invoke-RestMethod -Method "POST" -Uri ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/issues/" + $response[0].number + "/comments") @params | Out-Null } } + else { + Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request merge" + } } - while ($unmerged) + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" - if ($unmerged -eq $true) { $params = @{ + Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?searchCriteria.sourceRefName=refs/heads/system&searchCriteria.targetRefName=refs/heads/main&searchCriteria.status=active&api-version=5.1" + Method = "Get" Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) + "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + "Content-Type" = "application/json" } - Body = (@{ - "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/merge/README.md" -Raw)" - } | ConvertTo-Json) } - Invoke-RestMethod -Method "POST" -Uri ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/issues/" + $response[0].number + "/comments") @params | Out-Null + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "URI: $($params.Uri)" + $response = Invoke-RestMethod @params + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Pull request response count: $($response.count)" + + if ($response.count -eq 0) { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new pull request" + + $params = @{ + Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?api-version=5.1" + Method = "Post" + Headers = @{ + "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + "Content-Type" = "application/json" + } + Body = (@{ + "sourceRefName" = "refs/heads/system" + "targetRefName" = "refs/heads/main" + "title" = "$env:GITHUB_PULL_REQUEST" + "description" = "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" + } | ConvertTo-Json -Depth 5) + } + $response = Invoke-RestMethod @params + + Write-AzOpsLog -Level Information -Topic "rest" -Message "Assigning pull request label" + + $params = @{ + Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($response.pullRequestId)/labels?api-version=5.1-preview.1" + Method = "Post" + Headers = @{ + "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + "Content-Type" = "application/json" + } + Body = (@{ + "name" = "system" + } | ConvertTo-Json -Depth 5) + } + Invoke-RestMethod @params + } + } + default { + Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" } - } - else { - Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request merge" } } } diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 69bf239d..4f593fc1 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -83,35 +83,64 @@ function Invoke-AzOpsGitPush { Write-AzOpsLog -Level Information -Topic "git" -Message $_ } - if ($global:AzOpsStrictMode -eq 1) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Branch is not consistent with Azure" - Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" - Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $global:GitHubComments" - $params = @{ - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) + switch ($global:SCMPlatform) { + "GitHub" { + if ($global:AzOpsStrictMode -eq 1) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Branch is not consistent with Azure" + Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $global:GitHubComments" + $params = @{ + Headers = @{ + "Authorization" = ("Bearer " + $global:GitHubToken) + } + Body = (@{ + "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/strict/README.md" -Raw) `n Changes: `n`n$output" + } | ConvertTo-Json) + } + Invoke-RestMethod -Method "POST" -Uri $global:GitHubComments @params | Out-Null + exit 1 + } + if ($global:AzOpsStrictMode -eq 0) { + Write-AzOpsLog -Level Warning -Topic "git" -Message "Default Mode" + Write-AzOpsLog -Level Information -Topic "git" -Message "Changes:" + Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $global:GitHubComments" + $params = @{ + Headers = @{ + "Authorization" = ("Bearer " + $global:GitHubToken) + } + Body = (@{ + "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/default/README.md" -Raw) `n Changes: `n`n$output" + } | ConvertTo-Json) + } + Invoke-RestMethod -Method "POST" -Uri $global:GitHubComments @params | Out-Null } - Body = (@{ - "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/strict/README.md" -Raw) `n Changes: `n`n$output" - } | ConvertTo-Json) } - Invoke-RestMethod -Method "POST" -Uri $global:GitHubComments @params | Out-Null - exit 1 - } - if ($global:AzOpsStrictMode -eq 0) { - Write-AzOpsLog -Level Warning -Topic "git" -Message "Default Mode" - Write-AzOpsLog -Level Information -Topic "git" -Message "Changes:" - Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" - Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $global:GitHubComments" - $params = @{ - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) + "AzureDevOps" { + Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $env:INPUT_ADO_COMMENTS" + $params = @{ + Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/threads?api-version=5.1" + Method = "Post" + Headers = @{ + "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + "Content-Type" = "application/json" + } + Body = (@{ + comments = @( + (@{ + "parentCommentId" = 0 + "content" = "$(Get-Content -Path "$PSScriptRoot/../Comments-ado.md" -Raw)" + "commentType" = 1 + }) + ) + } | ConvertTo-Json -Depth 5) } - Body = (@{ - "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/default/README.md" -Raw) `n Changes: `n`n$output" - } | ConvertTo-Json) + Invoke-RestMethod @params | Out-Null + exit 1 + } + default { + Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" } - Invoke-RestMethod -Method "POST" -Uri $global:GitHubComments @params | Out-Null } } else { From 8c22f3ea3a8f09abe98b9bfa3bc9b28de1445c87 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 30 Jul 2020 12:51:31 +0100 Subject: [PATCH 03/33] Update guidance/ --- src/Comments-ado.md | 17 ----------------- .../guidance/strict/azdevops/README.md | 18 ++++++++++++++++++ .../guidance/strict/{ => github}/README.md | 2 +- 3 files changed, 19 insertions(+), 18 deletions(-) delete mode 100644 src/Comments-ado.md create mode 100644 src/auxiliary/guidance/strict/azdevops/README.md rename src/auxiliary/guidance/strict/{ => github}/README.md (87%) diff --git a/src/Comments-ado.md b/src/Comments-ado.md deleted file mode 100644 index a05bda92..00000000 --- a/src/Comments-ado.md +++ /dev/null @@ -1,17 +0,0 @@ -**AzOps** - -Status: _Out of Sync_ - -Description: - -_The repository does not contain the latest Azure Resource Manager state, remediation is required before merging of the Pull Request can complete._ - -Remediation: - -You can [re-initialize](https://github.com/Azure/Enterprise-Scale/blob/main/docs/Deploy/setup-azuredevops.md#discover-environment) your repository to pull latest changes from Azure by invoking the Azure Pipeline. You can monitor the status of the Pipeline in `Pipelines` section. Upon successful completion, this will create a new `system` branch and Pull Request containing changes with latest configuration. Name of the Pull Request will be `Azure Change Notification`. - -- 1. Please merge Pull Request from `system` branch in to your `main` branch. -- 2. Update you feature branch from main `git pull origin/main` -- 3. Push your branch to `origin` by running following command `git push` - -Please manually run the AzOps pipeline, setting the `DoPull` variable to `true` diff --git a/src/auxiliary/guidance/strict/azdevops/README.md b/src/auxiliary/guidance/strict/azdevops/README.md new file mode 100644 index 00000000..d3dcacb1 --- /dev/null +++ b/src/auxiliary/guidance/strict/azdevops/README.md @@ -0,0 +1,18 @@ +## AzOps + +The 'main' branch in the repository does not contain the latest configuration of Azure. + +It is strongly recommended to ensure that 'feature' and 'main' branches contain the current Azure configuration. + +### Remediation + +[Re-initialize](https://github.com/Azure/Enterprise-Scale/blob/main/docs/Deploy/setup-azuredevops.md#discover-environment) your repository to pull latest changes from Azure by invoking the Azure Pipeline. You can monitor the status of the Pipeline in `Pipelines` section. + +Upon successful completion, the action will create a new `system` branch and a new `Azure Change Notification` pull request containing the latest configuration. + +- Please merge Pull Request from `system` branch in to your `main` branch. + +- Update you feature branch from main `git pull origin/main` + +- Push your branch to `origin` by running following command `git push` + diff --git a/src/auxiliary/guidance/strict/README.md b/src/auxiliary/guidance/strict/github/README.md similarity index 87% rename from src/auxiliary/guidance/strict/README.md rename to src/auxiliary/guidance/strict/github/README.md index 7de2ebdf..0c79e7fb 100644 --- a/src/auxiliary/guidance/strict/README.md +++ b/src/auxiliary/guidance/strict/github/README.md @@ -6,7 +6,7 @@ It is strongly recommended to ensure that 'feature' and 'main' branches contain ### Remediation -[Re-initialization](https://github.com/Azure/Enterprise-Scale/blob/main/docs/Deploy/discover-environment.md#initialize-existing-environment) of the repository to pull the latest changes from Azure by manually invoking the GitHub Action. +[Re-initialize](https://github.com/Azure/Enterprise-Scale/blob/main/docs/Deploy/discover-environment.md#initialize-existing-environment) of the repository to pull the latest changes from Azure by manually invoking the GitHub Action. Upon successful completion, the action will create a new `system` branch and a new `Azure Change Notification` pull request containing the latest configuration. From f3e5ed997bcd6f47598425343e5495c59eb2bae1 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 30 Jul 2020 12:52:37 +0100 Subject: [PATCH 04/33] Update guidance/ --- src/auxiliary/guidance/strict/azdevops/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/auxiliary/guidance/strict/azdevops/README.md b/src/auxiliary/guidance/strict/azdevops/README.md index d3dcacb1..0221e478 100644 --- a/src/auxiliary/guidance/strict/azdevops/README.md +++ b/src/auxiliary/guidance/strict/azdevops/README.md @@ -6,7 +6,7 @@ It is strongly recommended to ensure that 'feature' and 'main' branches contain ### Remediation -[Re-initialize](https://github.com/Azure/Enterprise-Scale/blob/main/docs/Deploy/setup-azuredevops.md#discover-environment) your repository to pull latest changes from Azure by invoking the Azure Pipeline. You can monitor the status of the Pipeline in `Pipelines` section. +[Re-initialize](https://github.com/Azure/Enterprise-Scale/blob/main/docs/Deploy/setup-azuredevops.md#discover-environment) your repository to pull latest changes from Azure by invoking the Azure Pipeline. You can monitor the status of the Pipeline in `Pipelines` section. Upon successful completion, the action will create a new `system` branch and a new `Azure Change Notification` pull request containing the latest configuration. From 3abd661a57dbe8cb7db3e0567236beed00d600fa Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 30 Jul 2020 15:59:17 +0100 Subject: [PATCH 05/33] Move providerfeatures.json & resourceproviders.json --- src/{ => auxiliary}/providerfeatures.json | 0 src/{ => auxiliary}/resourceproviders.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/{ => auxiliary}/providerfeatures.json (100%) rename src/{ => auxiliary}/resourceproviders.json (100%) diff --git a/src/providerfeatures.json b/src/auxiliary/providerfeatures.json similarity index 100% rename from src/providerfeatures.json rename to src/auxiliary/providerfeatures.json diff --git a/src/resourceproviders.json b/src/auxiliary/resourceproviders.json similarity index 100% rename from src/resourceproviders.json rename to src/auxiliary/resourceproviders.json From affe6059ef897ccee373534484a5484158fd89bf Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Fri, 31 Jul 2020 10:42:39 +0100 Subject: [PATCH 06/33] Update Dockerfile --- Dockerfile | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0933a35e..422676df 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,13 +9,15 @@ ARG azure_resources=2.3.0 RUN [ "/bin/bash", "-c", "apt-get update &> /dev/null && apt-get install -y git wget &> /dev/null" ] RUN [ "/bin/bash", "-c", "wget https://github.com/cli/cli/releases/download/v${github}/gh_${github}_linux_amd64.deb -O /tmp/gh_${github}_linux_amd64.deb &> /dev/null" ] +RUN [ "/bin/bash", "-c", "curl -sL https://aka.ms/InstallAzureCLIDeb | bash &> /dev/null"] +RUN [ "/bin/bash", "-c", "az extension add --name azure-devops --system &> /dev/null"] RUN [ "/bin/bash", "-c", "dpkg -i /tmp/gh_${github}_linux_amd64.deb &> /dev/null" ] RUN [ "/usr/bin/pwsh", "-Command", "$ProgressPreference = 'SilentlyContinue'; Install-Module -Name Az.Accounts -RequiredVersion ${azure_accounts} -Scope AllUsers -Force" ] RUN [ "/usr/bin/pwsh", "-Command", "$ProgressPreference = 'SilentlyContinue'; Install-Module -Name Az.Resources -RequiredVersion ${azure_resources} -Scope AllUsers -Force" ] -COPY . /action +COPY . /var/lib/app -ENV AzOpsMainTemplate='/action/template/template.json' -ENV AzOpsStateConfig='/action/src/AzOpsStateConfig.json' +ENV AzOpsMainTemplate='/var/lib/app/template/template.json' +ENV AzOpsStateConfig='/var/lib/app/src/AzOpsStateConfig.json' -ENTRYPOINT ["pwsh", "/action/entrypoint.ps1"] +ENTRYPOINT ["pwsh", "/var/lib/app/entrypoint.ps1"] From f68f4ee4df56f7d2e4207571a5d9e23fb27fb9bf Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Fri, 31 Jul 2020 10:42:45 +0100 Subject: [PATCH 07/33] Update public/ --- src/public/Invoke-AzOpsGitPull.ps1 | 96 +++++++++++++++--------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index f9db6046..0f3e41e1 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -178,53 +178,55 @@ function Invoke-AzOpsGitPull { } "AzureDevOps" { Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" - - $params = @{ - Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?searchCriteria.sourceRefName=refs/heads/system&searchCriteria.targetRefName=refs/heads/main&searchCriteria.status=active&api-version=5.1" - Method = "Get" - Headers = @{ - "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) - "Content-Type" = "application/json" - } - } - Write-AzOpsLog -Level Verbose -Topic "rest" -Message "URI: $($params.Uri)" - $response = Invoke-RestMethod @params - Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Pull request response count: $($response.count)" - - if ($response.count -eq 0) { - Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new pull request" - - $params = @{ - Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?api-version=5.1" - Method = "Post" - Headers = @{ - "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) - "Content-Type" = "application/json" - } - Body = (@{ - "sourceRefName" = "refs/heads/system" - "targetRefName" = "refs/heads/main" - "title" = "$env:GITHUB_PULL_REQUEST" - "description" = "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" - } | ConvertTo-Json -Depth 5) - } - $response = Invoke-RestMethod @params - - Write-AzOpsLog -Level Information -Topic "rest" -Message "Assigning pull request label" - - $params = @{ - Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($response.pullRequestId)/labels?api-version=5.1-preview.1" - Method = "Post" - Headers = @{ - "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) - "Content-Type" = "application/json" - } - Body = (@{ - "name" = "system" - } | ConvertTo-Json -Depth 5) - } - Invoke-RestMethod @params - } + $response = Start-AzOpsNativeExecution { + az repos pr list --output json + } | ConvertFrom-Json | ForEach-Object { $_ | Where-Object -FilterScript {$_.sourceRefName -eq "refs/heads/system"} } + + Write-AzOpsLog -Level Information -Topic "az" -Message "$($response)" + + # $params = @{ + # Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?searchCriteria.sourceRefName=refs/heads/system&searchCriteria.targetRefName=refs/heads/main&searchCriteria.status=active&api-version=5.1" + # Method = "Get" + # Headers = @{ + # "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + # "Content-Type" = "application/json" + # } + # } + # $response = Invoke-RestMethod @params + + # if ($null -ne $response) { + # Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new pull request" + # $params = @{ + # Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?api-version=5.1" + # Method = "Post" + # Headers = @{ + # "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + # "Content-Type" = "application/json" + # } + # Body = (@{ + # "sourceRefName" = "refs/heads/system" + # "targetRefName" = "refs/heads/main" + # "title" = "$env:GITHUB_PULL_REQUEST" + # "description" = "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" + # } | ConvertTo-Json -Depth 5) + # } + # $response = Invoke-RestMethod @params + + # Write-AzOpsLog -Level Information -Topic "rest" -Message "Assigning pull request label" + + # $params = @{ + # Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($response.pullRequestId)/labels?api-version=5.1-preview.1" + # Method = "Post" + # Headers = @{ + # "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + # "Content-Type" = "application/json" + # } + # Body = (@{ + # "name" = "system" + # } | ConvertTo-Json -Depth 5) + # } + # Invoke-RestMethod @params + # } } default { Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" From 0496c4432a84b403c87e063d94dc71cb677ab897 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Mon, 3 Aug 2020 14:11:14 +0100 Subject: [PATCH 08/33] Add Azure DevOps support --- entrypoint.ps1 | 54 ++++-- .../Initialize-AzOpsGlobalVariables.ps1 | 27 ++- src/public/Invoke-AzOpsGitPull.ps1 | 83 ++++----- src/public/Invoke-AzOpsGitPush.ps1 | 158 +++++++++++------- 4 files changed, 196 insertions(+), 126 deletions(-) diff --git a/entrypoint.ps1 b/entrypoint.ps1 index 41d9a390..aea439d9 100644 --- a/entrypoint.ps1 +++ b/entrypoint.ps1 @@ -32,19 +32,51 @@ function Initialization { Connect-AzAccount -TenantId $credentials.tenantId -ServicePrincipal -Credential $credential -SubscriptionId $credentials.subscriptionId -WarningAction SilentlyContinue | Out-Null # Configure git - Start-AzOpsNativeExecution { - git config --global user.email $env:GITHUB_EMAIL - git config --global user.name $env:GITHUB_USERNAME - } | Out-Host + switch ($env:SCM_PLATFORM) { + "GitHub" { + Start-AzOpsNativeExecution { + git config --global user.name $env:GITHUB_USERNAME + git config --global user.email $env:GITHUB_EMAIL + } | Out-Host + + } + "AzureDevOps" { + Start-AzOpsNativeExecution { + git config --global user.name $env:AZDEVOPS_USERNAME + git config --global user.email $env:AZDEVOPS_EMAIL + } | Out-Host + + } + } # Print environment variables - Write-AzOpsLog -Level Information -Topic "entrypoint" -Message "AZOPS_STATE is $($env:AZOPS_STATE)" - Write-AzOpsLog -Level Information -Topic "entrypoint" -Message "AZOPS_ENROLLMENT_ACCOUNT is $($env:AZOPS_ENROLLMENT_ACCOUNT)" - Write-AzOpsLog -Level Information -Topic "entrypoint" -Message "AZOPS_OFFER_TYPE is $($env:AZOPS_OFFER_TYPE)" - Write-AzOpsLog -Level Information -Topic "entrypoint" -Message "AZOPS_DEFAULT_DEPLOYMENT_REGION is $($env:AZOPS_DEFAULT_DEPLOYMENT_REGION)" - Write-AzOpsLog -Level Information -Topic "entrypoint" -Message "AZOPS_INVALIDATE_CACHE is $($env:AZOPS_INVALIDATE_CACHE)" - Write-AzOpsLog -Level Information -Topic "entrypoint" -Message "AZOPS_IGNORE_CONTEXT_CHECK is $($env:AZOPS_IGNORE_CONTEXT_CHECK)" - Write-AzOpsLog -Level Information -Topic "entrypoint" -Message "AZOPS_THROTTLE_LIMIT is $($env:AZOPS_THROTTLE_LIMIT)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_STATE: $($env:AZOPS_STATE)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_INVALIDATE_CACHE: $($env:AZOPS_INVALIDATE_CACHE)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_ENROLLMENT_ACCOUNT: $($env:AZOPS_ENROLLMENT_ACCOUNT)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_DEFAULT_DEPLOYMENT_REGION: $($env:AZOPS_DEFAULT_DEPLOYMENT_REGION)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_IGNORE_CONTEXT_CHECK: $($env:AZOPS_IGNORE_CONTEXT_CHECK)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_OFFER_TYPE: $($env:AZOPS_OFFER_TYPE)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_STRICT_MODE: $($env:AZOPS_STRICT_MODE)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_THROTTLE_LIMIT: $($env:AZOPS_THROTTLE_LIMIT)" + switch ($env:SCM_PLATFORM) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_BASE_REF: $($env:GITHUB_BASE_REF)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_COMMENTS: $($env:GITHUB_COMMENTS)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_EMAIL: $($env:GITHUB_EMAIL)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_HEAD_REF: $($env:GITHUB_HEAD_REF)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_ISSUE: $($env:GITHUB_ISSUE)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_USERNAME: $($env:GITHUB_USERNAME)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_PULL_REQUEST: $($env:GITHUB_PULL_REQUEST)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_AUTO_MERGE: $($env:GITHUB_AUTO_MERGE)" + + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_AUTO_MERGE: $($env:AZDEVOPS_AUTO_MERGE)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_EMAIL: $($env:AZDEVOPS_EMAIL)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_USERNAME: $($env:AZDEVOPS_USERNAME)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_PULL_REQUEST: $($env:AZDEVOPS_PULL_REQUEST)" + } + } # Initialize global variables Initialize-AzOpsGlobalVariables diff --git a/src/public/Initialize-AzOpsGlobalVariables.ps1 b/src/public/Initialize-AzOpsGlobalVariables.ps1 index 44dc390d..ed432b74 100644 --- a/src/public/Initialize-AzOpsGlobalVariables.ps1 +++ b/src/public/Initialize-AzOpsGlobalVariables.ps1 @@ -42,10 +42,11 @@ function Initialize-AzOpsGlobalVariables { # Required environment variables hashtable with default values $AzOpsEnvVariables = @{ + # AzOps AZOPS_STATE = @{ AzOpsState = (Join-Path $pwd -ChildPath "azops") } # Folder to store AzOpsState artefact AZOPS_MAIN_TEMPLATE = @{ AzOpsMainTemplate = "$PSScriptRoot\..\..\template\template.json" } # Main template json AZOPS_STATE_CONFIG = @{ AzOpsStateConfig = "$PSScriptRoot\..\AzOpsStateConfig.json" } # Configuration file for resource serialization - AZOPS_ENROLLMENT_PRINCIPAL_NAME = @{ AzOpsEnrollmentAccountPrincipalName = $null } + AZOPS_ENROLLMENT_ACCOUNT = @{ AzOpsEnrollmentAccountPrincipalName = $null } AZOPS_EXCLUDED_SUB_OFFER = @{ AzOpsExcludedSubOffer = "AzurePass_2014-09-01,FreeTrial_2014-09-01,AAD_2015-09-01" } # Excluded QuotaIDs as per https://docs.microsoft.com/en-us/azure/cost-management-billing/costs/understand-cost-mgt-data#supported-microsoft-azure-offers AZOPS_EXCLUDED_SUB_STATE = @{ AzOpsExcludedSubState = "Disabled,Deleted,Warned,Expired,PastDue" } # Excluded subscription states as per https://docs.microsoft.com/en-us/rest/api/resources/subscriptions/list#subscriptionstate AZOPS_OFFER_TYPE = @{ AzOpsOfferType = 'MS-AZR-0017P' } @@ -60,15 +61,25 @@ function Initialize-AzOpsGlobalVariables { AZOPS_STRICT_MODE = @{ AzOpsStrictMode = 0 } AZOPS_SKIP_RESOURCE_GROUP = @{ AzOpsSkipResourceGroup = 1 } AZOPS_SKIP_POLICY = @{ AzOpsSkipPolicy = 0 } - GITHUB_API_URL = @{ GitHubApiUrl = $null } - GITHUB_PULL_REQUEST = @{ GitHubPullRequest = $null } - GITHUB_REPOSITORY = @{ GitHubRepository = $null } - GITHUB_TOKEN = @{ GitHubToken = $null } - GITHUB_AUTO_MERGE = @{ GitHubAutoMerge = 1 } - GITHUB_BRANCH = @{ GitHubBranch = $null } - GITHUB_COMMENTS = @{ GitHubComments = $null } + # Azure DevOps + AZDEVOPS_AUTO_MERGE = @{ AzDevOpsAutoMerge = 1 } + AZDEVOPS_EMAIL = @{ AzDevOpsEmail = $null } + AZDEVOPS_USERNAME = @{ AzDevOpsUsername = $null } + AZDEVOPS_PULL_REQUEST = @{ AzDevOpsPullRequest = $null } + AZDEVOPS_HEAD_REF = @{ AzDevOpsHeadRef = $null } + AZDEVOPS_BASE_REF = @{ AzDevOpsBaseRef = $null } + # GitHub + GITHUB_AUTO_MERGE = @{ GitHubAutoMerge = 1 } # Auto merge pull requests for pull workflow + GITHUB_EMAIL = @{ GitHubEmail = $null } + GITHUB_USERNAME = @{ GitHubUsername = $null } + GITHUB_PULL_REQUEST = @{ GitHubPullRequest = $null } # Pull Request title GITHUB_HEAD_REF = @{ GitHubHeadRef = $null } GITHUB_BASE_REF = @{ GitHubBaseRef = $null } + GITHUB_API_URL = @{ GitHubApiUrl = $null } # Built-in env var + GITHUB_REPOSITORY = @{ GitHubRepository = $null } # Built-in env var + GITHUB_TOKEN = @{ GitHubToken = $null } # Built-in env var + GITHUB_COMMENTS = @{ GitHubComments = $null } # Built-in env var + # Source Control SCM_PLATFORM = @{ SCMPlatform = $null } } # Iterate through each variable and take appropriate action diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index 0f3e41e1..709025b6 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -74,7 +74,7 @@ function Invoke-AzOpsGitPull { switch ($global:SCMPlatform) { "GitHub" { - # GitHub Labels + # GitHub Labels - Get Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if label (system) exists" # TODO: Replace REST call when GH CLI paging support is available $params = @{ @@ -104,6 +104,7 @@ function Invoke-AzOpsGitPull { $response = Invoke-RestMethod -Method "Post" @params } + # GitHub PUll Request - List Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" # TODO: Replace REST call when GH CLI paging support is available $params = @{ @@ -177,59 +178,39 @@ function Invoke-AzOpsGitPull { } } "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "rest" -Message "Checking if pull request exists" + Write-AzOpsLog -Level Information -Topic "az" -Message "Checking if pull request exists" $response = Start-AzOpsNativeExecution { - az repos pr list --output json - } | ConvertFrom-Json | ForEach-Object { $_ | Where-Object -FilterScript {$_.sourceRefName -eq "refs/heads/system"} } - - Write-AzOpsLog -Level Information -Topic "az" -Message "$($response)" - - # $params = @{ - # Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?searchCriteria.sourceRefName=refs/heads/system&searchCriteria.targetRefName=refs/heads/main&searchCriteria.status=active&api-version=5.1" - # Method = "Get" - # Headers = @{ - # "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) - # "Content-Type" = "application/json" - # } - # } - # $response = Invoke-RestMethod @params - - # if ($null -ne $response) { - # Write-AzOpsLog -Level Information -Topic "rest" -Message "Creating new pull request" - # $params = @{ - # Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests?api-version=5.1" - # Method = "Post" - # Headers = @{ - # "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) - # "Content-Type" = "application/json" - # } - # Body = (@{ - # "sourceRefName" = "refs/heads/system" - # "targetRefName" = "refs/heads/main" - # "title" = "$env:GITHUB_PULL_REQUEST" - # "description" = "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" - # } | ConvertTo-Json -Depth 5) - # } - # $response = Invoke-RestMethod @params - - # Write-AzOpsLog -Level Information -Topic "rest" -Message "Assigning pull request label" - - # $params = @{ - # Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($response.pullRequestId)/labels?api-version=5.1-preview.1" - # Method = "Post" - # Headers = @{ - # "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) - # "Content-Type" = "application/json" - # } - # Body = (@{ - # "name" = "system" - # } | ConvertTo-Json -Depth 5) - # } - # Invoke-RestMethod @params - # } + az repos pr list --status active --output json + } | ConvertFrom-Json | ForEach-Object { $_ | Where-Object -FilterScript { $_.sourceRefName -eq "refs/heads/system" } } + + # Azure DevOps Pull Request - Create + if ($null -eq $response) { + Write-AzOpsLog -Level Information -Topic "az" -Message "Creating new pull request" + Start-AzOpsNativeExecution { + az repos pr create --source-branch "refs/heads/system" --target-branch "refs/heads/main" --title $global:AzDevOpsPullRequest --description "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" + } | Out-Host + } + else { + Write-AzOpsLog -Level Information -Topic "az" -Message "Skipping pull request creation" + } + + # Azure DevOps Pull Request - Merge + if ($global:AzDevOpsAutoMerge -eq 1) { + Write-AzOpsLog -Level Information -Topic "az" -Message "Retrieving new pull request" + $response = Start-AzOpsNativeExecution { + az repos pr list --status active --source-branch "refs/heads/system" --target-branch "refs/heads/main" --output json + } | ConvertFrom-Json + + Write-AzOpsLog -Level Information -Topic "az" -Message "Merging new pull request" + Start-AzOpsNativeExecution { + az repos pr update --id $response.pullRequestId --auto-complete --delete-source-branch --status completed --squash true + } + } + + } default { - Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" + Write-AzOpsLog -Level Error -Topic "none" -Message "Could not determine SCM platform. Current value is $global:SCMPlatform" } } } diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 4f593fc1..969235e2 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -51,30 +51,51 @@ function Invoke-AzOpsGitPush { git reset --hard } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking if local branch ($global:GitHubHeadRef) exists" - $branch = Start-AzOpsNativeExecution { - git branch --list $global:GitHubHeadRef - } - - if ($branch) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:GitHubHeadRef - } | Out-Host - } - else { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out new local branch ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git checkout -b $global:GitHubHeadRef origin/$global:GitHubHeadRef - } | Out-Host + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking if local branch ($global:GitHubHeadRef) exists" + $branch = Start-AzOpsNativeExecution { + git branch --list $global:GitHubHeadRef + } + + if ($branch) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:GitHubHeadRef + } | Out-Host + } + else { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out new local branch ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git checkout -b $global:GitHubHeadRef origin/$global:GitHubHeadRef + } | Out-Host + } + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking if local branch ($global:AzDevOpsHeadRef) exists" + $branch = Start-AzOpsNativeExecution { + git branch --list $global:AzDevOpsHeadRef + } + + if ($branch) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:AzDevOpsHeadRef + } | Out-Host + } + else { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out new local branch ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git checkout -b $global:AzDevOpsHeadRef origin/$global:AzDevOpsHeadRef + } | Out-Host + } + } } if ($diff) { Write-AzOpsLog -Level Information -Topic "git" -Message "Formatting diff changes" $diff = $diff -join "," - } - if ($null -ne $diff) { Write-AzOpsLog -Level Information -Topic "git" -Message "Changes:" $output = @() $diff.Split(",") | ForEach-Object { @@ -117,26 +138,31 @@ function Invoke-AzOpsGitPush { } } "AzureDevOps" { - Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $env:INPUT_ADO_COMMENTS" - $params = @{ - Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/threads?api-version=5.1" - Method = "Post" - Headers = @{ - "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) - "Content-Type" = "application/json" - } - Body = (@{ - comments = @( - (@{ - "parentCommentId" = 0 - "content" = "$(Get-Content -Path "$PSScriptRoot/../Comments-ado.md" -Raw)" - "commentType" = 1 - }) - ) - } | ConvertTo-Json -Depth 5) - } - Invoke-RestMethod @params | Out-Null - exit 1 + # if ($env:AZDEVOPS_STRICT_MODE -eq 1) { + # Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $env:INPUT_ADO_COMMENTS" + # $params = @{ + # Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/threads?api-version=5.1" + # Method = "Post" + # Headers = @{ + # "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) + # "Content-Type" = "application/json" + # } + # Body = (@{ + # comments = @( + # (@{ + # "parentCommentId" = 0 + # "content" = "$(Get-Content -Path "$PSScriptRoot/../Comments-ado.md" -Raw)" + # "commentType" = 1 + # }) + # ) + # } | ConvertTo-Json -Depth 5) + # } + # Invoke-RestMethod @params | Out-Null + # exit 1 + # } + # if ($env:AZDEVOPS_STRICT_MODE -eq 0) { + + # } } default { Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" @@ -233,20 +259,30 @@ function Invoke-AzOpsGitPush { git fetch origin } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:GitHubHeadRef - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:GitHubHeadRef) changes" - Start-AzOpsNativeExecution { - git pull origin $global:GitHubHeadRef - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "git" -Message "Merging origin branch ($global:GitHubBaseRef) changes" - Start-AzOpsNativeExecution { - git merge origin/$global:GitHubHeadRef --no-commit - } | Out-Host + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:GitHubHeadRef + } | Out-Host + + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:GitHubHeadRef) changes" + Start-AzOpsNativeExecution { + git pull origin $global:GitHubHeadRef + } | Out-Host + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:AzDevOpsHeadRef + } | Out-Host + + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:AzDevOpsHeadRef) changes" + Start-AzOpsNativeExecution { + git pull origin $global:AzDevOpsHeadRef + } | Out-Host + } + } Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy @@ -267,10 +303,20 @@ function Invoke-AzOpsGitPush { git commit -m 'System commit' } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git push origin $global:GitHubHeadRef - } | Out-Host + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git push origin $global:GitHubHeadRef + } | Out-Host + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git push origin $global:AzDevOpsHeadRef + } | Out-Host + } + } } } From 6e38d7a1294068579e52ea47117cff2744ca00b9 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Mon, 3 Aug 2020 14:16:15 +0100 Subject: [PATCH 09/33] Fix export raw templates env var --- src/public/Initialize-AzOpsRepository.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/Initialize-AzOpsRepository.ps1 b/src/public/Initialize-AzOpsRepository.ps1 index 1aa04ec7..e1c93095 100644 --- a/src/public/Initialize-AzOpsRepository.ps1 +++ b/src/public/Initialize-AzOpsRepository.ps1 @@ -92,7 +92,7 @@ function Initialize-AzOpsRepository { } # Set environment variable ExportRawTemplate to 1 if switch ExportRawTemplate switch has been used if ($PSBoundParameters['ExportRawTemplate']) { - $env:ExportRawTemplate = 1 + $env:AZOPS_EXPORT_RAW_TEMPLATES = 1 } # Initialize Global Variables and return error if not set Initialize-AzOpsGlobalVariables From 8e6c03ffffc4ba69e4a1e1a22a9725b515f86a30 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Mon, 3 Aug 2020 14:16:53 +0100 Subject: [PATCH 10/33] Fix auxiliary path --- src/public/Invoke-AzOpsGitPush.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 969235e2..1647d70e 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -115,7 +115,7 @@ function Invoke-AzOpsGitPush { "Authorization" = ("Bearer " + $global:GitHubToken) } Body = (@{ - "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/strict/README.md" -Raw) `n Changes: `n`n$output" + "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/strict/github/README.md" -Raw) `n Changes: `n`n$output" } | ConvertTo-Json) } Invoke-RestMethod -Method "POST" -Uri $global:GitHubComments @params | Out-Null From 9b5bd77cb251e700dcd8b080bfba33c88e08720e Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Mon, 3 Aug 2020 14:19:16 +0100 Subject: [PATCH 11/33] Update comment formatting --- src/private/ConvertTo-AzOpsState.ps1 | 2 +- src/private/Get-AzOpsAllSubscription.ps1 | 2 +- src/private/Get-AzOpsResourceDefinitionAtScope.ps1 | 8 ++++---- src/private/New-AzOpsStateDeployment.ps1 | 2 +- src/public/Initialize-AzOpsGlobalVariables.ps1 | 2 +- src/public/Initialize-AzOpsRepository.ps1 | 4 ++-- tests/AzOps.IAB.Tests.ps1 | 5 ++--- 7 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/private/ConvertTo-AzOpsState.ps1 b/src/private/ConvertTo-AzOpsState.ps1 index 8f93462a..72091c72 100644 --- a/src/private/ConvertTo-AzOpsState.ps1 +++ b/src/private/ConvertTo-AzOpsState.ps1 @@ -282,7 +282,7 @@ function ConvertTo-AzOpsState { } } else { - Write-AzOpsLog -Level Warning -Topic "ConvertTo-AzOpsState" -Message "Unable to find valid object to convert." + Write-AzOpsLog -Level Warning -Topic "ConvertTo-AzOpsState" -Message "Unable to find valid object to convert" } } diff --git a/src/private/Get-AzOpsAllSubscription.ps1 b/src/private/Get-AzOpsAllSubscription.ps1 index 084a8536..a43f98d0 100644 --- a/src/private/Get-AzOpsAllSubscription.ps1 +++ b/src/private/Get-AzOpsAllSubscription.ps1 @@ -42,7 +42,7 @@ function Get-AzOpsAllSubscription { $IncludedSubscriptions = $AllSubscriptions | Where-Object { $_.state -notin $ExcludedStates -and $_.subscriptionPolicies.quotaId -notin $ExcludedOffers } # Validate that subscriptions were found if ($null -eq $IncludedSubscriptions) { - Write-AzOpsLog -Level Error -Topic "Get-AzOpsAllSubscription" -Message "Found [$($IncludedSubscriptions.count)] subscriptions - verify appropriate permissions or that excluded offers and states are correct." + Write-AzOpsLog -Level Error -Topic "Get-AzOpsAllSubscription" -Message "Found [$($IncludedSubscriptions.count)] subscriptions - verify appropriate permissions or that excluded offers and states are correct" } else { # Calculate no of excluded subscriptions diff --git a/src/private/Get-AzOpsResourceDefinitionAtScope.ps1 b/src/private/Get-AzOpsResourceDefinitionAtScope.ps1 index dd987069..bb087193 100644 --- a/src/private/Get-AzOpsResourceDefinitionAtScope.ps1 +++ b/src/private/Get-AzOpsResourceDefinitionAtScope.ps1 @@ -143,11 +143,11 @@ function Get-AzOpsResourceDefinitionAtScope { catch { if ($retryCount -lt $maxRetryCount) { $sleepTimeInSeconds = [math]::Pow($backoffMultiplier, $retryCount) - Write-AzOpsLog -Level Warning -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "Caught error finding Resource Groups (retryCount=$retryCount). Waiting for $sleepTimeInSeconds seconds." + Write-AzOpsLog -Level Warning -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "Caught error finding Resource Groups (retryCount=$retryCount). Waiting for $sleepTimeInSeconds seconds" Start-Sleep -Seconds $sleepTimeInSeconds } elseif ($retryCount -ge $maxRetryCount) { - Write-AzOpsLog -Level Warning -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "Timeout exporting Resource Groups from Subscription $($context.Subscription.Id)." + Write-AzOpsLog -Level Warning -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "Timeout exporting Resource Groups from Subscription $($context.Subscription.Id)" Write-AzOpsLog -Level Error -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "$($_.Exception.Message | Out-String)" break } @@ -206,11 +206,11 @@ function Get-AzOpsResourceDefinitionAtScope { catch { if ($retryCount -lt $maxRetryCount) { $sleepTimeInSeconds = [math]::Pow($backoffMultiplier, $retryCount) - Write-AzOpsLog -Level Warning -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "Caught error finding Resources (retryCount=$retryCount). Waiting for $sleepTimeInSeconds seconds." + Write-AzOpsLog -Level Warning -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "Caught error finding Resources (retryCount=$retryCount). Waiting for $sleepTimeInSeconds seconds" Start-Sleep -Seconds $sleepTimeInSeconds } elseif ($retryCount -ge $maxRetryCount) { - Write-AzOpsLog -Level Warning -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "Timeout exporting Resources from Resource Group [$($rg.ResourceGroupName)]." + Write-AzOpsLog -Level Warning -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "Timeout exporting Resources from Resource Group [$($rg.ResourceGroupName)]" Write-AzOpsLog -Level Error -Topic "Get-AzOpsResourceDefinitionAtScope" -Message "$($_.Exception.Message | Out-String)" break } diff --git a/src/private/New-AzOpsStateDeployment.ps1 b/src/private/New-AzOpsStateDeployment.ps1 index ca09bc49..e11061e4 100644 --- a/src/private/New-AzOpsStateDeployment.ps1 +++ b/src/private/New-AzOpsStateDeployment.ps1 @@ -187,7 +187,7 @@ function New-AzOpsStateDeployment { New-AzResourceGroupDeployment -ResourceGroupName $scope.resourcegroup -TemplateFile $templatePath -TemplateParameterFile $filename -Name $deploymentName } else { - Write-AzOpsLog -Level Error -Topic "New-AzOpsStateDeployment" -Message "Resource Group [$($scope.resourcegroup)] not found. Unable to initiate deployment." + Write-AzOpsLog -Level Error -Topic "New-AzOpsStateDeployment" -Message "Resource Group [$($scope.resourcegroup)] not found. Unable to initiate deployment" } } elseif ($scope.subscription -and $PSCmdlet.ShouldProcess("Start Subscription Deployment?")) { diff --git a/src/public/Initialize-AzOpsGlobalVariables.ps1 b/src/public/Initialize-AzOpsGlobalVariables.ps1 index ed432b74..814e594a 100644 --- a/src/public/Initialize-AzOpsGlobalVariables.ps1 +++ b/src/public/Initialize-AzOpsGlobalVariables.ps1 @@ -177,7 +177,7 @@ function Initialize-AzOpsGlobalVariables { } else { - Write-AzOpsLog -Level Error -Topic "Initialize-AzOpsGlobalVariables" -Message "Cannot access root management group $RootScope. Verify that principal $((Get-AzContext).Account.Id) have access or set env:AZOPS_SUPPORT_PARTIAL_MG_DISCOVERY to 1 for partial discovery support." + Write-AzOpsLog -Level Error -Topic "Initialize-AzOpsGlobalVariables" -Message "Cannot access root management group $RootScope. Verify that principal $((Get-AzContext).Account.Id) have access or set env:AZOPS_SUPPORT_PARTIAL_MG_DISCOVERY to 1 for partial discovery support" } } diff --git a/src/public/Initialize-AzOpsRepository.ps1 b/src/public/Initialize-AzOpsRepository.ps1 index e1c93095..e182ccfa 100644 --- a/src/public/Initialize-AzOpsRepository.ps1 +++ b/src/public/Initialize-AzOpsRepository.ps1 @@ -97,7 +97,7 @@ function Initialize-AzOpsRepository { # Initialize Global Variables and return error if not set Initialize-AzOpsGlobalVariables if (-not (Test-AzOpsVariables)) { - Write-AzOpsLog -Level Error -Topic "Initialize-AzOpsRepository" -Message "AzOps Global Variables not set." + Write-AzOpsLog -Level Error -Topic "Initialize-AzOpsRepository" -Message "AzOps Global Variables not set" } # Get tenant id for current Az Context $TenantId = (Get-AzContext).Tenant.Id @@ -123,7 +123,7 @@ function Initialize-AzOpsRepository { #Handle migration from old folder structure by checking for parenthesis pattern $MigrationRequired = (Get-ChildItem -Recurse -Force -Path $global:AzOpsState -File | Where-Object { $_.Name -like "Microsoft.Management-managementGroups_$TenantId.parameters.json" } | Select-Object -ExpandProperty FullName -First 1) -notmatch '\((.*)\)' if ($MigrationRequired) { - Write-AzOpsLog -Level Verbose -Topic "Initialize-AzOpsRepository" -Message "Migration from old to new structure required. All artifacts will be lost." + Write-AzOpsLog -Level Verbose -Topic "Initialize-AzOpsRepository" -Message "Migration from old to new structure required. All artifacts will be lost" } if ($PSBoundParameters['Force'] -or $true -eq $MigrationRequired) { # Force will delete $global:AzOpsState directory diff --git a/tests/AzOps.IAB.Tests.ps1 b/tests/AzOps.IAB.Tests.ps1 index 824bfceb..2aa99c80 100644 --- a/tests/AzOps.IAB.Tests.ps1 +++ b/tests/AzOps.IAB.Tests.ps1 @@ -64,7 +64,7 @@ Describe "Tenant E2E Deployment (Integration Test)" -Tag "integration", "e2e", " Write-AzOpsLog -Level Information -Topic "AzOps.IAB.Tests" -Message "Running Remove-AzOpsManagementGroup" Remove-AzOpsManagementGroup -GroupName 'Tailspin' } - Write-AzOpsLog -Level Information -Topic "AzOps.IAB.Tests" -Message "Tailspin Management Group hierarchy removed." + Write-AzOpsLog -Level Information -Topic "AzOps.IAB.Tests" -Message "Tailspin Management Group hierarchy removed" #endregion # Task: Initialize azops/ @@ -228,8 +228,7 @@ Describe "Tenant E2E Deployment (Integration Test)" -Tag "integration", "e2e", " AfterAll { # Cleaning up Tailspin Management Group - if(Get-AzManagementGroup -GroupName 'Tailspin' -ErrorAction SilentlyContinue) - { + if (Get-AzManagementGroup -GroupName 'Tailspin' -ErrorAction SilentlyContinue) { Write-AzOpsLog -Level Verbose -Topic "AzOps.IAB.Tests" -Message "Cleaning up Tailspin Management Group" Remove-AzOpsManagementGroup -groupName 'Tailspin' } From 271ec34ca96e720e128a2ff1d126607f53b4e31c Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Mon, 3 Aug 2020 16:14:07 +0100 Subject: [PATCH 12/33] Add branch name parsing --- entrypoint.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/entrypoint.ps1 b/entrypoint.ps1 index aea439d9..db0b5116 100644 --- a/entrypoint.ps1 +++ b/entrypoint.ps1 @@ -49,6 +49,12 @@ function Initialization { } } + # Update branch names + if ($env:SCM_PLATFORM -eq "AzureDevOps") { + $env:AZDEVOPS_HEAD_BRANCH = ($env:AZDEVOPS_HEAD_BRANCH).Replace("refs/heads/", "") + $env:AZDEVOPS_BASE_BRANCH = ($env:AZDEVOPS_BASE_BRANCH).Replace("refs/heads/", "") + } + # Print environment variables Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_STATE: $($env:AZOPS_STATE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_INVALIDATE_CACHE: $($env:AZOPS_INVALIDATE_CACHE)" From f802fa02c8b681130af49ad4fdcfa5b2be1f85ee Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Mon, 3 Aug 2020 16:14:49 +0100 Subject: [PATCH 13/33] Add azure devops global vars --- src/public/Initialize-AzOpsGlobalVariables.ps1 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/public/Initialize-AzOpsGlobalVariables.ps1 b/src/public/Initialize-AzOpsGlobalVariables.ps1 index 814e594a..b3c23830 100644 --- a/src/public/Initialize-AzOpsGlobalVariables.ps1 +++ b/src/public/Initialize-AzOpsGlobalVariables.ps1 @@ -66,8 +66,13 @@ function Initialize-AzOpsGlobalVariables { AZDEVOPS_EMAIL = @{ AzDevOpsEmail = $null } AZDEVOPS_USERNAME = @{ AzDevOpsUsername = $null } AZDEVOPS_PULL_REQUEST = @{ AzDevOpsPullRequest = $null } + AZDEVOPS_PULL_REQUEST_ID = @{ AzDevOpsPullRequestId = $null } AZDEVOPS_HEAD_REF = @{ AzDevOpsHeadRef = $null } AZDEVOPS_BASE_REF = @{ AzDevOpsBaseRef = $null } + AZDEVOPS_API_URL = @{ AzDevOpsApiUrl = $null } + AZDEVOPS_PROJECT_ID = @{ AzDevOpsProjectId = $null } + AZDEVOPS_REPOSITORY = @{ AzDevOpsRepository = $null } + AZDEVOPS_TOKEN = @{ AzDevOpsToken = $null } # GitHub GITHUB_AUTO_MERGE = @{ GitHubAutoMerge = 1 } # Auto merge pull requests for pull workflow GITHUB_EMAIL = @{ GitHubEmail = $null } From 400b0a0e94bb103d341ae13a09ad98bb5c65004a Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Mon, 3 Aug 2020 16:15:14 +0100 Subject: [PATCH 14/33] Add azure devops support --- src/public/Invoke-AzOpsGitPull.ps1 | 2 +- src/public/Invoke-AzOpsGitPush.ps1 | 76 ++++++++++++++++++------------ 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index 709025b6..774a1de4 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -170,7 +170,7 @@ function Invoke-AzOpsGitPull { "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/merge/README.md" -Raw)" } | ConvertTo-Json) } - Invoke-RestMethod -Method "POST" -Uri ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/issues/" + $response[0].number + "/comments") @params | Out-Null + Invoke-RestMethod -Method "Post" -Uri ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/issues/" + $response[0].number + "/comments") @params | Out-Null } } else { diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 1647d70e..01c4c6c0 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -107,62 +107,80 @@ function Invoke-AzOpsGitPush { switch ($global:SCMPlatform) { "GitHub" { if ($global:AzOpsStrictMode -eq 1) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Branch is not consistent with Azure" Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $global:GitHubComments" $params = @{ Headers = @{ "Authorization" = ("Bearer " + $global:GitHubToken) + "Content-Type" = "application/json" } Body = (@{ "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/strict/github/README.md" -Raw) `n Changes: `n`n$output" } | ConvertTo-Json) } - Invoke-RestMethod -Method "POST" -Uri $global:GitHubComments @params | Out-Null + Invoke-RestMethod -Method "Post" -Uri $global:GitHubComments @params | Out-Null exit 1 } if ($global:AzOpsStrictMode -eq 0) { Write-AzOpsLog -Level Warning -Topic "git" -Message "Default Mode" - Write-AzOpsLog -Level Information -Topic "git" -Message "Changes:" Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $global:GitHubComments" $params = @{ Headers = @{ "Authorization" = ("Bearer " + $global:GitHubToken) + "Content-Type" = "application/json" } Body = (@{ "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/default/README.md" -Raw) `n Changes: `n`n$output" } | ConvertTo-Json) } - Invoke-RestMethod -Method "POST" -Uri $global:GitHubComments @params | Out-Null + Invoke-RestMethod -Method "Post" -Uri $global:GitHubComments @params | Out-Null } } "AzureDevOps" { - # if ($env:AZDEVOPS_STRICT_MODE -eq 1) { - # Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $env:INPUT_ADO_COMMENTS" - # $params = @{ - # Uri = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$($env:SYSTEM_TEAMPROJECTID)/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/pullRequests/$($env:SYSTEM_PULLREQUEST_PULLREQUESTID)/threads?api-version=5.1" - # Method = "Post" - # Headers = @{ - # "Authorization" = ("Bearer " + $env:SYSTEM_ACCESSTOKEN) - # "Content-Type" = "application/json" - # } - # Body = (@{ - # comments = @( - # (@{ - # "parentCommentId" = 0 - # "content" = "$(Get-Content -Path "$PSScriptRoot/../Comments-ado.md" -Raw)" - # "commentType" = 1 - # }) - # ) - # } | ConvertTo-Json -Depth 5) - # } - # Invoke-RestMethod @params | Out-Null - # exit 1 - # } - # if ($env:AZDEVOPS_STRICT_MODE -eq 0) { - - # } + if ($global:AzOpsStrictMode -eq 1) { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" + $params = @{ + Url = ($global:AzDevOpsApiUrl + $global:AzDevOpsProjectId + "/_apis/git/repositories/" + $global:AzDevOpsRepository + "/pullRequests/" + $global:AzDevOpsPullRequestId + "/threads?api-version=5.1") + Method = "Post" + Headers = @{ + "Authorization" = ("Bearer " + $global:AzDevOpsToken) + "Content-Type" = "application/json" + } + Body = (@{ + comments = @( + (@{ + "parentCommentId" = 0 + "content" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/strict/azdevops/README.md" -Raw) `n Changes: `n`n$output" + "commentType" = 1 + }) + ) + } | ConvertTo-Json -Depth 5) + } + Invoke-RestMethod @params + exit 1 + } + if ($global:AzOpsStrictMode -eq 0) { + Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" + $params = @{ + Url = ($global:AzDevOpsApiUrl + $global:AzDevOpsProjectId + "/_apis/git/repositories/" + $global:AzDevOpsRepository + "/pullRequests/" + $global:AzDevOpsPullRequestId + "/threads?api-version=5.1") + Method = "Post" + Headers = @{ + "Authorization" = ("Bearer " + $global:AzDevOpsToken) + "Content-Type" = "application/json" + } + Body = (@{ + comments = @( + (@{ + "parentCommentId" = 0 + "content" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/default/README.md" -Raw) `n Changes: `n`n$output" + "commentType" = 1 + }) + ) + } | ConvertTo-Json -Depth 5) + } + Invoke-RestMethod @params + } } default { Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" From 4d92162c63e1bd008e802a7efb487d8ea4f0e946 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Tue, 4 Aug 2020 21:22:22 +0100 Subject: [PATCH 15/33] Update entrypoint.ps1 --- entrypoint.ps1 | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/entrypoint.ps1 b/entrypoint.ps1 index db0b5116..18819d6c 100644 --- a/entrypoint.ps1 +++ b/entrypoint.ps1 @@ -45,36 +45,34 @@ function Initialization { git config --global user.name $env:AZDEVOPS_USERNAME git config --global user.email $env:AZDEVOPS_EMAIL } | Out-Host - } } # Update branch names - if ($env:SCM_PLATFORM -eq "AzureDevOps") { - $env:AZDEVOPS_HEAD_BRANCH = ($env:AZDEVOPS_HEAD_BRANCH).Replace("refs/heads/", "") - $env:AZDEVOPS_BASE_BRANCH = ($env:AZDEVOPS_BASE_BRANCH).Replace("refs/heads/", "") + if (($env:SCM_PLATFORM -eq "AzureDevOps") -and ($env:INPUT_MODE -eq "Push")) { + $env:AZDEVOPS_HEAD_REF = ($env:AZDEVOPS_HEAD_REF).Replace("refs/heads/", "") + $env:AZDEVOPS_BASE_REF = ($env:AZDEVOPS_BASE_REF).Replace("refs/heads/", "") } # Print environment variables + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_MODE: $($env:INPUT_MODE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_STATE: $($env:AZOPS_STATE)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_INVALIDATE_CACHE: $($env:AZOPS_INVALIDATE_CACHE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_ENROLLMENT_ACCOUNT: $($env:AZOPS_ENROLLMENT_ACCOUNT)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_OFFER_TYPE: $($env:AZOPS_OFFER_TYPE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_DEFAULT_DEPLOYMENT_REGION: $($env:AZOPS_DEFAULT_DEPLOYMENT_REGION)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_INVALIDATE_CACHE: $($env:AZOPS_INVALIDATE_CACHE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_IGNORE_CONTEXT_CHECK: $($env:AZOPS_IGNORE_CONTEXT_CHECK)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_OFFER_TYPE: $($env:AZOPS_OFFER_TYPE)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_STRICT_MODE: $($env:AZOPS_STRICT_MODE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_THROTTLE_LIMIT: $($env:AZOPS_THROTTLE_LIMIT)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_STRICT_MODE: $($env:AZOPS_STRICT_MODE)" switch ($env:SCM_PLATFORM) { "GitHub" { - Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_BASE_REF: $($env:GITHUB_BASE_REF)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_COMMENTS: $($env:GITHUB_COMMENTS)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_AUTO_MERGE: $($env:GITHUB_AUTO_MERGE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_EMAIL: $($env:GITHUB_EMAIL)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_HEAD_REF: $($env:GITHUB_HEAD_REF)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_ISSUE: $($env:GITHUB_ISSUE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_USERNAME: $($env:GITHUB_USERNAME)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_PULL_REQUEST: $($env:GITHUB_PULL_REQUEST)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_AUTO_MERGE: $($env:GITHUB_AUTO_MERGE)" - + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_HEAD_REF: $($env:GITHUB_HEAD_REF)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_BASE_REF: $($env:GITHUB_BASE_REF)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_COMMENTS: $($env:GITHUB_COMMENTS)" } "AzureDevOps" { Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_AUTO_MERGE: $($env:AZDEVOPS_AUTO_MERGE)" From f4810020cec5b855e028c35486a2b16b10bbf13a Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Tue, 4 Aug 2020 21:29:34 +0100 Subject: [PATCH 16/33] Update Invoke-AzOpsGitPull.ps1 --- src/public/Invoke-AzOpsGitPull.ps1 | 46 ++++++++++++++++-------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index 6793bab0..774a1de4 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -118,9 +118,9 @@ function Invoke-AzOpsGitPull { # GitHub Pull Request - Create if (-not $response) { Write-AzOpsLog -Level Information -Topic "gh" -Message "Creating new pull request" - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) - } + Start-AzOpsNativeExecution { + gh pr create --title $global:GitHubPullRequest --body "Auto-generated PR triggered by Azure Resource Manager" --label "system" --repo $global:GitHubRepository + } | Out-Host } else { Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request creation" @@ -172,30 +172,30 @@ function Invoke-AzOpsGitPull { } Invoke-RestMethod -Method "Post" -Uri ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/issues/" + $response[0].number + "/comments") @params | Out-Null } + } + else { + Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request merge" + } + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "az" -Message "Checking if pull request exists" + $response = Start-AzOpsNativeExecution { + az repos pr list --status active --output json + } | ConvertFrom-Json | ForEach-Object { $_ | Where-Object -FilterScript { $_.sourceRefName -eq "refs/heads/system" } } + + # Azure DevOps Pull Request - Create + if ($null -eq $response) { + Write-AzOpsLog -Level Information -Topic "az" -Message "Creating new pull request" + Start-AzOpsNativeExecution { + az repos pr create --source-branch "refs/heads/system" --target-branch "refs/heads/main" --title $global:AzDevOpsPullRequest --description "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" + } | Out-Host + } + else { Write-AzOpsLog -Level Information -Topic "az" -Message "Skipping pull request creation" } # Azure DevOps Pull Request - Merge if ($global:AzDevOpsAutoMerge -eq 1) { - "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "az" -Message "Checking if pull request exists" - $response = Start-AzOpsNativeExecution { - az repos pr list --status active --output json - } | ConvertFrom-Json | ForEach-Object { $_ | Where-Object -FilterScript { $_.sourceRefName -eq "refs/heads/system" } } - - # Azure DevOps Pull Request - Create - if ($null -eq $response) { - Write-AzOpsLog -Level Information -Topic "az" -Message "Creating new pull request" - Start-AzOpsNativeExecution { - az repos pr create --source-branch "refs/heads/system" --target-branch "refs/heads/main" --title $global:AzDevOpsPullRequest --description "Auto-generated PR triggered by Azure Resource Manager `nNew or modified resources discovered in Azure" - } | Out-Host - } - else { - Write-AzOpsLog -Level Information -Topic "az" -Message "Skipping pull request creation" - } - end {} - - } if ($global:AzDevOpsAutoMerge -eq 1) { Write-AzOpsLog -Level Information -Topic "az" -Message "Retrieving new pull request" $response = Start-AzOpsNativeExecution { az repos pr list --status active --source-branch "refs/heads/system" --target-branch "refs/heads/main" --output json @@ -206,6 +206,8 @@ function Invoke-AzOpsGitPull { az repos pr update --id $response.pullRequestId --auto-complete --delete-source-branch --status completed --squash true } } + + } default { Write-AzOpsLog -Level Error -Topic "none" -Message "Could not determine SCM platform. Current value is $global:SCMPlatform" From 890c0b5ccd557aac32dc389f44bbac99c21d3b37 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Tue, 4 Aug 2020 21:31:10 +0100 Subject: [PATCH 17/33] Remove blank space --- src/public/Invoke-AzOpsGitPull.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index 774a1de4..a817c8cb 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -206,8 +206,6 @@ function Invoke-AzOpsGitPull { az repos pr update --id $response.pullRequestId --auto-complete --delete-source-branch --status completed --squash true } } - - } default { Write-AzOpsLog -Level Error -Topic "none" -Message "Could not determine SCM platform. Current value is $global:SCMPlatform" From fe321762361bbaa89f6d647d5bbb8eb617a39f05 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Tue, 4 Aug 2020 21:32:18 +0100 Subject: [PATCH 18/33] Fix parameter name --- src/public/Invoke-AzOpsGitPush.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 01c4c6c0..f2bf09f7 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -141,7 +141,7 @@ function Invoke-AzOpsGitPush { if ($global:AzOpsStrictMode -eq 1) { Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" $params = @{ - Url = ($global:AzDevOpsApiUrl + $global:AzDevOpsProjectId + "/_apis/git/repositories/" + $global:AzDevOpsRepository + "/pullRequests/" + $global:AzDevOpsPullRequestId + "/threads?api-version=5.1") + Uri = ($global:AzDevOpsApiUrl + $global:AzDevOpsProjectId + "/_apis/git/repositories/" + $global:AzDevOpsRepository + "/pullRequests/" + $global:AzDevOpsPullRequestId + "/threads?api-version=5.1") Method = "Post" Headers = @{ "Authorization" = ("Bearer " + $global:AzDevOpsToken) @@ -163,7 +163,7 @@ function Invoke-AzOpsGitPush { if ($global:AzOpsStrictMode -eq 0) { Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" $params = @{ - Url = ($global:AzDevOpsApiUrl + $global:AzDevOpsProjectId + "/_apis/git/repositories/" + $global:AzDevOpsRepository + "/pullRequests/" + $global:AzDevOpsPullRequestId + "/threads?api-version=5.1") + Uri = ($global:AzDevOpsApiUrl + $global:AzDevOpsProjectId + "/_apis/git/repositories/" + $global:AzDevOpsRepository + "/pullRequests/" + $global:AzDevOpsPullRequestId + "/threads?api-version=5.1") Method = "Post" Headers = @{ "Authorization" = ("Bearer " + $global:AzDevOpsToken) From 4d53b068cf00f801d99f26c04238279918658e91 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Tue, 4 Aug 2020 22:37:38 +0100 Subject: [PATCH 19/33] Update default scm logic --- entrypoint.ps1 | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/entrypoint.ps1 b/entrypoint.ps1 index 18819d6c..68688967 100644 --- a/entrypoint.ps1 +++ b/entrypoint.ps1 @@ -33,19 +33,18 @@ function Initialization { # Configure git switch ($env:SCM_PLATFORM) { - "GitHub" { - Start-AzOpsNativeExecution { - git config --global user.name $env:GITHUB_USERNAME - git config --global user.email $env:GITHUB_EMAIL - } | Out-Host - - } "AzureDevOps" { Start-AzOpsNativeExecution { git config --global user.name $env:AZDEVOPS_USERNAME git config --global user.email $env:AZDEVOPS_EMAIL } | Out-Host } + default { + Start-AzOpsNativeExecution { + git config --global user.name $env:GITHUB_USERNAME + git config --global user.email $env:GITHUB_EMAIL + } | Out-Host + } } # Update branch names @@ -65,7 +64,13 @@ function Initialization { Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_THROTTLE_LIMIT: $($env:AZOPS_THROTTLE_LIMIT)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZOPS_STRICT_MODE: $($env:AZOPS_STRICT_MODE)" switch ($env:SCM_PLATFORM) { - "GitHub" { + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_AUTO_MERGE: $($env:AZDEVOPS_AUTO_MERGE)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_EMAIL: $($env:AZDEVOPS_EMAIL)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_USERNAME: $($env:AZDEVOPS_USERNAME)" + Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_PULL_REQUEST: $($env:AZDEVOPS_PULL_REQUEST)" + } + default { Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_AUTO_MERGE: $($env:GITHUB_AUTO_MERGE)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_EMAIL: $($env:GITHUB_EMAIL)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_USERNAME: $($env:GITHUB_USERNAME)" @@ -74,12 +79,6 @@ function Initialization { Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_BASE_REF: $($env:GITHUB_BASE_REF)" Write-AzOpsLog -Level Information -Topic "env-var" -Message "GITHUB_COMMENTS: $($env:GITHUB_COMMENTS)" } - "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_AUTO_MERGE: $($env:AZDEVOPS_AUTO_MERGE)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_EMAIL: $($env:AZDEVOPS_EMAIL)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_USERNAME: $($env:AZDEVOPS_USERNAME)" - Write-AzOpsLog -Level Information -Topic "env-var" -Message "AZDEVOPS_PULL_REQUEST: $($env:AZDEVOPS_PULL_REQUEST)" - } } # Initialize global variables From bb4aa4f0fa11de12c81c720a3ffc5650efb1436c Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Tue, 4 Aug 2020 22:37:51 +0100 Subject: [PATCH 20/33] Set default scm platform --- src/public/Initialize-AzOpsGlobalVariables.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/Initialize-AzOpsGlobalVariables.ps1 b/src/public/Initialize-AzOpsGlobalVariables.ps1 index b3c23830..4fd2653e 100644 --- a/src/public/Initialize-AzOpsGlobalVariables.ps1 +++ b/src/public/Initialize-AzOpsGlobalVariables.ps1 @@ -85,7 +85,7 @@ function Initialize-AzOpsGlobalVariables { GITHUB_TOKEN = @{ GitHubToken = $null } # Built-in env var GITHUB_COMMENTS = @{ GitHubComments = $null } # Built-in env var # Source Control - SCM_PLATFORM = @{ SCMPlatform = $null } + SCM_PLATFORM = @{ SCMPlatform = "GitHub" } } # Iterate through each variable and take appropriate action foreach ($AzOpsEnv in $AzOpsEnvVariables.Keys) { From fcee6f1dd7b04fd12f0a23909953b22598996958 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Tue, 4 Aug 2020 23:27:12 +0100 Subject: [PATCH 21/33] Add pull request wait --- src/public/Invoke-AzOpsGitPull.ps1 | 43 +++++++----------------------- 1 file changed, 9 insertions(+), 34 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index a817c8cb..6278ffcf 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -126,6 +126,9 @@ function Invoke-AzOpsGitPull { Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request creation" } + # GitHub Pull Request - Wait + Start-Sleep -Seconds 5 + # GitHub Pull Request - Merge if ($global:GitHubAutoMerge -eq 1) { Write-AzOpsLog -Level Information -Topic "rest" -Message "Retrieving new pull request" @@ -138,40 +141,9 @@ function Invoke-AzOpsGitPull { $response = Invoke-RestMethod -Method "Get" @params Write-AzOpsLog -Level Information -Topic "gh" -Message "Merging new pull request" - $attempt = 1 - $retryCount = 3 - $unmerged = $true - do { - try { - Start-AzOpsNativeExecution { - gh pr merge $response[0].number --squash --delete-branch -R $global:GitHubRepository - } | Out-Host - $unmerged = $false - } - catch { - if ($attempt -gt $retryCount) { - $unmerged = $true - } - else { - Write-AzOpsLog -Level Warning -Topic "gh" -Message "Retrying pull request merge" - Start-Sleep -Seconds 5 - $attempt = $attempt + 1 - } - } - } - while ($unmerged) - - if ($unmerged -eq $true) { - $params = @{ - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) - } - Body = (@{ - "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/merge/README.md" -Raw)" - } | ConvertTo-Json) - } - Invoke-RestMethod -Method "Post" -Uri ($global:GitHubApiUrl + "/repos/" + $global:GitHubRepository + "/issues/" + $response[0].number + "/comments") @params | Out-Null - } + Start-AzOpsNativeExecution { + gh pr merge $response[0].number --squash --delete-branch -R $global:GitHubRepository + } | Out-Host } else { Write-AzOpsLog -Level Information -Topic "gh" -Message "Skipping pull request merge" @@ -194,6 +166,9 @@ function Invoke-AzOpsGitPull { Write-AzOpsLog -Level Information -Topic "az" -Message "Skipping pull request creation" } + # Azure DevOps Pull Request - Wait + Start-Sleep -Second 5 + # Azure DevOps Pull Request - Merge if ($global:AzDevOpsAutoMerge -eq 1) { Write-AzOpsLog -Level Information -Topic "az" -Message "Retrieving new pull request" From 02d72ae2e552d4eb1acf659a09b10b10e99ba6b6 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Wed, 5 Aug 2020 15:15:00 +0100 Subject: [PATCH 22/33] Update Invoke-AzOpsGitPush.ps1 --- src/public/Invoke-AzOpsGitPush.ps1 | 201 ++++++++++++----------------- 1 file changed, 83 insertions(+), 118 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index f2bf09f7..22d7c787 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -18,95 +18,97 @@ function Invoke-AzOpsGitPush { $skipPolicy = $false } - Write-AzOpsLog -Level Information -Topic "git" -Message "Fetching latest origin changes" - Start-AzOpsNativeExecution { - git fetch origin - } | Out-Host + if ($global:AzOpsStrictMode -eq 1) { + Write-AzOpsLog -Level Information -Topic "pwsh" -Message "AzOpsStrictMode is set to 1, verifying pull before push" + + Write-AzOpsLog -Level Information -Topic "git" -Message "Fetching latest origin changes" + Start-AzOpsNativeExecution { + git fetch origin + } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out origin branch (main)" - Start-AzOpsNativeExecution { - git checkout origin/main - } | Out-Host + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out origin branch (main)" + Start-AzOpsNativeExecution { + git checkout origin/main + } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch (main) changes" - Start-AzOpsNativeExecution { - git pull origin main - } | Out-Host + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch (main) changes" + Start-AzOpsNativeExecution { + git pull origin main + } | Out-Host - Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" - Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy + Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" + Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy - Write-AzOpsLog -Level Information -Topic "git" -Message "Adding azops file changes" - Start-AzOpsNativeExecution { - git add --intent-to-add $global:AzOpsState - } | Out-Host + Write-AzOpsLog -Level Information -Topic "git" -Message "Adding azops file changes" + Start-AzOpsNativeExecution { + git add --intent-to-add $global:AzOpsState + } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" - $diff = Start-AzOpsNativeExecution { - git diff --ignore-space-at-eol --name-status - } + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" + $diff = Start-AzOpsNativeExecution { + git diff --ignore-space-at-eol --name-status + } - Write-AzOpsLog -Level Information -Topic "git" -Message "Resetting local main branch" - Start-AzOpsNativeExecution { - git reset --hard - } | Out-Host + Write-AzOpsLog -Level Information -Topic "git" -Message "Resetting local main branch" + Start-AzOpsNativeExecution { + git reset --hard + } | Out-Host - switch ($global:SCMPlatform) { - "GitHub" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking if local branch ($global:GitHubHeadRef) exists" - $branch = Start-AzOpsNativeExecution { - git branch --list $global:GitHubHeadRef - } + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking if local branch ($global:GitHubHeadRef) exists" + $branch = Start-AzOpsNativeExecution { + git branch --list $global:GitHubHeadRef + } - if ($branch) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:GitHubHeadRef - } | Out-Host - } - else { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out new local branch ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git checkout -b $global:GitHubHeadRef origin/$global:GitHubHeadRef - } | Out-Host - } - } - "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking if local branch ($global:AzDevOpsHeadRef) exists" - $branch = Start-AzOpsNativeExecution { - git branch --list $global:AzDevOpsHeadRef + if ($branch) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:GitHubHeadRef + } | Out-Host + } + else { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out new local branch ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git checkout -b $global:GitHubHeadRef origin/$global:GitHubHeadRef + } | Out-Host + } } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking if local branch ($global:AzDevOpsHeadRef) exists" + $branch = Start-AzOpsNativeExecution { + git branch --list $global:AzDevOpsHeadRef + } - if ($branch) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:AzDevOpsHeadRef - } | Out-Host - } - else { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out new local branch ($global:AzDevOpsHeadRef)" - Start-AzOpsNativeExecution { - git checkout -b $global:AzDevOpsHeadRef origin/$global:AzDevOpsHeadRef - } | Out-Host + if ($branch) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:AzDevOpsHeadRef + } | Out-Host + } + else { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out new local branch ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git checkout -b $global:AzDevOpsHeadRef origin/$global:AzDevOpsHeadRef + } | Out-Host + } } } - } - if ($diff) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Formatting diff changes" - $diff = $diff -join "," + if ($diff) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Formatting diff changes" + $diff = $diff -join "," - Write-AzOpsLog -Level Information -Topic "git" -Message "Changes:" - $output = @() - $diff.Split(",") | ForEach-Object { - $output += ( "``" + $_ + "``") - $output += "`n`n" - Write-AzOpsLog -Level Information -Topic "git" -Message $_ - } + Write-AzOpsLog -Level Information -Topic "git" -Message "Changes:" + $output = @() + $diff.Split(",") | ForEach-Object { + $output += ( "``" + $_ + "``") + $output += "`n`n" + Write-AzOpsLog -Level Information -Topic "git" -Message $_ + } - switch ($global:SCMPlatform) { - "GitHub" { - if ($global:AzOpsStrictMode -eq 1) { + switch ($global:SCMPlatform) { + "GitHub" { Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $global:GitHubComments" $params = @{ @@ -121,24 +123,7 @@ function Invoke-AzOpsGitPush { Invoke-RestMethod -Method "Post" -Uri $global:GitHubComments @params | Out-Null exit 1 } - if ($global:AzOpsStrictMode -eq 0) { - Write-AzOpsLog -Level Warning -Topic "git" -Message "Default Mode" - Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" - Write-AzOpsLog -Level Verbose -Topic "rest" -Message "Uri: $global:GitHubComments" - $params = @{ - Headers = @{ - "Authorization" = ("Bearer " + $global:GitHubToken) - "Content-Type" = "application/json" - } - Body = (@{ - "body" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/default/README.md" -Raw) `n Changes: `n`n$output" - } | ConvertTo-Json) - } - Invoke-RestMethod -Method "Post" -Uri $global:GitHubComments @params | Out-Null - } - } - "AzureDevOps" { - if ($global:AzOpsStrictMode -eq 1) { + "AzureDevOps" { Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" $params = @{ Uri = ($global:AzDevOpsApiUrl + $global:AzDevOpsProjectId + "/_apis/git/repositories/" + $global:AzDevOpsRepository + "/pullRequests/" + $global:AzDevOpsPullRequestId + "/threads?api-version=5.1") @@ -160,35 +145,15 @@ function Invoke-AzOpsGitPush { Invoke-RestMethod @params exit 1 } - if ($global:AzOpsStrictMode -eq 0) { - Write-AzOpsLog -Level Information -Topic "rest" -Message "Writing comment to pull request" - $params = @{ - Uri = ($global:AzDevOpsApiUrl + $global:AzDevOpsProjectId + "/_apis/git/repositories/" + $global:AzDevOpsRepository + "/pullRequests/" + $global:AzDevOpsPullRequestId + "/threads?api-version=5.1") - Method = "Post" - Headers = @{ - "Authorization" = ("Bearer " + $global:AzDevOpsToken) - "Content-Type" = "application/json" - } - Body = (@{ - comments = @( - (@{ - "parentCommentId" = 0 - "content" = "$(Get-Content -Path "$PSScriptRoot/../auxiliary/guidance/default/README.md" -Raw) `n Changes: `n`n$output" - "commentType" = 1 - }) - ) - } | ConvertTo-Json -Depth 5) - } - Invoke-RestMethod @params + default { + Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $global:SCMPlatform" } } - default { - Write-AzOpsLog -Level Error -Topic "rest" -Message "Could not determine SCM platform from SCMPLATFORM. Current value is $env:SCMPLATFORM" - } } - } - else { - Write-AzOpsLog -Level Information -Topic "git" -Message "Branch is consistent with Azure" + else { + Write-AzOpsLog -Level Information -Topic "git" -Message "Branch is consistent with Azure" + } + } } From 7a06bbb6c87eb398852339ec687f0d83f782f785 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Wed, 5 Aug 2020 15:39:19 +0100 Subject: [PATCH 23/33] Update Invoke-AzOpsGitPush.ps1 --- src/public/Invoke-AzOpsGitPush.ps1 | 104 ++++++++++++++--------------- 1 file changed, 50 insertions(+), 54 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index a0269896..da70e8e6 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -163,11 +163,6 @@ function Invoke-AzOpsGitPush { } process { - # Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling latest changes" - # Start-AzOpsNativeExecution { - # git pull - # } | Out-Host - # Changes Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" $changeSet = @() @@ -246,65 +241,66 @@ function Invoke-AzOpsGitPush { git fetch origin } | Out-Host - switch ($global:SCMPlatform) { - "GitHub" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:GitHubHeadRef - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:GitHubHeadRef) changes" - Start-AzOpsNativeExecution { - git pull origin $global:GitHubHeadRef - } | Out-Host - } - "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:AzDevOpsHeadRef - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:AzDevOpsHeadRef) changes" - Start-AzOpsNativeExecution { - git pull origin $global:AzDevOpsHeadRef - } | Out-Host - } - } - - Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" - Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy - - Write-AzOpsLog -Level Information -Topic "git" -Message "Adding azops file changes" - Start-AzOpsNativeExecution { - git add $global:AzOpsState - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" - $status = Start-AzOpsNativeExecution { - git status --short - } - - if ($status) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Creating new commit" - Start-AzOpsNativeExecution { - git commit -m 'System commit' - } | Out-Host - + if ($global:AzOpsStrictMode -eq 1) { switch ($global:SCMPlatform) { "GitHub" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:GitHubHeadRef)" + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" Start-AzOpsNativeExecution { - git push origin $global:GitHubHeadRef + git checkout $global:GitHubHeadRef + } | Out-Host + + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:GitHubHeadRef) changes" + Start-AzOpsNativeExecution { + git pull origin $global:GitHubHeadRef } | Out-Host } "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:AzDevOpsHeadRef)" + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" Start-AzOpsNativeExecution { - git push origin $global:AzDevOpsHeadRef + git checkout $global:AzDevOpsHeadRef } | Out-Host + + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:AzDevOpsHeadRef) changes" + Start-AzOpsNativeExecution { + git pull origin $global:AzDevOpsHeadRef + } | Out-Host + } + } + + Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" + Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy + + Write-AzOpsLog -Level Information -Topic "git" -Message "Adding azops file changes" + Start-AzOpsNativeExecution { + git add $global:AzOpsState + } | Out-Host + + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" + $status = Start-AzOpsNativeExecution { + git status --short + } + + if ($status) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Creating new commit" + Start-AzOpsNativeExecution { + git commit -m 'System commit' + } | Out-Host + + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git push origin $global:GitHubHeadRef + } | Out-Host + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git push origin $global:AzDevOpsHeadRef + } | Out-Host + } } } } } - } \ No newline at end of file From 67046c44767eca369d009f035bbf101305133b97 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Wed, 5 Aug 2020 15:40:30 +0100 Subject: [PATCH 24/33] Update Invoke-AzOpsGitPush.ps1 --- src/public/Invoke-AzOpsGitPush.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index da70e8e6..1275916e 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -234,14 +234,14 @@ function Invoke-AzOpsGitPush { } end { - Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking post refresh process" - Write-AzOpsLog -Level Information -Topic "git" -Message "Fetching latest origin changes" Start-AzOpsNativeExecution { git fetch origin } | Out-Host if ($global:AzOpsStrictMode -eq 1) { + Write-AzOpsLog -Level Information -Topic "pwsh" -Message "AzOpsStrictMode is set to 1, verifying pull before push" + switch ($global:SCMPlatform) { "GitHub" { Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" From 93867399db1747951f2a56d8548892ce564ce2e0 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 12:36:46 +0100 Subject: [PATCH 25/33] Workflow test --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index d886332b..44c3281e 100644 --- a/action.yml +++ b/action.yml @@ -7,7 +7,7 @@ inputs: required: true runs: using: "docker" - image: "docker://mscet/azops:main" + image: "docker://ljtill/azops:latest" branding: icon: "sunrise" color: "green" From d8165a31923568e887b19a61a23eba919d5f37a8 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 12:50:09 +0100 Subject: [PATCH 26/33] Revert workflow test --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 44c3281e..d886332b 100644 --- a/action.yml +++ b/action.yml @@ -7,7 +7,7 @@ inputs: required: true runs: using: "docker" - image: "docker://ljtill/azops:latest" + image: "docker://mscet/azops:main" branding: icon: "sunrise" color: "green" From d90baf6ad88bc6ae58975756ea391139a2df8f18 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 13:55:46 +0100 Subject: [PATCH 27/33] Update push / pull workflow --- src/public/Invoke-AzOpsGitPull.ps1 | 2 +- src/public/Invoke-AzOpsGitPush.ps1 | 119 +++++++++++++++-------------- 2 files changed, 64 insertions(+), 57 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPull.ps1 b/src/public/Invoke-AzOpsGitPull.ps1 index 6278ffcf..5eaef39f 100644 --- a/src/public/Invoke-AzOpsGitPull.ps1 +++ b/src/public/Invoke-AzOpsGitPull.ps1 @@ -64,7 +64,7 @@ function Invoke-AzOpsGitPull { Write-AzOpsLog -Level Information -Topic "git" -Message "Creating new commit" Start-AzOpsNativeExecution { - git commit -m 'System commit' + git commit -m 'System pull commit' } | Out-Host Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin" diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 1275916e..d89d5a94 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -163,11 +163,20 @@ function Invoke-AzOpsGitPush { } process { - # Changes - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" - $changeSet = @() - $changeSet = Start-AzOpsNativeExecution { - git diff origin/main --ignore-space-at-eol --name-status + $commit = Start-AzOpsNativeExecution { + git log -1 --pretty=format:%s + } + + if ($commit -match "System push commit") { + Write-AzOpsLog -Level Information -Topic "git" -Message "Deployment not required" + $changeSet = @() + } + else { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" + $changeSet = @() + $changeSet = Start-AzOpsNativeExecution { + git diff origin/main --ignore-space-at-eol --name-status + } } if (!$changeSet) { @@ -239,66 +248,64 @@ function Invoke-AzOpsGitPush { git fetch origin } | Out-Host - if ($global:AzOpsStrictMode -eq 1) { - Write-AzOpsLog -Level Information -Topic "pwsh" -Message "AzOpsStrictMode is set to 1, verifying pull before push" + Write-AzOpsLog -Level Information -Topic "pwsh" -Message "AzOpsStrictMode is set to 1, verifying pull before push" - switch ($global:SCMPlatform) { - "GitHub" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:GitHubHeadRef - } | Out-Host + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:GitHubHeadRef + } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:GitHubHeadRef) changes" - Start-AzOpsNativeExecution { - git pull origin $global:GitHubHeadRef - } | Out-Host - } - "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:AzDevOpsHeadRef - } | Out-Host + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:GitHubHeadRef) changes" + Start-AzOpsNativeExecution { + git pull origin $global:GitHubHeadRef + } | Out-Host + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:AzDevOpsHeadRef + } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:AzDevOpsHeadRef) changes" - Start-AzOpsNativeExecution { - git pull origin $global:AzDevOpsHeadRef - } | Out-Host - } + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:AzDevOpsHeadRef) changes" + Start-AzOpsNativeExecution { + git pull origin $global:AzDevOpsHeadRef + } | Out-Host } + } - Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" - Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy + Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" + Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy - Write-AzOpsLog -Level Information -Topic "git" -Message "Adding azops file changes" - Start-AzOpsNativeExecution { - git add $global:AzOpsState - } | Out-Host + Write-AzOpsLog -Level Information -Topic "git" -Message "Adding azops file changes" + Start-AzOpsNativeExecution { + git add $global:AzOpsState + } | Out-Host - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" - $status = Start-AzOpsNativeExecution { - git status --short - } + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" + $status = Start-AzOpsNativeExecution { + git status --short + } - if ($status) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Creating new commit" - Start-AzOpsNativeExecution { - git commit -m 'System commit' - } | Out-Host + if ($status) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Creating new commit" + Start-AzOpsNativeExecution { + git commit -m 'System push commit' + } | Out-Host - switch ($global:SCMPlatform) { - "GitHub" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git push origin $global:GitHubHeadRef - } | Out-Host - } - "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:AzDevOpsHeadRef)" - Start-AzOpsNativeExecution { - git push origin $global:AzDevOpsHeadRef - } | Out-Host - } + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git push origin $global:GitHubHeadRef + } | Out-Host + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git push origin $global:AzDevOpsHeadRef + } | Out-Host } } } From bbb2bc3478975f8612ed364e3a74c3b39474500c Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 17:04:23 +0100 Subject: [PATCH 28/33] Update git push workflow --- src/public/Invoke-AzOpsGitPush.ps1 | 149 ++++++++++++++++------------- 1 file changed, 81 insertions(+), 68 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index d89d5a94..5d202820 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -163,20 +163,38 @@ function Invoke-AzOpsGitPush { } process { - $commit = Start-AzOpsNativeExecution { - git log -1 --pretty=format:%s - } + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" + $changeSet = @() + $changeSet = Start-AzOpsNativeExecution { + git diff origin/main --ignore-space-at-eol --name-status + } + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Switching to branch" + Start-AzOpsNativeExecution { + git checkout $global:AzDevOpsHeadRef + } | Out-Host + + $skipCommit = Start-AzOpsNativeExecution { + git log -1 --pretty=format:%s + } - if ($commit -match "System push commit") { - Write-AzOpsLog -Level Information -Topic "git" -Message "Deployment not required" - $changeSet = @() - } - else { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" - $changeSet = @() - $changeSet = Start-AzOpsNativeExecution { - git diff origin/main --ignore-space-at-eol --name-status - } + Write-Host "Commit message: $commit" + + if ($skipCommit -match "System push commit") { + Write-AzOpsLog -Level Information -Topic "git" -Message "Deployment not required" + $changeSet = @() + } + else { + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" + $changeSet = @() + $changeSet = Start-AzOpsNativeExecution { + git diff origin/main --ignore-space-at-eol --name-status + } + } + } } if (!$changeSet) { @@ -243,69 +261,64 @@ function Invoke-AzOpsGitPush { } end { - Write-AzOpsLog -Level Information -Topic "git" -Message "Fetching latest origin changes" - Start-AzOpsNativeExecution { - git fetch origin - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "pwsh" -Message "AzOpsStrictMode is set to 1, verifying pull before push" - - switch ($global:SCMPlatform) { - "GitHub" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:GitHubHeadRef - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:GitHubHeadRef) changes" - Start-AzOpsNativeExecution { - git pull origin $global:GitHubHeadRef - } | Out-Host - } - "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" - Start-AzOpsNativeExecution { - git checkout $global:AzDevOpsHeadRef - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:AzDevOpsHeadRef) changes" - Start-AzOpsNativeExecution { - git pull origin $global:AzDevOpsHeadRef - } | Out-Host - } - } - - Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" - Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy - - Write-AzOpsLog -Level Information -Topic "git" -Message "Adding azops file changes" - Start-AzOpsNativeExecution { - git add $global:AzOpsState - } | Out-Host - - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" - $status = Start-AzOpsNativeExecution { - git status --short - } - - if ($status) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Creating new commit" - Start-AzOpsNativeExecution { - git commit -m 'System push commit' - } | Out-Host - + if (-not($skipCommit -match "System push commit")) { switch ($global:SCMPlatform) { "GitHub" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:GitHubHeadRef)" + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git checkout $global:GitHubHeadRef + } | Out-Host + + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:GitHubHeadRef) changes" Start-AzOpsNativeExecution { - git push origin $global:GitHubHeadRef + git pull origin $global:GitHubHeadRef } | Out-Host } "AzureDevOps" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:AzDevOpsHeadRef)" + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:AzDevOpsHeadRef)" Start-AzOpsNativeExecution { - git push origin $global:AzDevOpsHeadRef + git checkout $global:AzDevOpsHeadRef } | Out-Host + + Write-AzOpsLog -Level Information -Topic "git" -Message "Pulling origin branch ($global:AzDevOpsHeadRef) changes" + Start-AzOpsNativeExecution { + git pull origin $global:AzDevOpsHeadRef + } | Out-Host + } + } + + Write-AzOpsLog -Level Information -Topic "Initialize-AzOpsRepository" -Message "Invoking repository initialization" + Initialize-AzOpsRepository -InvalidateCache -Rebuild -SkipResourceGroup:$skipResourceGroup -SkipPolicy:$skipPolicy + + Write-AzOpsLog -Level Information -Topic "git" -Message "Adding azops file changes" + Start-AzOpsNativeExecution { + git add $global:AzOpsState + } | Out-Host + + Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" + $status = Start-AzOpsNativeExecution { + git status --short + } + + if ($status) { + Write-AzOpsLog -Level Information -Topic "git" -Message "Creating new commit" + Start-AzOpsNativeExecution { + git commit -m 'System push commit' + } | Out-Host + + switch ($global:SCMPlatform) { + "GitHub" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:GitHubHeadRef)" + Start-AzOpsNativeExecution { + git push origin $global:GitHubHeadRef + } | Out-Host + } + "AzureDevOps" { + Write-AzOpsLog -Level Information -Topic "git" -Message "Pushing new changes to origin ($global:AzDevOpsHeadRef)" + Start-AzOpsNativeExecution { + git push origin $global:AzDevOpsHeadRef + } | Out-Host + } } } } From e1f0467fc4ed88d99cce239f870a96238e3453b8 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 17:59:48 +0100 Subject: [PATCH 29/33] Update push workflow --- src/public/Invoke-AzOpsGitPush.ps1 | 103 ++++++++++++++--------------- 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 5d202820..89c5c528 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -181,7 +181,7 @@ function Invoke-AzOpsGitPush { git log -1 --pretty=format:%s } - Write-Host "Commit message: $commit" + Write-Host "Commit message: $skipCommit" if ($skipCommit -match "System push commit") { Write-AzOpsLog -Level Information -Topic "git" -Message "Deployment not required" @@ -197,66 +197,65 @@ function Invoke-AzOpsGitPush { } } - if (!$changeSet) { - Write-AzOpsLog -Level Information -Topic "git" -Message "No changes detected" - } - else { + if ($changeSet) { Write-AzOpsLog -Level Information -Topic "git" -Message "Iterating through changes" - } - - $deleteSet = @() - $addModifySet = @() - foreach ($change in $changeSet) { - $filename = ($change -split "`t")[-1] - if (($change -split "`t" | Select-Object -first 1) -eq 'D') { - $deleteSet += $filename + + $deleteSet = @() + $addModifySet = @() + foreach ($change in $changeSet) { + $filename = ($change -split "`t")[-1] + if (($change -split "`t" | Select-Object -first 1) -eq 'D') { + $deleteSet += $filename + } + elseif (($change -split "`t" | Select-Object -first 1) -eq 'A' -or 'M' -or 'R') { + $addModifySet += $filename + } } - elseif (($change -split "`t" | Select-Object -first 1) -eq 'A' -or 'M' -or 'R') { - $addModifySet += $filename + + Write-AzOpsLog -Level Information -Topic "git" -Message "Add / Modify:" + $addModifySet | ForEach-Object { + Write-AzOpsLog -Level Information -Topic "git" -Message $_ } - } - Write-AzOpsLog -Level Information -Topic "git" -Message "Add / Modify:" - $addModifySet | ForEach-Object { - Write-AzOpsLog -Level Information -Topic "git" -Message $_ - } + Write-AzOpsLog -Level Information -Topic "git" -Message "Delete:" + $deleteSet | ForEach-Object { + Write-AzOpsLog -Level Information -Topic "git" -Message $_ + } - Write-AzOpsLog -Level Information -Topic "git" -Message "Delete:" - $deleteSet | ForEach-Object { - Write-AzOpsLog -Level Information -Topic "git" -Message $_ - } + $addModifySet ` + | Where-Object -FilterScript { $_ -match '/*.subscription.json$' } ` + | Sort-Object -Property $_ ` + | ForEach-Object { + Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking new state deployment - *.subscription.json for a file $_" + New-AzOpsStateDeployment -filename $_ + } - # Deployment - $addModifySet ` - | Where-Object -FilterScript { $_ -match '/*.subscription.json$' } ` - | Sort-Object -Property $_ ` - | ForEach-Object { - Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking new state deployment - *.subscription.json for a file $_" - New-AzOpsStateDeployment -filename $_ - } + $addModifySet ` + | Where-Object -FilterScript { $_ -match '/*.providerfeatures.json$' } ` + | Sort-Object -Property $_ ` + | ForEach-Object { + Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking new state deployment - *.providerfeatures.json for a file $_" + New-AzOpsStateDeployment -filename $_ + } - $addModifySet ` - | Where-Object -FilterScript { $_ -match '/*.providerfeatures.json$' } ` - | Sort-Object -Property $_ ` - | ForEach-Object { - Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking new state deployment - *.providerfeatures.json for a file $_" - New-AzOpsStateDeployment -filename $_ - } + $addModifySet ` + | Where-Object -FilterScript { $_ -match '/*.resourceproviders.json$' } ` + | Sort-Object -Property $_ ` + | ForEach-Object { + Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking new state deployment - *.resourceproviders.json for a file $_" + New-AzOpsStateDeployment -filename $_ + } - $addModifySet ` - | Where-Object -FilterScript { $_ -match '/*.resourceproviders.json$' } ` - | Sort-Object -Property $_ ` - | ForEach-Object { - Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking new state deployment - *.resourceproviders.json for a file $_" - New-AzOpsStateDeployment -filename $_ + $addModifySet ` + | Where-Object -FilterScript { $_ -match '/*.parameters.json$' } ` + | Sort-Object -Property $_ ` + | Foreach-Object { + Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking new state deployment - *.parameters.json for a file $_" + New-AzOpsStateDeployment -filename $_ + } } - - $addModifySet ` - | Where-Object -FilterScript { $_ -match '/*.parameters.json$' } ` - | Sort-Object -Property $_ ` - | Foreach-Object { - Write-AzOpsLog -Level Information -Topic "Invoke-AzOpsGitPush" -Message "Invoking new state deployment - *.parameters.json for a file $_" - New-AzOpsStateDeployment -filename $_ + else { + Write-AzOpsLog -Level Information -Topic "git" -Message "No changes detected" } } From 164110ecc5727d1a606c8814b6e97d68d404d883 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 17:59:59 +0100 Subject: [PATCH 30/33] Workflow test --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index d886332b..44c3281e 100644 --- a/action.yml +++ b/action.yml @@ -7,7 +7,7 @@ inputs: required: true runs: using: "docker" - image: "docker://mscet/azops:main" + image: "docker://ljtill/azops:latest" branding: icon: "sunrise" color: "green" From eda66f81266bc6d7683d2d8024722b15ae1db292 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 18:26:44 +0100 Subject: [PATCH 31/33] Add skip logic --- src/public/Invoke-AzOpsGitPush.ps1 | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index 89c5c528..f724d1a5 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -17,6 +17,9 @@ function Invoke-AzOpsGitPush { else { $skipPolicy = $false } + + # Skip AzDevOps Run + $skip = $false Write-AzOpsLog -Level Information -Topic "git" -Message "Fetching latest origin changes" Start-AzOpsNativeExecution { @@ -165,7 +168,6 @@ function Invoke-AzOpsGitPush { process { switch ($global:SCMPlatform) { "GitHub" { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" $changeSet = @() $changeSet = Start-AzOpsNativeExecution { git diff origin/main --ignore-space-at-eol --name-status @@ -176,19 +178,20 @@ function Invoke-AzOpsGitPush { Start-AzOpsNativeExecution { git checkout $global:AzDevOpsHeadRef } | Out-Host - - $skipCommit = Start-AzOpsNativeExecution { + + $commitMessage = Start-AzOpsNativeExecution { git log -1 --pretty=format:%s } + Write-AzOpsLog -Level Verbose -Topic "git" -Message "Commit message: $commitMessage" - Write-Host "Commit message: $skipCommit" - - if ($skipCommit -match "System push commit") { - Write-AzOpsLog -Level Information -Topic "git" -Message "Deployment not required" + if ($commitMessage -match "System push commit") { + $skip = $true + } + + if ($skipRun -eq $true) { $changeSet = @() } else { - Write-AzOpsLog -Level Information -Topic "git" -Message "Checking for additions / modifications / deletions" $changeSet = @() $changeSet = Start-AzOpsNativeExecution { git diff origin/main --ignore-space-at-eol --name-status @@ -198,7 +201,7 @@ function Invoke-AzOpsGitPush { } if ($changeSet) { - Write-AzOpsLog -Level Information -Topic "git" -Message "Iterating through changes" + Write-AzOpsLog -Level Information -Topic "git" -Message "Deployment required" $deleteSet = @() $addModifySet = @() @@ -255,12 +258,12 @@ function Invoke-AzOpsGitPush { } } else { - Write-AzOpsLog -Level Information -Topic "git" -Message "No changes detected" + Write-AzOpsLog -Level Information -Topic "git" -Message "Deployment not required" } } end { - if (-not($skipCommit -match "System push commit")) { + if ($skip -eq $false) { switch ($global:SCMPlatform) { "GitHub" { Write-AzOpsLog -Level Information -Topic "git" -Message "Checking out existing local branch ($global:GitHubHeadRef)" From 1db078889f96f600b7a3f1e7afb580aa19bf4595 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 18:55:17 +0100 Subject: [PATCH 32/33] Fix status variable --- src/public/Invoke-AzOpsGitPush.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/public/Invoke-AzOpsGitPush.ps1 b/src/public/Invoke-AzOpsGitPush.ps1 index f724d1a5..24c237c8 100644 --- a/src/public/Invoke-AzOpsGitPush.ps1 +++ b/src/public/Invoke-AzOpsGitPush.ps1 @@ -188,7 +188,7 @@ function Invoke-AzOpsGitPush { $skip = $true } - if ($skipRun -eq $true) { + if ($skip -eq $true) { $changeSet = @() } else { From 64817f4deb06a89492babb2cd8e5bc0dc712da51 Mon Sep 17 00:00:00 2001 From: Lyon Till Date: Thu, 6 Aug 2020 18:55:28 +0100 Subject: [PATCH 33/33] Revert test workflow --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 44c3281e..d886332b 100644 --- a/action.yml +++ b/action.yml @@ -7,7 +7,7 @@ inputs: required: true runs: using: "docker" - image: "docker://ljtill/azops:latest" + image: "docker://mscet/azops:main" branding: icon: "sunrise" color: "green"