Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pipeline.rb « builder « variables « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e7a18d70b0d1740dbb7631701592c1afd1e9267 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true

module Gitlab
  module Ci
    module Variables
      class Builder
        class Pipeline
          include Gitlab::Utils::StrongMemoize

          def initialize(pipeline)
            @pipeline = pipeline
          end

          def predefined_variables
            Gitlab::Ci::Variables::Collection.new.tap do |variables|
              variables.append(key: 'CI_PIPELINE_IID', value: pipeline.iid.to_s)
              variables.append(key: 'CI_PIPELINE_SOURCE', value: pipeline.source.to_s)
              variables.append(key: 'CI_PIPELINE_CREATED_AT', value: pipeline.created_at&.iso8601)

              variables.concat(predefined_commit_variables) if pipeline.sha.present?
              variables.concat(predefined_commit_tag_variables) if pipeline.tag?
              variables.concat(predefined_merge_request_variables) if pipeline.merge_request?

              if pipeline.open_merge_requests_refs.any?
                variables.append(key: 'CI_OPEN_MERGE_REQUESTS', value: pipeline.open_merge_requests_refs.join(','))
              end

              variables.append(key: 'CI_GITLAB_FIPS_MODE', value: 'true') if Gitlab::FIPS.enabled?

              variables.append(key: 'CI_KUBERNETES_ACTIVE', value: 'true') if pipeline.has_kubernetes_active?
              variables.append(key: 'CI_DEPLOY_FREEZE', value: 'true') if pipeline.freeze_period?

              if pipeline.external_pull_request_event? && pipeline.external_pull_request
                variables.concat(pipeline.external_pull_request.predefined_variables)
              end
            end
          end

          private

          attr_reader :pipeline

          def predefined_commit_variables # rubocop:disable Metrics/AbcSize - Remove this rubocop:disable when FF `ci_remove_legacy_predefined_variables` is removed.
            Gitlab::Ci::Variables::Collection.new.tap do |variables|
              next variables unless pipeline.sha.present?

              variables.append(key: 'CI_COMMIT_SHA', value: pipeline.sha)
              variables.append(key: 'CI_COMMIT_SHORT_SHA', value: pipeline.short_sha)
              variables.append(key: 'CI_COMMIT_BEFORE_SHA', value: pipeline.before_sha)
              variables.append(key: 'CI_COMMIT_REF_NAME', value: pipeline.source_ref)
              variables.append(key: 'CI_COMMIT_REF_SLUG', value: pipeline.source_ref_slug)
              variables.append(key: 'CI_COMMIT_BRANCH', value: pipeline.ref) if pipeline.branch?
              variables.append(key: 'CI_COMMIT_MESSAGE', value: pipeline.git_commit_message.to_s)
              variables.append(key: 'CI_COMMIT_TITLE', value: pipeline.git_commit_full_title.to_s)
              variables.append(key: 'CI_COMMIT_DESCRIPTION', value: pipeline.git_commit_description.to_s)
              variables.append(key: 'CI_COMMIT_REF_PROTECTED', value: (!!pipeline.protected_ref?).to_s)
              variables.append(key: 'CI_COMMIT_TIMESTAMP', value: pipeline.git_commit_timestamp.to_s)
              variables.append(key: 'CI_COMMIT_AUTHOR', value: pipeline.git_author_full_text.to_s)

              if Feature.disabled?(:ci_remove_legacy_predefined_variables, pipeline.project)
                variables.concat(legacy_predefined_commit_variables)
              end
            end
          end
          strong_memoize_attr :predefined_commit_variables

          def legacy_predefined_commit_variables
            Gitlab::Ci::Variables::Collection.new.tap do |variables|
              variables.append(key: 'CI_BUILD_REF', value: pipeline.sha)
              variables.append(key: 'CI_BUILD_BEFORE_SHA', value: pipeline.before_sha)
              variables.append(key: 'CI_BUILD_REF_NAME', value: pipeline.source_ref)
              variables.append(key: 'CI_BUILD_REF_SLUG', value: pipeline.source_ref_slug)
            end
          end
          strong_memoize_attr :legacy_predefined_commit_variables

          def predefined_commit_tag_variables
            Gitlab::Ci::Variables::Collection.new.tap do |variables|
              git_tag = pipeline.project.repository.find_tag(pipeline.ref)

              next variables unless git_tag

              variables.append(key: 'CI_COMMIT_TAG', value: pipeline.ref)
              variables.append(key: 'CI_COMMIT_TAG_MESSAGE', value: git_tag.message)

              if Feature.disabled?(:ci_remove_legacy_predefined_variables, pipeline.project)
                variables.concat(legacy_predefined_commit_tag_variables)
              end
            end
          end
          strong_memoize_attr :predefined_commit_tag_variables

          def legacy_predefined_commit_tag_variables
            Gitlab::Ci::Variables::Collection.new.tap do |variables|
              variables.append(key: 'CI_BUILD_TAG', value: pipeline.ref)
            end
          end
          strong_memoize_attr :legacy_predefined_commit_tag_variables

          def predefined_merge_request_variables
            Gitlab::Ci::Variables::Collection.new.tap do |variables|
              variables.append(key: 'CI_MERGE_REQUEST_EVENT_TYPE', value: pipeline.merge_request_event_type.to_s)
              variables.append(key: 'CI_MERGE_REQUEST_SOURCE_BRANCH_SHA', value: pipeline.source_sha.to_s)
              variables.append(key: 'CI_MERGE_REQUEST_TARGET_BRANCH_SHA', value: pipeline.target_sha.to_s)

              if merge_request_diff.present?
                variables.append(key: 'CI_MERGE_REQUEST_DIFF_ID', value: merge_request_diff.id.to_s)
                variables.append(key: 'CI_MERGE_REQUEST_DIFF_BASE_SHA', value: merge_request_diff.base_commit_sha)
              end

              variables.concat(pipeline.merge_request.predefined_variables)
            end
          end
          strong_memoize_attr :predefined_merge_request_variables

          def merge_request_diff
            pipeline.merge_request_diff
          end
          strong_memoize_attr :merge_request_diff
        end
      end
    end
  end
end