2

我正在尝试通过使用 Trivy 进行 docker scan 并将其集成到 GitLab 中,管道已通过。但是作业失败了,不确定作业失败的原因。泊坞窗图像是有效的。启用共享运行器后更新了新错误

gitlab.yml

Trivy_container_scanning:
  stage: test
  image: docker:stable-git
  variables:
    # Override the GIT_STRATEGY variable in your `.gitlab-ci.yml` file and set it to `fetch` if you want to provide a `clair-whitelist.yml`
    # file. See https://docs.gitlab.com/ee/user/application_security/container_scanning/index.html#overriding-the-container-scanning-template
    # for details
    GIT_STRATEGY: none
    IMAGE: "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
  allow_failure: true
  before_script:
    - export TRIVY_VERSION=${TRIVY_VERSION:-v0.20.0}
    - apk add --no-cache curl docker-cli
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin ${TRIVY_VERSION}
    - curl -sSL -o /tmp/trivy-gitlab.tpl https://github.com/aquasecurity/trivy/raw/${TRIVY_VERSION}/contrib/gitlab.tpl
  script:
    - trivy --exit-code 0 --cache-dir .trivycache/ --no-progress --format template --template "@/tmp/trivy-gitlab.tpl" -o gl-container-scanning-report.json $IMAGE
    #- ./trivy — exit-code 0 — severity HIGH — no-progress — auto-refresh trivy-ci-test
    #- ./trivy — exit-code 1 — severity CRITICAL — no-progress — auto-refresh trivy-ci-test

  cache:
    paths:
      - .trivycache/
  artifacts:
    reports:
      container_scanning: gl-container-scanning-report.json
  dependencies: []
  only:
    refs:
      - branches

Dockerfile

FROM composer:1.7.2
RUN git clone https://github.com/aquasecurity/trivy-ci-test.git && cd trivy-ci-test && rm Cargo.lock && rm Pipfile.lock
CMD apk add — no-cache mysql-client
ENTRYPOINT [“mysql”]

作业错误:

Running with gitlab-runner 13.2.4 (264446b2)
  on gitlab-runner-gitlab-runner-76f48bbd84-8sc2l GCJviaG2
Preparing the "kubernetes" executor
30:00
Using Kubernetes namespace: gitlab-managed-apps
Using Kubernetes executor with image docker:stable-git ...
Preparing environment
30:18
Waiting for pod gitlab-managed-apps/runner-gcjviag2-project-1020-concurrent-0pgp84 to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner-gcjviag2-project-1020-concurrent-0pgp84 to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner-gcjviag2-project-1020-concurrent-0pgp84 to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner-gcjviag2-project-1020-concurrent-0pgp84 to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner-gcjviag2-project-1020-concurrent-0pgp84 to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner-gcjviag2-project-1020-concurrent-0pgp84 to be running, status is Pending
ERROR: Job failed (system failure): prepare environment: image pull failed: Back-off pulling image "docker:stable-git". Check https://docs.gitlab.com/runner/shells/index.html#shell-profile-loading for more information

另一个错误:

Running with gitlab-runner 13.2.4 (264446b2)
  on gitlab-runner-gitlab-runner-76f48bbd84-8sc2l GCJviaG2
Preparing the "kubernetes" executor
30:00
Using Kubernetes namespace: gitlab-managed-apps
Using Kubernetes executor with image $CI_REGISTRY/devops/docker-alpine-sdk:19.03.15 ...
Preparing environment
30:03
Waiting for pod gitlab-managed-apps/runner-gcjviag2-project-1020-concurrent-0t7plc to be running, status is Pending
ERROR: Job failed (system failure): prepare environment: image pull failed: Failed to apply default image tag "/devops/docker-alpine-sdk:19.03.15": couldn't parse image reference "/devops/docker-alpine-sdk:19.03.15": invalid reference format. Check https://docs.gitlab.com/runner/shells/index.html#shell-profile-loading for more information
4

2 回答 2

1

根本原因实际上是在 gitlab cicd 变量中没有设置变量。定义注册表凭据后,一切正常。

于 2021-10-19T15:35:54.803 回答
0

接下来是gitlab-org/gitlab-runner 问题 27664

经过一些试验和错误,我和我们的团队发现问题是由于运行者未能使用服务帐户密码提取图像。
为了解决这个问题,我们使用了一个自定义配置,它image_pull_secrets.dockercfg格式中指定,以便成功拉取图像。

runner-custom-config-map 的内容:

kind: ConfigMap
apiVersion: v1
metadata:
  name: runner-custom-config-map
  namespace: runner-namespace
data:
  config.toml: |-
    [[runners]]
      [runners.kubernetes]
        image_pull_secrets = ["secret_to_docker_cfg_file_with_sa_token"]

在运行器操作规范中使用:

spec:
  concurrent: 1
  config: runner-custom-config-map
  gitlabUrl: 'https://example.gitlab.com'
  imagePullPolicy: Always
  serviceaccount: kubernetes-service-account
  token: gitlab-runner-registration-secret

secret_to_docker_cfg_file_with_sa_token

kind: Secret
apiVersion: v1
  name: secret_to_docker_cfg_file_with_sa_token
  namespace: plt-gitlab-runners
data:
  .dockercfg: >-
    __docker_cfg_file_with_pull_token__
type: kubernetes.io/dockercfg
于 2021-10-18T06:01:30.700 回答