将 Docker Scout 与 Microsoft Azure DevOps Pipelines 集成


以下示例运行在一个连接到 Azure DevOps 的代码仓库中,该仓库包含 Docker 镜像的定义和内容。当向 main 分支提交代码时,该流水线将构建该镜像,并使用 Docker Scout 生成 CVE 报告。

首先,设置其余的工作流,并配置对所有流水线步骤可用的变量。将以下内容添加到 azure-pipelines.yml 文件中:

trigger:
  - main

resources:
  - repo: self

variables:
  tag: "$(Build.BuildId)"
  image: "vonwig/nodejs-service"

这将配置工作流,以使用特定的容器镜像来运行应用程序,并使用构建ID为每次新构建的镜像打上标签。

将以下内容添加到 YAML 文件中:

stages:
  - stage: Build
    displayName: Build image
    jobs:
      - job: Build
        displayName: Build
        pool:
          vmImage: ubuntu-latest
        steps:
          - task: Docker@2
            displayName: Build an image
            inputs:
              command: build
              dockerfile: "$(Build.SourcesDirectory)/Dockerfile"
              repository: $(image)
              tags: |
                $(tag)                
          - task: CmdLine@2
            displayName: Find CVEs on image
            inputs:
              script: |
                # Install the Docker Scout CLI
                curl -sSfL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh | sh -s --
                # Login to Docker Hub required for Docker Scout CLI
                echo $(DOCKER_HUB_PAT) | docker login -u $(DOCKER_HUB_USER) --password-stdin
                # Get a CVE report for the built image and fail the pipeline when critical or high CVEs are detected
                docker scout cves $(image):$(tag) --exit-code --only-severity critical,high                

这将创建前面提到的流程。它会使用检出的 Dockerfile 构建并标记镜像,下载 Docker Scout CLI,然后针对新标签运行cves命令,以生成 CVE 报告。该报告仅显示严重性为“严重”或“高”的漏洞。