CD script for APIM deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script should be used into the build pipeline to update API changes | |
# to Azure API management Service. | |
# The steps that are scripted below: | |
# | |
# 1. Updates the Internal Git Repo with the published API documents in Azure | |
# 2. Retrieves the Git Credentails of internal Repo | |
# 3. Clones the repo in to a temporary folder | |
# 4. Merges changes from VSTS repo to the temporary cloned repository | |
# 5. Commit dirty changes | |
# 6. Push changes to Azure Internal Repo (master branch) | |
# 7. Publishes the API updates from internal Repo to Azure API management service. | |
# | |
# Example Usage: | |
# | |
# Parameters: | |
# - SubscriptionId: The subscription ID where the API management Service is provisioned | |
# - ResourceGroup: Resource group name | |
# - ServiceName: API management Service name | |
# - SourceDirectory: Directory where the API documents, policies etc. are located | |
# - TempDirectory: A temporary directory where the data will be downloaded | |
# - UserName: $ENV:RELEASE_DEPLOYMENT_REQUESTEDFOREMAIL - The user name that will be used to commit to Git. You can use Relase triggerd here | |
# - UserEmailAddress: $ENV:RELEASE_DEPLOYMENT_REQUESTEDFOR The email address of the user | |
# - CommitMessage: $ENV:RELEASE_RELEASEWEBURL The commit message | |
# | |
# Get-AzureRMApiManagementGitRepo ` | |
# -SubscriptionId "-- Subscription ID -- " ` | |
# -ResourceGroup " -- Resource group name -- " ` | |
# -ServiceName " --- APIM name --- " ` | |
# -SourceDirectory "..\src\api-management" ` | |
# -TempDirectory 'C:\Temp\apim' ` | |
# -UserName "Moim Hossain" ` | |
# -CommitMessage "Commited from CI" | |
# -UserEmailAddress "moim.hossain" | |
Function ExecuteGitCommand { | |
param | |
( | |
[System.Object[]]$gitCommandArguments | |
) | |
$gitExePath = "C:\Program Files\git\bin\git.exe" | |
& $gitExePath $gitCommandArguments | |
} | |
Function Copy-DirectoryContents { | |
param | |
( | |
[System.String]$SrcPath, | |
[System.String]$destPath | |
) | |
ROBOCOPY $SrcPath $destPath /MIR | |
} | |
Function Get-AzureRMApiManagementGitRepo { | |
param | |
( | |
[System.String] | |
$SubscriptionId, | |
[System.String] | |
$ResourceGroup, | |
[System.String] | |
$ServiceName, | |
[System.String] | |
$UserName, | |
[System.String] | |
$UserEmailAddress, | |
[System.String] | |
$CommitMessage, | |
[System.String] | |
$SourceDirectory, | |
[System.String] | |
$TempDirectory | |
) | |
Write-Output "Initializing context..." | |
$context = New-AzureRmApiManagementContext ` | |
-ResourceGroupName $ResourceGroup ` | |
-ServiceName $ServiceName | |
Write-Output "Initializing context...Completed" | |
Write-Output "Syncing Git Repo with current API management state..." | |
Save-AzureRmApiManagementTenantGitConfiguration ` | |
-Context $context ` | |
-Branch 'master' ` | |
-PassThru -Force | |
Write-Output "Syncing Git Repo with current API management state...Completed" | |
$expiry = (Get-Date) + '1:00:00' | |
$parameters = @{ | |
"keyType" = "primary" | |
"expiry" = ('{0:yyyy-MM-ddTHH:mm:ss.000Z}' -f $expiry) | |
} | |
$resourceId = '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.ApiManagement/service/{2}/users/git' -f $SubscriptionId, $ResourceGroup, $ServiceName | |
if ((Test-Path -Path $TempDirectory )) { | |
Remove-Item $TempDirectory -Force -Recurse -ErrorAction "Stop" | |
} | |
$gitRemoteSrcPath = Join-Path -Path $TempDirectory -ChildPath 'remote-api-src' | |
Write-Output "Retrieving Git Credentials..." | |
$gitUsername = 'apim' | |
$gitPassword = (Invoke-AzureRmResourceAction ` | |
-Action 'token' ` | |
-ResourceId $resourceId ` | |
-Parameters $parameters ` | |
-ApiVersion '2016-10-10' ` | |
-Force).Value | |
$escapedGitPassword = [System.Uri]::EscapeDataString($gitPassword) | |
Write-Output "Retrieving Git Credentials...Completed" | |
$gitRepositoryUrl = 'https://{0}:{1}@{2}.scm.azure-api.net/' -f $gitUsername, $escapedGitPassword, $ServiceName | |
Write-Host "Performing Git clone... $gitRemoteSrcPath" | |
ExecuteGitCommand -gitCommandArguments @("clone", "$gitRepositoryUrl", "$gitRemoteSrcPath") | |
Write-Host "Performing Git clone... Completed" | |
# Copy changes from Source Repository (VSTS) to Locally Cloned Repository | |
$apiSources = Join-Path -Path $gitRemoteSrcPath -ChildPath 'api-management' | |
Copy-DirectoryContents ` | |
-SrcPath $SourceDirectory ` | |
-destPath $apiSources | |
Set-Location $gitRemoteSrcPath | |
ExecuteGitCommand -gitCommandArguments @("config", "user.email", $UserEmailAddress) | |
ExecuteGitCommand -gitCommandArguments @("config", "user.name", $UserName) | |
ExecuteGitCommand -gitCommandArguments @("add", "--all") | |
ExecuteGitCommand -gitCommandArguments @("commit", "-m", $CommitMessage) | |
ExecuteGitCommand -gitCommandArguments @("push") | |
Publish-AzureRmApiManagementTenantGitConfiguration ` | |
-Context $context ` | |
-Branch 'master' ` | |
-PassThru ` | |
-Verbose | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment