How can a script access Service Connections? (Azure Devops Pipelines)

前端 未结 5 726
南旧
南旧 2021-01-01 15:31

According to https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints there\'s a rich array of Service Connection types. I can easily manage a set o

5条回答
  •  庸人自扰
    2021-01-01 15:58

    I am using the same service connection in my scripts/tools as for the ARM deployments.

    In order to export the variables, I created the following template.

    parameters:
    - name: azureSubscription
      type: string
    - name: exportAsOutput
      type: boolean
      default: false
      
    steps:  
    - task: AzureCLI@2
      name: exported_azure_credentials
      displayName: 'Export Azure Credentials'
      inputs:
        azureSubscription: '${{ parameters.azureSubscription }}'
        scriptType: pscore
        scriptLocation: inlineScript
        addSpnToEnvironment: true
        ${{ if eq(parameters.exportAsOutput, true) }}:
          inlineScript: |
            Write-Host "##vso[task.setvariable variable=AZURE_TENANT_ID]$env:tenantId"
            Write-Host "##vso[task.setvariable variable=AZURE_TENANT_ID;isOutput=true]$env:tenantId"
            Write-Host "##vso[task.setvariable variable=AZURE_CLIENT_ID]$env:servicePrincipalId"
            Write-Host "##vso[task.setvariable variable=AZURE_CLIENT_ID;isOutput=true]$env:servicePrincipalId"
            Write-Host "##vso[task.setvariable variable=AZURE_CLIENT_SECRET]$env:servicePrincipalKey"
            Write-Host "##vso[task.setvariable variable=AZURE_CLIENT_SECRET;isOutput=true]$env:servicePrincipalKey"
        ${{ if eq(parameters.exportAsOutput, false) }}:
          inlineScript: |
            Write-Host "##vso[task.setvariable variable=AZURE_TENANT_ID]$env:tenantId"
            Write-Host "##vso[task.setvariable variable=AZURE_CLIENT_ID]$env:servicePrincipalId"
            Write-Host "##vso[task.setvariable variable=AZURE_CLIENT_SECRET]$env:servicePrincipalKey"
    

    DevOps is really clever about secrets, so they do not show up in the pipeline logs.

提交回复
热议问题