Docker标签在Bitbucket管道的步骤之间丢失

时间:2018-11-29 16:06:50

标签: docker gradle bitbucket bitbucket-pipelines

我正在使用Bitbucket pipelines与Gradle构建Docker映像。这是我的构建:

definitions:
  steps:
    - step: &build-docker
        name: Build Docker images
        image:
          name: openjdk:8
        services:
          - docker
        script:
          - ./gradlew dockerBuildImage
          - docker image ls
        caches:
          - gradle-wrapper
          - gradle
          - docker
    - step: &publish-docker
        name: Publish Docker images
        image:
          name: docker
        services:
          - docker
        script:
          - docker image ls
        caches:
          - docker
pipelines:
  default:
    - step: *build-docker
    - step: *publish-docker

我的build.gradle.kts配置为使用UTC时间戳标记图像:

configure<DockerExtension> {
    configure(this.getProperty("javaApplication"), closureOf<DockerJavaApplication> {
        baseImage = "openjdk:8-jre-alpine"
        tag = "${name}:${Instant.now().epochSecond}"
    })
}

当我在本地运行dockerBuildImage任务时,我可以看到自己标记的图像:

$docker image ls
REPOSITORY     TAG          IMAGE ID       CREATED       SIZE
…
forklift-1     1540454741   93fd78260bd1   5 weeks ago   105MB
forklift-2     1540454741   3c8e4e191fd3   5 weeks ago   105MB
forklift-3     1540454741   1e80caffd59e   5 weeks ago   105MB
forklift-4     1540454741   0e3d9c513144   5 weeks ago   105MB
…

“ build-docker”步骤的输出如下:

REPOSITORY                  TAG            IMAGE ID       CREATED          SIZE
forklift-1                  1543511971     13146b26fe19   1 second ago     105MB
forklift-2                  1543511971     7581987997aa   3 seconds ago    105MB
forklift-3                  1543511971     a6ef74a8530e   6 seconds ago    105MB
forklift-4                  1543511970     a7087154d731   10 seconds ago   105MB
<none>                      <none>         cfc622dd7b3c   3 hours ago      105MB
<none>                      <none>         f17e20778baf   3 hours ago      105MB
<none>                      <none>         75cc06f4b5ee   3 hours ago      105MB
<none>                      <none>         1762b4f89680   3 hours ago      105MB
openjdk                     8-jre-alpine   2e01f547f003   5 weeks ago      83MB

但是第二步的输出没有任何标签,尽管图像的大小大致相等:

REPOSITORY   TAG      IMAGE ID       CREATED       SIZE
<none>       <none>   cfc622dd7b3c   3 hours ago   105MB
<none>       <none>   f17e20778baf   3 hours ago   105MB
<none>       <none>   75cc06f4b5ee   3 hours ago   105MB
<none>       <none>   1762b4f89680   3 hours ago   105MB

丢失的标签在哪里

请注意,第二步(docker image ls)中的某些图像ID似乎与第一步中打印的图像ID相同。

P.S。我知道,如果我需要标签(例如发布),则只需一步即可完成构建和发布。

1 个答案:

答案 0 :(得分:0)

虽然我无法找到根本原因,但我基于Docker的saveload命令以及Bitbucket Pipelines' artifacts进行了简单的解决。

首先,我对标记方案进行了一些更改:

configure<DockerExtension> {
    configure(this.getProperty("javaApplication"), closureOf<DockerJavaApplication> {
        baseImage = "openjdk:8-jre-alpine"
        tag = "${name}:${System.getenv("DOCKER_TAG")}"
    })
}

因此,我依赖于可以在外部设置的环境变量DOCKER_TAG而不是UTC时间戳。

然后,定义“ build-docker”步骤,如下所示:

- step: &build-docker
    name: Build Docker images
    image:
      name: openjdk:8
    services:
      - docker
    script:
      - export DOCKER_TAG=${BITBUCKET_BUILD_NUMBER}
      - ./gradlew dockerBuildImage
      - docker save
        --output images.tar
        forklift-1:${DOCKER_TAG}
        forklift-2:${DOCKER_TAG}
        forklift-3:${DOCKER_TAG}
        forklift-3:${DOCKER_TAG}
    artifacts:
      - images.tar
    caches:
      - gradle-wrapper
      - gradle

我可以将内部版本号用作标签,但是可以提供任何值。

最后,推动图像的步骤是:

- step: &publish-docker
    name: Publish Docker images
    image:
      name: docker
    services:
      - docker
    script:
      - docker load --input images.tar
      - docker image ls
      - docker push …

这有效,因为docker save

  

为标准输出流生成一个受污染的存储库。

包含提供的每个参数的所有父层,和所有标记+版本或指定的repo:tag。