How to get commit date and time on GitLab CI

北城余情 提交于 2020-12-29 06:55:49

问题


Actually I have a need to get a timestamp of my commit in GitLab CI. Alredy have tested the official documentation of GitLab CI Variables here: https://docs.gitlab.com/ee/ci/variables

But there are only commit variables for

  • CI_COMMIT_SHA The commit revision for which project is built
  • CI_COMMIT_TAG The commit tag name. Present only when building tags.

Is there a way to achieve this? Need to add these values to variables. My prefered way would be to add it to the variables section.

job:
  variables:
    COMMIT_TIME: $(git_timestamp)
  script: echo $COMMIT_TIME

I'm open to any helpful suggestions.


回答1:


Yes you can get the commit time, like this:

job:
  script: 
    - export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)
    - echo $COMMIT_TIME

If you want to have your COMMIT_TIME variable in every job use the before_script option:

before_script:
  - export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)   

job:
  script: 
    - echo $COMMIT_TIME

The %ct format gives you unixtimestamp if you want something else, have a look at the PRETTY_FORMATS in this reference on git show.




回答2:


From GitLab 13.4 you can use a new predefined variable for this: CI_COMMIT_TIMESTAMP



来源:https://stackoverflow.com/questions/49173988/how-to-get-commit-date-and-time-on-gitlab-ci

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!