azure - 使 Azure Keyvault secret 在整个管道中可用

标签 azure azure-devops azure-pipelines

为了从 keystore 访问我的 secret ,我运行

        - task: AzureKeyVault@2
          inputs:
            azureSubscription: $(KEYVAULT_SC_DEV)
            KeyVaultName: $(KEYVAULT_NAME_DEV)
            SecretsFilter: APICREDENTIALS
            RunAsPreJob: true 

效果很好。

但是,我有多个工作,现在面临着不得不重复这些台词太多次的麻烦。

那么,有没有办法告诉 Azure Devops 这个 secret 应该为每个作业/阶段/步骤等全局设置?

最佳答案

如果您希望通过 AzureKeyVault@2 任务使 Azure Keyvault secret 在多个作业或阶段中可用,则可以在不同阶段中使用输出。

例如,我在 KeyVault 中设置了 secret 密码

跨多个工作:

 variables:
     # map the output variable from A into this job
     password-job-b: $[ dependencies.A.outputs['ouputvariable.mypassword'] ]

跨多个阶段:

variables:
      # map the output variable from A into this job
      password-stage-two: $[ stageDependencies.One.A.outputs['ouputvariable.mypassword'] ]

整个工作:

 - task: AzureKeyVault@2
   RunAsPreJob: true ## Make the secret(s) available to the whole job

完整的 yaml 示例:

trigger:
- none

pool:
  vmImage: ubuntu-latest

stages:
- stage: One
  jobs:
  - job: A
    steps:
    - task: AzureKeyVault@2
      inputs:
       azureSubscription: ‘your subscription‘
       KeyVaultName: ‘your keyvault name’
       SecretsFilter: '*'
       RunAsPreJob: true
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: 'echo "##vso[task.setvariable variable=mypassword;isOutput=true]$(password)"'
      name : ouputvariable
  - job: B
    dependsOn : A 
    variables:
     # map the output variable from A into this job
     password-job-b: $[ dependencies.A.outputs['ouputvariable.mypassword'] ]
    steps:
    - script: echo this is password :$(password-job-b) # this step uses the mapped-in variable
- stage: Two
  variables:
      # map the output variable from A into this job
      password-stage-two: $[ stageDependencies.One.A.outputs['ouputvariable.mypassword'] ]
  jobs:
  - job: C
    steps:
    - script: echo this is password :$(password-stage-two) # this step uses the mapped-in variable

多个作业的结果: Result across multiple jobs

多个阶段的结果: Result across multiple stages

更新

issecret设置为true时,变量的值将被保存为secret。

script: 'echo "##vso[task.setvariable variable=mypassword;isOutput=true;issecret=true]$(password)"'

关于azure - 使 Azure Keyvault secret 在整个管道中可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74856953/

相关文章:

azure - Databricks Repo 与 Azure DevOps - 尝试添加存储库时出现 400 错误请求

azure-devops - 如果 yaml 中的前一阶段部署条件设置为 false,则阻止部署

azure - 通过 Azure Pipeline 将 Maven 构建部署到 Azure Artifacts 时如何修复 401?

sockets - Jmeter错误: Java.net.SocketException : Connection reset at java.net.SocketInputStream.read(未知来源)位于

Azure:Web.config 连接字符串'参数不能为 null 或为空

visual-studio - TFS 常量错误输出 TF10121 : The path '' is not found or not supported. 键入或选择其他路径

azure-devops - 创建自定义板

git - Azure Pipelines 多存储库如何获取 Git 提交 ID

azure - 我们可以在azure中的两个应用程序服务上使用负载均衡器吗

asp.net - WebApi 角色入口点 404