Search code examples
bitbucketbitbucket-pipelines

Get current date time in bitbucket pipeline


I have a simple question about bitbucket pipeline YAML script. I would like to use the current date and time as a variable in my pipeline, how to do that?

I tried:

pipelines:
  custom:
    build-docker-img:
      - variables:
          - name: MY_TAG
            default: $(date +%Y-%m-%d_%H:%M:%S)_$BITBUCKET_BUILD_NUMBER
      - step:
          name: Build my image
          script:
            - docker build -t myname/myapp:$MY_TAG .

But when running the pipeline, bitbucket runner doesn't translate (neither understand) the $(date +%Y-%m-%d_%H:%M:%S) to current date time value.


Solution

  • Inspired by @N1ngu's answer, I solved the issue by:

    pipelines:
      custom:
        build-docker-img:
          - step:
              name: Build my image
              script:
                - MY_TAG=${MY_TAG:-$(date +%Y-%m-%d)_$BITBUCKET_BUILD_NUMBER}
                - docker build -t myname/myapp:$MY_TAG .
    

    I can't use time due to the colon : makes it not a valid image tag.