Commit 4063fd3b authored by Fabio Pitino's avatar Fabio Pitino

Merge branch '34281-add-ci-variable-timestamp' into 'master'

Add CI predefined environment variable CI_COMMIT_TIMESTAMP

Closes #34281

See merge request gitlab-org/gitlab!40388
parents 8e44e8a3 096626ab
...@@ -491,6 +491,12 @@ module Ci ...@@ -491,6 +491,12 @@ module Ci
end end
end end
def git_commit_timestamp
strong_memoize(:git_commit_timestamp) do
commit.try(:timestamp)
end
end
def before_sha def before_sha
super || Gitlab::Git::BLANK_SHA super || Gitlab::Git::BLANK_SHA
end end
...@@ -768,6 +774,7 @@ module Ci ...@@ -768,6 +774,7 @@ module Ci
variables.append(key: 'CI_COMMIT_TITLE', value: git_commit_full_title.to_s) variables.append(key: 'CI_COMMIT_TITLE', value: git_commit_full_title.to_s)
variables.append(key: 'CI_COMMIT_DESCRIPTION', value: git_commit_description.to_s) variables.append(key: 'CI_COMMIT_DESCRIPTION', value: git_commit_description.to_s)
variables.append(key: 'CI_COMMIT_REF_PROTECTED', value: (!!protected_ref?).to_s) variables.append(key: 'CI_COMMIT_REF_PROTECTED', value: (!!protected_ref?).to_s)
variables.append(key: 'CI_COMMIT_TIMESTAMP', value: git_commit_timestamp.to_s)
# legacy variables # legacy variables
variables.append(key: 'CI_BUILD_REF', value: sha) variables.append(key: 'CI_BUILD_REF', value: sha)
......
...@@ -221,12 +221,16 @@ class Commit ...@@ -221,12 +221,16 @@ class Commit
description.present? description.present?
end end
def timestamp
committed_date.xmlschema
end
def hook_attrs(with_changed_files: false) def hook_attrs(with_changed_files: false)
data = { data = {
id: id, id: id,
message: safe_message, message: safe_message,
title: title, title: title,
timestamp: committed_date.xmlschema, timestamp: timestamp,
url: Gitlab::UrlBuilder.build(self), url: Gitlab::UrlBuilder.build(self),
author: { author: {
name: author_name, name: author_name,
......
---
title: Add CI_COMMIT_TIMESTAMP CI variable
merge_request: 40388
author: Nasko Vasilev
type: added
...@@ -43,6 +43,7 @@ Kubernetes-specific environment variables are detailed in the ...@@ -43,6 +43,7 @@ Kubernetes-specific environment variables are detailed in the
| `CI_COMMIT_BRANCH` | 12.6 | 0.5 | The commit branch name. Present only when building branches. | | `CI_COMMIT_BRANCH` | 12.6 | 0.5 | The commit branch name. Present only when building branches. |
| `CI_COMMIT_TAG` | 9.0 | 0.5 | The commit tag name. Present only when building tags. | | `CI_COMMIT_TAG` | 9.0 | 0.5 | The commit tag name. Present only when building tags. |
| `CI_COMMIT_TITLE` | 10.8 | all | The title of the commit - the full first line of the message | | `CI_COMMIT_TITLE` | 10.8 | all | The title of the commit - the full first line of the message |
| `CI_COMMIT_TIMESTAMP` | 13.4 | all | The timestamp of the commit in the ISO 8601 format. |
| `CI_CONCURRENT_ID` | all | 11.10 | Unique ID of build execution within a single executor. | | `CI_CONCURRENT_ID` | all | 11.10 | Unique ID of build execution within a single executor. |
| `CI_CONCURRENT_PROJECT_ID` | all | 11.10 | Unique ID of build execution within a single executor and project. | | `CI_CONCURRENT_PROJECT_ID` | all | 11.10 | Unique ID of build execution within a single executor and project. |
| `CI_CONFIG_PATH` | 9.4 | 0.5 | The path to CI configuration file. Defaults to `.gitlab-ci.yml` | | `CI_CONFIG_PATH` | 9.4 | 0.5 | The path to CI configuration file. Defaults to `.gitlab-ci.yml` |
......
...@@ -50,6 +50,7 @@ RSpec.describe Ci::Bridge do ...@@ -50,6 +50,7 @@ RSpec.describe Ci::Bridge do
CI_PROJECT_PATH_SLUG CI_PROJECT_NAMESPACE CI_PROJECT_ROOT_NAMESPACE CI_PROJECT_PATH_SLUG CI_PROJECT_NAMESPACE CI_PROJECT_ROOT_NAMESPACE
CI_PIPELINE_IID CI_CONFIG_PATH CI_PIPELINE_SOURCE CI_COMMIT_MESSAGE CI_PIPELINE_IID CI_CONFIG_PATH CI_PIPELINE_SOURCE CI_COMMIT_MESSAGE
CI_COMMIT_TITLE CI_COMMIT_DESCRIPTION CI_COMMIT_REF_PROTECTED CI_COMMIT_TITLE CI_COMMIT_DESCRIPTION CI_COMMIT_REF_PROTECTED
CI_COMMIT_TIMESTAMP
] ]
expect(bridge.scoped_variables_hash.keys).to include(*variables) expect(bridge.scoped_variables_hash.keys).to include(*variables)
......
...@@ -2329,6 +2329,7 @@ RSpec.describe Ci::Build do ...@@ -2329,6 +2329,7 @@ RSpec.describe Ci::Build do
{ key: 'CI_COMMIT_TITLE', value: pipeline.git_commit_title, public: true, masked: false }, { key: 'CI_COMMIT_TITLE', value: pipeline.git_commit_title, public: true, masked: false },
{ key: 'CI_COMMIT_DESCRIPTION', value: pipeline.git_commit_description, public: true, masked: false }, { key: 'CI_COMMIT_DESCRIPTION', value: pipeline.git_commit_description, public: true, masked: false },
{ key: 'CI_COMMIT_REF_PROTECTED', value: (!!pipeline.protected_ref?).to_s, public: true, masked: false }, { key: 'CI_COMMIT_REF_PROTECTED', value: (!!pipeline.protected_ref?).to_s, public: true, masked: false },
{ key: 'CI_COMMIT_TIMESTAMP', value: pipeline.git_commit_timestamp, public: true, masked: false },
{ key: 'CI_BUILD_REF', value: build.sha, public: true, masked: false }, { key: 'CI_BUILD_REF', value: build.sha, public: true, masked: false },
{ key: 'CI_BUILD_BEFORE_SHA', value: build.before_sha, public: true, masked: false }, { key: 'CI_BUILD_BEFORE_SHA', value: build.before_sha, public: true, masked: false },
{ key: 'CI_BUILD_REF_NAME', value: build.ref, public: true, masked: false }, { key: 'CI_BUILD_REF_NAME', value: build.ref, public: true, masked: false },
......
...@@ -715,6 +715,7 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do ...@@ -715,6 +715,7 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
CI_COMMIT_TITLE CI_COMMIT_TITLE
CI_COMMIT_DESCRIPTION CI_COMMIT_DESCRIPTION
CI_COMMIT_REF_PROTECTED CI_COMMIT_REF_PROTECTED
CI_COMMIT_TIMESTAMP
CI_BUILD_REF CI_BUILD_REF
CI_BUILD_BEFORE_SHA CI_BUILD_BEFORE_SHA
CI_BUILD_REF_NAME CI_BUILD_REF_NAME
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment