我正在为 Node 应用程序设置 Azure 管道,并使用 Jest 来测试 API 和集成。源代码位于 Azure DevOps 上,代码部署在 Azure 门户中。
当我运行测试时,它在管道中失败,因为 .env 从未在远程存储库中检查。环境变量通过配置在运行时位于 Azure 门户中,因此管道无法真正访问它。
有哪些方法可以访问或创建环境变量的新位置以便我的测试运行虚拟机?
我当前的解决方案(我不知道它是否正确)是创建一个变量组并重新定义所有环境变量,以便管道可以读取此处描述的变量:https://damienaicheh.github.io/azure/devops/2019/09/04/how-to-use-variables-inside-your-azure-devops-builds-en.html https://damienaicheh.github.io/azure/devops/2019/09/04/how-to-use-variables-inside-your-azure-devops-builds-en.html
我的问题是:
- 它是否正确?这里存储的任何变量都与构建无关,它们也不是运行命令的输入,而是源代码中需要我的所有环境变量,以便我可以在虚拟机中进行测试(例如:base_url、apiKeys 等)。
- 如果这是正确的,我怎样才能避免重写和重新分配管道中的所有值?我可以获取整个变量组并且源代码可以解释吗?我想避免这样
- env
- API_KEY: $(apiKey)
- MAPS_KEY: $(mapsKey)
- CLIENT_KEY: $(clientKey)
- CLIENT_SECRET: $(clientSecret)
-
-
- and so on...
// looking for something like this
-env: myVariableGroup
- 任何导致帖子、文章更好的解决方案吗?我正在考虑使用密钥保管库,但我认为它本质上与我必须逐一导入相同。
管道变量会自动映射到环境变量,因此不需要额外的工作。只有一个例外——秘密。您必须明确映射它们
steps:
- script: echo $MYSECRET
env:
MYSECRET: $(Foo)
因此,声明、组或模板中的所有值都映射到环境变量
变量.yaml
variables:
variableFromTemplate: 'valueFromTemplate'
构建.yaml
variables:
- group: PROD
- name: variableFromDeclaration
value: 'valueFromDeclaration'
- template: vars.yaml
pool:
vmImage: 'ubuntu-latest'
steps:
- script: env | sort
- script: |
echo $VARIABLEFROMDECLARATION
echo $VARIABLEFROMGROUP
echo $VARIABLEFROMTEMPLATE
- pwsh: |
$url = "https://dev.azure.com/thecodemanual/$(System.TeamProject)/_apis/build/builds/$(Build.BuildId)?api-version=5.1"
$build = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:MY_SECRET"}
Write-Host "Pipeline = $($build | ConvertTo-Json -Depth 100)"
$status = $build.status
Write-Host $status
name: initial
env:
MY_SECRET: $(System.AccessToken)
因此,对于每一步,您都需要定义秘密env
部分。作为解决方法,你我尝试使用集装箱工作 https://learn.microsoft.com/en-us/azure/devops/pipelines/process/container-phases?view=azure-devops并定义环境映射.
resources:
containers:
- container: string # identifier (A-Z, a-z, 0-9, and underscore)
image: string # container image name
options: string # arguments to pass to container at startup
endpoint: string # reference to a service connection for the private registry
env: { string: string } # list of environment variables to add
ports: [ string ] # ports to expose on the container
volumes: [ string ] # volumes to mount on the container
mapDockerSocket: bool # whether to map in the Docker daemon socket; defaults to true
mountReadOnly: # volumes to mount read-only - all default to false
externals: boolean # components required to talk to the agent
tasks: boolean # tasks required by the job
tools: boolean # installable tools like Python and Ruby
work: boolean # the work directory
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)