將 Docker Scout 與 Microsoft Azure DevOps Pipelines 整合
以下示例在連線到 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
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 報告。它只顯示關鍵或高嚴重性漏洞。