docker - 使用 Azure Devops Pipeline 构建容器时如何将变量传递给 docker
问题描述
我正在尝试使用具有 DDBB 连接的 PHP 项目构建一个容器,并且希望通过环境变量设置 DDBB。
我已经用管道变量定义了变量。
我还在变量部分中设置了变量
variables:
imageName: 'project'
repositoryNameDes: 'portalweb-des/project'
repositoryNamePro: 'portalweb-pro/project'
connectionECRpredes: 'amazon container registry w-predes'
connectionECRpro: 'amazon container registry w-pro'
tName: $(Build.SourceBranchName)_$(Build.SourceVersion)
ecrRepositoryNameBaseUrl: '513537361685.dkr.ecr.eu-west-1.amazonaws.com'
dirNameS3: 'project'
bucketNameDes: 'cluster-drupal-des-deploy-s3'
deployOnECS: $[or(startsWith(variables['Build.SourceBranch'], 'refs/heads/tags/'), startsWith(variables['Build.SourceBranch'], 'refs/heads/develop'), startsWith(variables['Build.SourceBranch'], 'refs/heads/feature/generardocker'))]
ddbb_name: $(DDBB_NAME)
但是我在构建时看不到如何加载变量,我不确定是否有选项设置环境变量,如果我必须编写自定义构建以使用变量运行或如何做了。
steps:
- task: Docker@2
displayName: Build an image
inputs:
repository: $(imageName)
command: build
Dockerfile: Dockerfile
tags: $(tName)
- task: ECRPushImage@1
inputs:
ENV: '$(dev)'
awsCredentials: '$(connectionECRpredes)'
regionName: 'eu-west-1'
imageSource: 'imagename'
sourceImageName: '$(imageName)'
sourceImageTag: '$(tName)'
repositoryName: '$(repositoryNameDes)'
pushTag: $(tName)
解决方案
在最常见的情况下,您设置变量并在 YAML 文件中使用它们。这使您可以跟踪版本控制系统中变量的更改。您还可以在管道设置 UI 中定义变量(请参阅经典选项卡)并在 YAML 中引用它们。
这是一个示例,展示了如何设置两个变量configuration
和platform
,并在稍后的步骤中使用它们。要在 YAML 语句中使用变量,请将其包装在$()
.
# Set variables once
variables:
configuration: debug
platform: x64
steps:
# Use them once
- task: MSBuild@1
inputs:
solution: solution1.sln
configuration: $(configuration) # Use the variable
platform: $(platform)
# Use them again
- task: MSBuild@1
inputs:
solution: solution2.sln
configuration: $(configuration) # Use the variable
platform: $(platform)
您可以查看以下链接以获取更多详细信息:
推荐阅读
- visual-studio-code - 单击“解决保存冲突”上的错误按钮后,我可以在 VSCode 上恢复文件的不同版本吗?
- javascript - 使用 StrictTemplate 分配模板引用变量?
- git - 如何管理两个链接的 GitHub 存储库
- database - springboot中如何互换配置数据库
- layer - 删除和编辑 acrobat 图层
- node.js - 如何通过 ARN 获取 AWS 资源的标签?
- runtime-error - 为什么我的简单代码在 spyder 上运行良好,但在 SPOJ 的在线 IDE 上运行良好?
- c++ - 如何分配作为单例类的数据成员的指针?
- ios - 通过按下注册按钮,即使所有文本字段都为空,segue 也会执行
- c - 在 C 中的进程间通信中传递结构