Home > database >  Change timestamp variable format or do replacement
Change timestamp variable format or do replacement

Time:03-17

I am currently working on setting up ci/cd pipeline for pushing nuget packages.

I want to use the built-in CI_COMMIT_TIMESTAMP for version suffix however its ISO 8601 format is not valid for this.

Example ISO 8601 (UTC): 2022-03-15T18:34:43Z

Will need to at least replace colon.

Is it possible to format it differently or do some sort of text replacement?

CodePudding user response:

You can't change how the variable is presented, but you can reformat the value in your job.

The unix date command can do this. For example, you can declare any valid format as the desired output format.

MY_JOB:
  variables:
    DESIRED_FORMAT: "%Y-%m-%dT%H-%M-%S"
  script:
    - nuget_format="$(date -d "$CI_COMMIT_TIMESTAMP"  "$DESIRED_FORMAT")"
    - echo "$nuget_format" 

This will have an output like:

2022-03-15T23-43-17

An alternative may be to use sed to replace the occurrences of : with -.

script:
  - nuget_time_format="$(sed "s/:/-/g" <<< $CI_COMMIT_TIMESTAMP)"
  - echo "$nuget_time_format"
  • Related