Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Created June 3, 2019 20:30
Show Gist options
  • Save JustinGrote/e2d9e9c883ee71428ae335f68d33225d to your computer and use it in GitHub Desktop.
Save JustinGrote/e2d9e9c883ee71428ae335f68d33225d to your computer and use it in GitHub Desktop.
Azure Devops Pipeline for Deploying Powershell Azure Functions via Terraform
variables:
#Specified which branch that terraform apply and azure function deploy will run
deployBranch: 'refs/heads/master'
jobs:
- job: AzureFunction
displayName: Prepare Azure Function Package
pool:
vmImage: 'vs2017-win2016'
steps:
- checkout: self
fetchDepth: 1
#There is a bug where first run has a 10 second delay. This is done just so that other task times are reported correctly.
- pwsh: "echo Initialized"
displayName: 'Initialize Powershell Core'
- pwsh: |
Install-Module Pester -Scope CurrentUser -force
Import-Module Pester
Invoke-Pester -OutputFormat NUnitXML -OutputFile $(Build.StagingDirectory)/TestResults-Pester.XML #-CodeCoverageOutputFileFormat JaCoCo -CodeCoverageOutputFile $(Build.ArtifactStagingDirectory)/CodeCoverage-Pester.XML -CodeCoverage (Get-ChildItem run.ps1 -Recurse)
displayName: Run Pester Tests
- task: PublishTestResults@2
displayName: 'Capture Pester Test Results'
inputs:
testRunner: NUnit
testResultsFiles: $(Build.StagingDirectory)/TestResults-Pester.XML
testRunTitle: Pester
# - task: PublishCodeCoverageResults@1
# inputs:
# codeCoverageTool: 'JaCoCo'
# summaryFileLocation: $(Build.ArtifactStagingDirectory)/CodeCoverage-Pester.XML
- task: CopyFiles@2
displayName: 'Copy Files to Staging'
inputs:
Contents: |
**/*
!local.settings.json
!.gitignore
!.funcignore
!profile.ps1
!requirements.psd1
!.git/**/*
!.vscode/**/*
!.azurefunctions/**/*
!**/Tests/**/*
!**/
TargetFolder: '$(Build.StagingDirectory)'
- task: PublishPipelineArtifact@0
displayName: 'Publish Artifact: AzureFunctionPackage'
inputs:
targetPath: '$(Build.StagingDirectory)'
ArtifactName: AzureFunctionPackage
- job: Terraform
displayName: Run Terraform
dependsOn: AzureFunction
pool:
vmImage: 'vs2017-win2016'
steps:
- checkout: self
fetchDepth: 1
#There is a bug where first run has a 10 second delay. This is done just so that other task times are reported correctly.
- pwsh: "echo Initialized"
displayName: 'Initialize Powershell Core'
#Enabled when Debugging
- pwsh: 'dir env: | ft | out-string;get-variable | ft | out-string'
displayName: DEBUG Show all variables
enabled: "false"
- pwsh: |
[String[]]$credfile = 'credentials "app.terraform.io" {'
$credFile += " token = `"$($ENV:TFE_TEAM_TOKEN)`""
$credFile += '}'
[IO.File]::WriteAllLines("$ENV:APPDATA\terraform.rc",$credFile,[Text.UTF8Encoding]::New($false))
displayName: Fetch Terraform Enterprise Team Token
#env:
# TFE_TEAM_TOKEN: $(TFE_TEAM_TOKEN)
# - powershell: gc -raw $ENV:APPDATA\terraform.rc; ($ENV:TFE_TEAM_TOKEN).SubString(0,4)
# displayName: DEBUG-Show Backend TFE Token
- pwsh: .\Tools\Install-Terraform.ps1
displayName: Install Terraform
timeoutInMinutes: 1
- pwsh: .\terraform.exe init --input=false
displayName: Run Terraform Init
timeoutInMinutes: 1
- pwsh: .\terraform.exe plan --input=false --out=$(Build.StagingDirectory)/adslusprtgfunctions.tfplan
displayName: Run Terraform Plan
condition: and(succeeded(), ne(variables['Build.SourceBranch'], variables['DeployBranch']))
#TODO: Show Plan as a PR Status
- pwsh: .\terraform.exe apply --input=false --auto-approve
displayName: Run Terraform Apply
condition: and(succeeded(), eq(variables['Build.SourceBranch'], variables['DeployBranch']))
- pwsh: |
$matrix = @{}
$tfoutputjson = & .\terraform.exe output -json | ConvertFrom-Json
$tfoutputjson.azurerm_function_app_name.value.foreach{
$matrix.$PSItem = @{}
$matrix.$PSItem.FunctionAppName = $PSItem
}
$matrix = $matrix | ConvertTo-Json -Compress
$matrix
echo "##vso[task.setVariable variable=matrix;isOutput=true]$matrix"
displayName: Generate Azure Function App Deploy Matrix
name: GenerateAzFuncAppDeployMatrix
- job: Deploy
dependsOn: Terraform
condition: and(succeeded(), eq(variables['Build.SourceBranch'], variables['DeployBranch']))
pool:
vmImage: 'vs2017-win2016'
strategy:
matrix: $[ dependencies.Terraform.outputs['GenerateAzFuncAppDeployMatrix.matrix']]
steps:
- checkout: none
- task: DownloadPipelineArtifact@1
inputs:
artifactName: AzureFunctionPackage
targetPath: $(Build.StagingDirectory)
- task: AzureFunctionApp@1
displayName: Deploy to Azure Function
condition: and(succeeded(), eq(variables['Build.SourceBranch'], variables['DeployBranch']))
inputs:
azureSubscription: ADSLUSPRTGFunctions-Development
appType: functionApp
appName: $(FunctionAppName)
package: $(Build.StagingDirectory)
deploymentMethod: zipDeploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment