2 azure-devops azure-pipelines-yaml
我正在尝试捕获 Azure Pipeline $(System.AccessToken) 并在此管道中对其进行编码,以允许 NPM 安装对 Azure DevOps 中的我的源进行身份验证。
trigger:
- main
resources:
- repo: self
variables:
- group: Docker-Environment
- name: tag
value: '$(Build.BuildId)'
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
pool: Docker
workspace:
clean: all
steps:
- bash: |
NPM_PASS_SYS= printf "%s"":$(System.AccessToken)" | base64 # This looks Ok
env:
NPM_SYSTEM_TOKEN: "$NPM_PASS_SYS"
displayName: ECHO NPM_PASS_SYS VAR
- bash: |
echo $NPM_SYSTEM_TOKEN #EMPTY with $NPM_SYSTEM_TOKEN and $(NPM_SYSTEM_TOKEN)
displayName: "Echo the variable"
- task: Docker@2
displayName: Building the image
inputs:
command: build
repository: $(IMAGE_NAME)
dockerfile: '**/Dockerfile'
containerRegistry: 'DockerRegistryServiceConnection'
arguments: '--no-cache --build-arg NPM_USER=$(NPM_USER) --build-arg NPM_PASS=$(NPM_PASS_SYS)'
tags: |
$(tag)
- task: Docker@2
displayName: Push to Azure Registry
inputs:
command: push
repository: $(IMAGE_NAME)
containerRegistry: 'DockerRegistryServiceConnection'
tags: |
$(tag)
name: 'push'
- task: Bash@3
inputs:
targetType: 'inline'
script: |
docker image rm $(docker image ls -aq) || true
docker builder prune -f --filter "until=24h"
displayName: 'Clean old images'
- task: Bash@3
inputs:
targetType: 'inline'
script: |
rm -rf $(Build.SourcesDirectory)
displayName: 'Clean sources directory'
Run Code Online (Sandbox Code Playgroud)
NPM_PASS_SYS= printf "%s"":$(System.AccessToken)" | base64实际上将输出打印到 Azure DevOps 控制台,但回显NPM_PASS_SYS始终返回空字符串。
我已经授予组织贡献者中我的项目构建服务对源的访问权限。
在 YAML 示例中,您可以使用以下格式:NPM_PASS_SYS= printf "%s"":$(System.AccessToken)" | base64 在 bash 任务中设置变量。
在 Pipeline 中,bash 任务是单独的进程。当您使用当前格式设置变量时,它仅适用于当前任务。
它不能传递到下一个任务。
另一方面,envbash任务中的字段用于将变量传递给当前任务脚本,而不是设置Pipeline的变量。
为了解决这个问题,您需要使用以下格式定义变量:echo "##vso[task.setvariable variable=myOutputVa]this is the value"
这是一个 YAML 示例:您可以使用脚本:NPM_PASS_SYS=$(printf "%s"":$(System.AccessToken)" | base64 -w 0)将令牌转换为 base64 格式。
stages:
- stage: Build
displayName: Build and push stage
jobs:
- job: Build
displayName: Build
steps:
- bash: |
NPM_PASS_SYS=$(printf "%s"":$(System.AccessToken)" | base64 -w 0)
echo $NPM_PASS_SYS
echo "##vso[task.setvariable variable=NPM_SYSTEM_TOKEN]$NPM_PASS_SYS"
displayName: ECHO NPM_PASS_SYS VAR
- bash: |
echo $(NPM_SYSTEM_TOKEN)
displayName: "Echo the variable"
Run Code Online (Sandbox Code Playgroud)
有关更详细的信息,您可以参考此文档:定义变量
结果:
| 归档时间: |
|
| 查看次数: |
2077 次 |
| 最近记录: |