Azure 构建管道未将文件映射到 Docker 卷

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 Build Pipeline Not Mapping Files to Docker Volume

您的评论很关键,根据您的评论,您非常接近答案。

如果您只在发布管道中添加 - stage: "ReleaseDev",就会遇到该问题。

为了也支持 YAML 中的发布管道 (CD),MS 提供了统一的 YAML 体验,因此您可以配置每个管道来执行 CI、CD 或 CI和CD在一起。

此外,MS还为build/deployment提供了不同的内置任务,如Checkout用于build阶段,Download Artifact用于部署阶段。

因此,如果我们只在没有 build 阶段的管道中添加 ReleaseDev,它将丢失内置任务 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 中获取源代码:

注意:如果你有多个agent并行,可能还需要注意build和deploy是否在同一个agent上运行,如果不是,我们需要手动上传下载, 查看 this document 了解更多详情。

希望对您有所帮助。