![](/img/trans.png)
[英]Pass file content as build argument in azure devops pipeline docker build task
[英]Azure Build Pipeline Not Mapping Files to Docker Volume
我正在创建我们的 Azure DevOps 持续部署管道。
其中一个步骤是通过源代码控制中的脚本将数据库迁移应用到我们的环境中。 我正在利用 Docker 来避免需要在代理上安装迁移工具(Liquibase):
- stage: "ReleaseDev"
jobs:
- deployment: "Database Migration (Development)"
pool:
name: "Some Servers"
environment: "Development - Some Environment"
strategy:
runOnce:
deploy:
steps:
- bash: |
docker run --rm -v "$(Build.SourcesDirectory)/db/Internal:/liquibase/changelog" liquibase/liquibase --url="jdbc:sqlserver://xxx.company.com;Database=SomeTestDatabase;" --changeLogFile=/liquibase/changelog/liquibaseChangeLog.json --username=dbo_liquibase --password=$DEV_LIQUIBASE_PASSWORD update
env:
DEV_LIQUIBASE_PASSWORD: $(dev-liquibase-password)
但是,它似乎没有从容器中的映射卷中找到liquibaseChangeLog.json
文件:
========================== Starting Command Output ===========================
##[debug]which '/bin/bash'
##[debug]found: '/bin/bash'
##[debug]/bin/bash arg: --noprofile
##[debug]/bin/bash arg: --norc
##[debug]/bin/bash arg: /home/azure/azure1/agent/_work/_temp/b865f905-04d6-4f31-8c9b-74a312d47670.sh
##[debug]exec tool: /bin/bash
##[debug]arguments:
##[debug] --noprofile
##[debug] --norc
##[debug] /home/azure/azure1/agent/_work/_temp/b865f905-04d6-4f31-8c9b-74a312d47670.sh
/bin/bash --noprofile --norc /home/azure/azure1/agent/_work/_temp/b865f905-04d6-4f31-8c9b-74a312d47670.sh
Liquibase Community 3.8.9 by Datical
Unexpected error running Liquibase: /liquibase/changelog/liquibaseChangeLog.json does not exist
For more information, please use the --logLevel flag
##[debug]Exit code 255 received from tool '/bin/bash'
##[debug]STDIO streams have closed for tool '/bin/bash'
##[error]Bash exited with code '255'.
##[debug]Processed: ##vso[task.issue type=error;]Bash exited with code '255'.
##[debug]task result: Failed
##[debug]Processed: ##vso[task.complete result=Failed;done=true;]
Finishing: Bash
我在我们的 CI 管道中为分支做了非常相似的事情,但是脚本在 Docker-Compose 任务中执行,而不是独立的 bash 脚本。 所以我很困惑在这种情况下有什么不同。
为可怜的 windows 开发人员寻找一些建议:)
编辑:在下面 Leo 的建议之后,它使我能够提出这个作为最终的工作解决方案。 他的意见是原则,这是实践。
stages:
- stage: Build
jobs:
- job: "BuildJob"
variables:
solution: "**/*.sln"
buildPlatform: "any cpu"
buildConfiguration: "Release"
pool:
name: "xxx Build Servers"
steps:
- task: PublishPipelineArtifact@1
displayName: "Publish Pipeline Artifact - DB Migrations"
inputs:
targetPath: "db"
artifact: "db_migrations"
- stage: "ReleaseDev"
jobs:
- deployment: "Development_DbMigration"
pool:
name: "xxx Docker Hosts"
environment: "Development - Web Farm"
strategy:
runOnce:
deploy:
steps:
- task: DownloadPipelineArtifact@2
displayName: "Download Pipeline Artifact - DB Migrations"
inputs:
artifactName: 'db_migrations'
targetPath: '$(build.artifactstagingdirectory)/db'
- bash: |
docker run --rm -v "$(build.artifactstagingdirectory)/db/Internal:/liquibase/changelog" liquibase/liquibase --url="jdbc:sqlserver://dev.xxx.com;Database=SomeDatabase;" --changeLogFile=/liquibase/changelog/liquibaseChangeLog.json --username=dbo_SomeDatabase --password=$DEV_LIQUIBASE_PASSWORD update
env:
DEV_LIQUIBASE_PASSWORD: $(dev-liquibase-password)
Azure 构建管道未将文件映射到 Docker 卷
您的评论很重要,根据您的评论,您非常接近您的答案。
如果您只在发布管道中添加 -stage: "ReleaseDev",您将遇到该问题。
为了同时支持 YAML 中的发布管道 (CD),MS 提供了统一的 YAML 体验,因此您可以将每个管道配置为同时执行 CI、CD 或 CI 和 CD。
此外,MS 还为构建/部署提供了不同的内置任务,例如build
阶段的Checkout
,部署阶段的Download Artifact
。
因此,如果我们只在管道中添加ReleaseDev
而没有build
阶段,它将缺少内置任务Checkout
。 那目录$(Build.SourcesDirectory
为空的原因:
要解决这个问题,我们只需要一个简单任务的阶段构建:
stages:
- stage: Build
jobs:
- job: Build
displayName: Build
pool:
name: MyPrivateAgent
steps:
- script: |
echo $(Build.SourcesDirectory)
- stage: "ReleaseDev"
jobs:
- deployment: "Database Migration (Development)"
pool:
name: "Some Servers"
现在,我们可以从 repo 中获取源代码:
注意:如果您有多个并行代理,您可能还需要注意构建和部署是否在同一个代理上运行,如果不是,我们需要手动上传和下载它们,查看此文档了解更多详细信息。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.