将 Docker Scout 与 Microsoft Azure DevOps 管道集成

以下示例在 Azure DevOps 连接的存储库中运行,其中包含 Docker 映像的定义和内容。由对主分支的提交触发,管道构建映像并使用 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
                docker login -u $(DOCKER_HUB_USER) -p $(DOCKER_HUB_PAT)
                # 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 报告。它仅显示严重或高严重性漏洞。