如何在构建映像时使用Docker凭据检查Azure Pipeline

qlzsbp2j  于 2023-01-16  发布在  Docker
关注(0)|答案(1)|浏览(107)

**要求:**我必须使用Docker Hub凭据构建Docker映像并将其推送到Harbor存储库

下面的代码段在Azure管道中工作,但不确定构建映像时是否使用了Docker凭据
是否有方法在Azure Pipeline(Devops)中检查它在构建映像时是否正在使用Docker Hub凭据?

- task: Docker@2
          displayName: docker login
          inputs:
            containerRegistry: 'docker-connection'
            command: 'login'
        - task: Docker@2
          displayName: build image on docker hub and tag it with harbor
          inputs:
            containerRegistry: 'harbor-connection'
            command: 'build'
            repository: 'repository'
            Dockerfile: '**/Dockerfile'
            tags: '12345'
        - task: Docker@2
          displayName: docker push
          inputs:
            containerRegistry: 'harbor-connection'
            command: 'push'
            repository: 'repository'
            tags: '12345'
        - task: Docker@2
          displayName: docker logout
          inputs:
            containerRegistry: 'docker-connection'
            command: 'logout'
bf1o4zei

bf1o4zei1#

我认为您应该在推送任务之前添加一个港口登录

- task: Bash@3
  displayName: 'harbor login'
  inputs:
    targetType: 'inline'
    script: |
      # Write your commands here
      ls $(Build.SourcesDirectory)/<project-name> 
      sudo mkdir /etc/docker/certs.d
      cd /etc/docker/certs.d
      pwd
      sudo mkdir /etc/docker/certs.d/<your-cluster-url> 
      cd /etc/docker/certs.d/<your-cluster-url>      
      pwd
      sudo cp $(Build.SourcesDirectory)/<project-name>/ca.crt /etc/docker/certs.d/<your-cluster-url>
      ls /etc/docker/certs.d/<your-cluster-url>
      # Docker login to your harbor using particular user
      /usr/bin/docker login <your-cluster-url> -u <username> -p <password>

- task: Docker@2
  displayName: 'push image to harbor'
  inputs:
    containerRegistry: 'harbor'
    command: 'push'
    repository: 'bot/<project-name>'
    tags: '$(Build.BuildNumber)'

这是从Chapatazars GitHub Repo中提取的
要了解有关管道脚本的更多知识:
Azure pipeline scripts
Crossplatform yml scripting
另一个正确的方法(我认为也是最简单的)是使用Azure发布管道(不需要yml文件)发布docker映像。在那里你可以使用构建过程的结果工件(这个构建来自你的yml管道)将映像推送到你需要的注册表。你可以看看官方文档:
Publish docker image from Azure pipelines

相关问题