Commit 303f0493 authored by Douwe Maan's avatar Douwe Maan

Merge branch 'feature/gb/expose-ci-api-url-variable' into 'master'

Expose `CI_API_V4_URL` CI/CD variable

Closes #54621

See merge request gitlab-org/gitlab-ce!23936
parents d85e04e4 8707827d
......@@ -1705,6 +1705,13 @@ class Project < ActiveRecord::Base
.append(key: 'CI_PROJECT_VISIBILITY', value: visibility)
.concat(container_registry_variables)
.concat(auto_devops_variables)
.concat(api_variables)
end
def api_variables
Gitlab::Ci::Variables::Collection.new.tap do |variables|
variables.append(key: 'CI_API_V4_URL', value: API::Helpers::Version.new('v4').root_url)
end
end
def container_registry_variables
......
---
title: Expose CI/CD predefined variable `CI_API_V4_URL`
merge_request: 23936
author:
type: added
......@@ -80,7 +80,8 @@ future GitLab releases.**
| **CI_MERGE_REQUEST_TARGET_BRANCH_NAME** | 11.6 | all | The target branch name of the merge request if it's [pipelines for merge requests](../merge_request_pipelines/index.md) |
| **CI_NODE_INDEX** | 11.5 | all | Index of the job in the job set. If the job is not parallelized, this variable is not set. |
| **CI_NODE_TOTAL** | 11.5 | all | Total number of instances of this job running in parallel. If the job is not parallelized, this variable is set to `1`. |
| **CI_PIPELINE_ID** | 8.10 | 0.5 | The unique id of the current pipeline that GitLab CI uses internally |
| **CI_API_V4_URL** | 11.7 | all | The GitLab API v4 root URL |
| **CI_PIPELINE_ID** | 8.10 | all | The unique id of the current pipeline that GitLab CI uses internally |
| **CI_PIPELINE_IID** | 11.0 | all | The unique id of the current pipeline scoped to project |
| **CI_PIPELINE_SOURCE** | 10.0 | all | Indicates how the pipeline was triggered. Possible options are: `push`, `web`, `trigger`, `schedule`, `api`, and `pipeline`. For pipelines created before GitLab 9.5, this will show as `unknown` |
| **CI_PIPELINE_TRIGGERED** | all | all | The flag to indicate that job was [triggered] |
......
# frozen_string_literal: true
module API
module Helpers
class Version
include Helpers::RelatedResourcesHelpers
def initialize(version)
@version = version.to_s
unless API.versions.include?(version)
raise ArgumentError, 'Unknown API version!'
end
end
def root_path
File.join('/', API.prefix.to_s, @version)
end
def root_url
@root_url ||= expose_url(root_path)
end
def to_s
@version
end
end
end
end
require 'spec_helper'
describe API::API do
describe '.prefix' do
it 'has a prefix defined' do
expect(described_class.prefix).to eq :api
end
end
describe '.version' do
it 'uses most recent version of the API' do
expect(described_class.version).to eq 'v4'
end
end
describe '.versions' do
it 'returns all available versions' do
expect(described_class.versions).to eq %w[v3 v4]
end
end
end
require 'spec_helper'
describe API::Helpers::Version do
describe '.new' do
it 'is possible to initialize it with existing API version' do
expect(described_class.new('v4').to_s).to eq 'v4'
end
it 'raises an error when unsupported API version is provided' do
expect { described_class.new('v111') }.to raise_error ArgumentError
end
end
describe '#root_path' do
it 'returns a root path of the API version' do
expect(described_class.new('v4').root_path).to eq '/api/v4'
end
end
describe '#root_url' do
it 'returns an URL for a root path for the API version' do
expect(described_class.new('v4').root_url)
.to eq 'http://localhost/api/v4'
end
end
end
......@@ -2132,6 +2132,7 @@ describe Ci::Build do
{ key: 'CI_PROJECT_NAMESPACE', value: project.namespace.full_path, public: true },
{ key: 'CI_PROJECT_URL', value: project.web_url, public: true },
{ key: 'CI_PROJECT_VISIBILITY', value: 'private', public: true },
{ key: 'CI_API_V4_URL', value: 'http://localhost/api/v4', public: true },
{ key: 'CI_PIPELINE_IID', value: pipeline.iid.to_s, public: true },
{ key: 'CI_CONFIG_PATH', value: pipeline.ci_yaml_file_path, public: true },
{ key: 'CI_PIPELINE_SOURCE', value: pipeline.source, public: true },
......
......@@ -3525,7 +3525,29 @@ describe Project do
end
end
context '#auto_devops_variables' do
describe '#api_variables' do
set(:project) { create(:project) }
it 'exposes API v4 URL' do
expect(project.api_variables.first[:key]).to eq 'CI_API_V4_URL'
expect(project.api_variables.first[:value]).to include '/api/v4'
end
it 'contains a URL variable for every supported API version' do
supported_versions = API::API.versions.select do |version|
API::API.routes.select { |route| route.version == version }.many?
end
required_variables = supported_versions.map do |version|
"CI_API_#{version.upcase}_URL"
end
expect(project.api_variables.map { |variable| variable[:key] })
.to contain_exactly(*required_variables)
end
end
describe '#auto_devops_variables' do
set(:project) { create(:project) }
subject { project.auto_devops_variables }
......
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