将 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 报告。它只显示 严重或高严重性漏洞。