我有一个叫devops
的Azure
我的管道有一个声明为devops
的资源
引用模板工作:
- template: templates/template.yaml@devops
在devops/templates/template.yaml
文件中,我想添加一个步骤来执行驻留在devops/scripts/myscript.ps1
中的powershell脚本
这个是可能的吗?template.yaml
文件中的语法是什么?
我试过了,但没用:
steps:
- task: PowerShell@2
inputs:
filePath: scripts/myscript.ps1@devops
发布于 2020-05-08 11:03:00
请试试这个:
#templates/template.yaml
parameters:
- name: repo # defaults for any parameters that aren't specified
default: ''
steps:
- task: PowerShell@2
inputs:
filePath: ${{ parameters.repo }}/scripts/myscript.ps1
和构建定义:
resources:
repositories:
- repository: devops
type: github
name: kmadof/devops-templates
endpoint: kmadof
steps:
- checkout: self
- checkout: devops
- template: templates/template.yaml@devops
parameters:
repo: devops-templates
如果您删除了checkout: self
,您将直接在(Agent.BuildDirectory)
中获得它们的devops
回购内容。请看这里获得更多信息。
发布于 2020-05-11 08:17:36
假设这两个repos在同一个团队项目中。然后:
您的template.yaml
在devops
回购中应该是:
steps:
- task: PowerShell@2
inputs:
filePath: scripts/myscript.ps1
您在main
回购中的构建定义应该是:
resources:
repositories:
- repository: devops
type: git
name: ProjectName/devops
steps:
- checkout: devops
- template: template.yaml@devops
由于- checkout: devops
只下载repos的内容,scripts/myscript.ps1
就足够了,我们不需要scripts/myscript.ps1@devops
。造成问题的直接原因是Azure服务不会自动将在线devops
回购的内容下载到本地代理。只要确保devops
回购的内容是下载的,事情就会好起来。
发布于 2022-11-04 13:04:32
实现这一点的另一种方法是通过使用System.AccessToken
的REST。您可以有一个可重用的步骤模板。
调用模板的示例。
- template: ../steps/internal-run-template-powershell.yml
parameters:
scriptPath: stages-file-server-deploy/checkApprovals.ps1
displayName: Check Approvers
branch: my-branch
模板本身可以调用REST,如下所示(请注意,在本例中,我使用的是PowerShell,但您可以使用最适合您的任何东西,如果您希望它是跨平台的,而不需要任何特殊代理需求,则始终可以创建一个任务扩展来使用内置的node.js)。
parameters:
- name: scriptPath
displayName: Where the script is located
type: string
- name: displayName
displayName: What you want this step to show up as
default: Run script from pipeline
type: string
- name: env
displayName: Environment variables to pass to the script
default: {}
type: object
- name: repo
displayName: Template repository
default: your_template_repo_name
type: string
- name: branch
displayName: Template repository branch to use
type: string
steps:
- task: PowerShell@2
displayName: ${{ parameters.displayName }}
inputs:
targetType: inline
script: |
$templateRepo = "${{ parameters.repo }}"
$script = "${{ parameters.scriptPath }}"
$branch = "${{ parameters.branch }}"
$authHeaders = @{
"Authorization" = "Basic " + ([convert]::ToBase64String([System.Text.Encoding]::UTF8.getBytes(":" + $env:SYSTEM_ACCESSTOKEN)))
}
$uri = "$env:SYSTEM_COLLECTIONURI/$env:SYSTEM_TEAMPROJECT/_apis/git/repositories/$templateRepo/items?path=$script&api-version=5.1"
if ($branch -ne "") {
$uri += "&versionDescriptor.version=$branch&versionDescriptor.versionType=branch"
}
"Running $uri"
invoke-webrequest -outfile .script.ps1 -headers $authHeaders -UseBasicParsing `
$uri
& .script.ps1
env:
${{ each v in parameters.env }}:
${{ v.key }}: ${{ v.value }}
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
这种方法避免了根模板必须传递资源的名称,也避免了模板本身必须依赖根模板。
请注意,系统访问令牌需要有足够的特权。特权是由用户“项目收集构建服务(帐户)”确定。
https://stackoverflow.com/questions/61676408
复制相似问题