2

I have this drone.yml which is running fine and generating a docker image. I am building the docker image with 2 tags.

  1. SHA of commit in GitHub.
  2. latest

What I want to do is to append the date & time of image creation in the format of YYYY-MM-DD_HH-MM to the 1st tag (SHA of commit in GitHub).

---
name: api-graph-base
kind: pipeline
type: docker

steps:

  - name: push-azure-nonprod
    when:
      event:
        - push
        - pull_request
      branch:
        - master
    image: plugins/docker
    settings:
      username: abc
      password:
        from_secret: xyz
      repo: nonprodazure/graph-base-api
      build_args:
        - LOG_LEVEL=debug
      registry: nonprodregistry.azurecir.io
      custom_dns: [100.100.100.100, 99.99.99.99]
      tags:
        - '${DRONE_BUILD_FINISHED}-${DRONE_COMMIT}'
        - latest
      dockerfile: Dockerfile

Image tags in drone build logs:

  • /usr/local/bin/docker tag c692df4346acada797d9541135f37124b15d1183 nonprodazure/graph-base-api:1600986079-c692df4346acada797d9541135f37124b15d1183

You can see in logs that the UNIX timestamp(1600986079) appended in the docker image tag name. How can I change the value of ${DRONE_BUILD_FINISHED} - Unix timestamp to DateTime human-readable string format (YYYY-MM-DD_HH-MM)?

Pradeep Singh
  • 1,094
  • 1
  • 9
  • 24

1 Answers1

2

This is what I needed to do to get date/time in a human-readable format and not UNIX format.

---
name: api-graph-base
kind: pipeline
type: docker

steps:
  - name: send-tags-to-tags-file
    image: node
    when:
      event:
        - push
        - pull_request
    commands:
      - echo -n "$(date +'%Y-%m-%d_%H-%M')_${DRONE_COMMIT}, latest" > .tags

  - name: push-azure-nonprod
    when:
      event:
        - push
      branch:
        - master
    image: plugins/docker
    settings:
      username: abc
      password:
        from_secret: xyz
      repo: nonprodazure/graph-base-api
      build_args:
        - LOG_LEVEL=debug
      registry: nonprodregistry.azurecir.io
      custom_dns: [100.100.100.100, 99.99.99.99]
      dockerfile: Dockerfile

Below 2 tags created for the docker image. The second tag name contains the required date-time format now.

enter image description here

Solution:

  • Remove tags in the existing step.
  • Added a new step to generate tags in a .tags file.

If you need to share data between sibling processes (pipeline steps) you need to share this information by writing to and reading from disk. The docker plugin will automatically read tags from a .tags file. One cannot create an environment variable in one process and read that environment variable in a sibling process.

References:

  1. using custom generated tags for docker images.
  2. plugins/docker configuration multiple tags using .tags file
Pradeep Singh
  • 1,094
  • 1
  • 9
  • 24