如何在 Azure Pipelines yml 模板中使用变量组?

发布于 2025-01-20 10:40:58 字数 779 浏览 0 评论 0 原文

因此,我正在处理一堆管道,并且使用YML模板设置了所有内容。但是,我很难在模板步骤内部扩展受保护的变量。我已经尝试通过正常方式传递受保护变量,但它们似乎没有扩展。然后,我尝试使用一个变量组,据说我可以直接在模板内引用。我说,因为微软在他们的网站上说了 https://learn.microsoft.com/en-us/azure/devops/pipelines/library/library/variable/variable-groups?view = azure-devops& tabs = tabs = yaml

:还可以在模板变量中引用一个变量组。

variables:
- group: my-variable-group

但是,每当我在模板代码中包括变量部分时,Azure DevOps在运行管道之前在解析YML时立即抱怨它。它吐出以下消息:

/ymls /my-template@my-repo(行:1,col:1):意外的值'变量'

我不坚持使用变量组,我只想拥有我的受保护变量在我的YML模板中扩展。有人知道该怎么做吗???

任何帮助都非常感谢!

So I'm working on a bunch of pipelines and I've set everything up using yml templates. However I struggle with getting protected variable expanded inside of my template steps. I've tried passing in the protected variables by normal means, but they seem to not get expanded. Then I tried using a variable group, which I supposedly can directly reference inside of templates. I say supposedly, because Microsoft says so on their website https://learn.microsoft.com/en-us/azure/devops/pipelines/library/variable-groups?view=azure-devops&tabs=yaml:

"You can also reference a variable group in a template. In the template variables.yml, the group my-variable-group is referenced. The variable group includes a variable named myhello."

variables:
- group: my-variable-group

However, whenever I include a variables section into my template code, Azure DevOps immediately complains about it when parsing the yml, before running the pipeline. It spits out the following message:

/ymls/my-template@my-repo (Line: 1, Col: 1): Unexpected value 'variables'

I don't insist on using variable groups, I just want to have my protected variables expanded in my yml template. Does anybody know how to do that???

Any help greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怼怹恏 2025-01-27 10:40:58

您应该在主管道上而不是在模板中定义变量组。然后您可以调用模板并使用您定义的变量。

例如,假设您有一个调用 template.ymlma​​in.yml

您应该在 main.yml 上定义以下变量组

variables:
- group: my-variable-group

并调用template.yml 上的变量

$(MY_VARIABLE)

https://thomasthornton.cloud/2021/09/02/referencing-variable-groups-in-azure-devops-pipeline-templates/

You should define your variable group on your main pipeline and not in the template. Then you can call your template and use the variable that you defined.

For example lets say that you have your main.yml which calls template.yml

You should define the below variable group on main.yml

variables:
- group: my-variable-group

And call the variable on your template.yml

$(MY_VARIABLE)

https://thomasthornton.cloud/2021/09/02/referencing-variable-groups-in-azure-devops-pipeline-templates/

○闲身 2025-01-27 10:40:58

终于想通了。感谢@GeralexGR。事实证明,当您在主管道 yml 中引用变量组时,您会自动在模板 yml 中访问它。但对于正常的脚本步骤,您仍然必须显式地传递它。

然后它看起来……像这样:

main-pipeline.yml:

variables:
- group: my-variable-group
...
jobs:
- job: my-job
  steps:
  - template: ymls/my-template.yml@my-repo
  # no need to pass in the variable group s parameter

my-template.yml:

steps:
- task: ShellScript@2
  inputs:
    scriptPath: 'my-script.sh'
    args: '$(my-secret-variable)'

Finally figured it out. Thanks to @GeralexGR. Turns out, when you reference a variable group in the main pipeline yml, you automatically have access to it in the template yml. But for normal script steps you still have to pass it in explicitly.

It then looks s.th. like this:

main-pipeline.yml:

variables:
- group: my-variable-group
...
jobs:
- job: my-job
  steps:
  - template: ymls/my-template.yml@my-repo
  # no need to pass in the variable group s parameter

my-template.yml:

steps:
- task: ShellScript@2
  inputs:
    scriptPath: 'my-script.sh'
    args: '$(my-secret-variable)'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文