Commit 88ba7a03 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Pass external_url from environment if job doesn't have one

Also update the document and add a changelog entry
parent 86cd09f5
...@@ -501,9 +501,15 @@ module Ci ...@@ -501,9 +501,15 @@ module Ci
def persisted_environment_variables def persisted_environment_variables
variables = persisted_environment.predefined_variables variables = persisted_environment.predefined_variables
variables << { key: 'CI_ENVIRONMENT_URL', if environment_url
value: expanded_environment_url, variables << { key: 'CI_ENVIRONMENT_URL',
public: true } if environment_url value: expanded_environment_url,
public: true }
elsif persisted_environment.external_url.present?
variables << { key: 'CI_ENVIRONMENT_URL',
value: persisted_environment.external_url,
public: true }
end
variables variables
end end
......
---
title: Add $CI_ENVIRONMENT_URL to predefined variables for pipelines
merge_request: 11695
author:
...@@ -94,6 +94,12 @@ the name given in `.gitlab-ci.yml` (with any variables expanded), while the ...@@ -94,6 +94,12 @@ the name given in `.gitlab-ci.yml` (with any variables expanded), while the
second is a "cleaned-up" version of the name, suitable for use in URLs, DNS, second is a "cleaned-up" version of the name, suitable for use in URLs, DNS,
etc. etc.
>**Note:**
Starting with GitLab 9.3, the environment URL is exposed to the Runner via
`$CI_ENVIRONMENT_URL`. The URL would be expanded from `.gitlab-ci.yml`, or if
the URL was not defined there, the external URL from the environment would be
used.
To sum up, with the above `.gitlab-ci.yml` we have achieved that: To sum up, with the above `.gitlab-ci.yml` we have achieved that:
- All branches will run the `test` and `build` jobs. - All branches will run the `test` and `build` jobs.
......
...@@ -43,6 +43,7 @@ future GitLab releases.** ...@@ -43,6 +43,7 @@ future GitLab releases.**
| **CI_DEBUG_TRACE** | all | 1.7 | Whether [debug tracing](#debug-tracing) is enabled | | **CI_DEBUG_TRACE** | all | 1.7 | Whether [debug tracing](#debug-tracing) is enabled |
| **CI_ENVIRONMENT_NAME** | 8.15 | all | The name of the environment for this job | | **CI_ENVIRONMENT_NAME** | 8.15 | all | The name of the environment for this job |
| **CI_ENVIRONMENT_SLUG** | 8.15 | all | A simplified version of the environment name, suitable for inclusion in DNS, URLs, Kubernetes labels, etc. | | **CI_ENVIRONMENT_SLUG** | 8.15 | all | A simplified version of the environment name, suitable for inclusion in DNS, URLs, Kubernetes labels, etc. |
| **CI_ENVIRONMENT_URL** | 9.3 | all | The URL of the environment for this job |
| **CI_JOB_ID** | 9.0 | all | The unique id of the current job that GitLab CI uses internally | | **CI_JOB_ID** | 9.0 | all | The unique id of the current job that GitLab CI uses internally |
| **CI_JOB_MANUAL** | 8.12 | all | The flag to indicate that job was manually started | | **CI_JOB_MANUAL** | 8.12 | all | The flag to indicate that job was manually started |
| **CI_JOB_NAME** | 9.0 | 0.5 | The name of the job as defined in `.gitlab-ci.yml` | | **CI_JOB_NAME** | 9.0 | 0.5 | The name of the job as defined in `.gitlab-ci.yml` |
......
...@@ -20,7 +20,7 @@ describe Ci::Build, :models do ...@@ -20,7 +20,7 @@ describe Ci::Build, :models do
it { is_expected.to validate_presence_of(:ref) } it { is_expected.to validate_presence_of(:ref) }
it { is_expected.to respond_to(:has_trace?) } it { is_expected.to respond_to(:has_trace?) }
it { is_expected.to respond_to(:trace) } it { is_expected.to respond_to(:trace) }
it { is_expected.to validate_length_of(:external_url).is_at_most(255) } it { is_expected.to validate_length_of(:environment_url).is_at_most(255) }
describe '#actionize' do describe '#actionize' do
context 'when build is a created' do context 'when build is a created' do
...@@ -1208,33 +1208,55 @@ describe Ci::Build, :models do ...@@ -1208,33 +1208,55 @@ describe Ci::Build, :models do
] ]
end end
let!(:environment) do
create(:environment,
project: build.project,
name: 'production',
slug: 'prod-slug',
external_url: '')
end
before do before do
build.update(environment: 'production') build.update(environment: 'production')
create(:environment, project: build.project, name: 'production', slug: 'prod-slug')
end end
context 'when no URL was set' do shared_examples 'containing environment variables' do
it { environment_variables.each { |v| is_expected.to include(v) } } it { environment_variables.each { |v| is_expected.to include(v) } }
end
context 'when no URL was set' do
it_behaves_like 'containing environment variables'
it 'does not have CI_ENVIRONMENT_URL' do it 'does not have CI_ENVIRONMENT_URL' do
keys = subject.map { |var| var[:key] } keys = subject.map { |var| var[:key] }
expect(keys).to include('CI_ENVIRONMENT_NAME', 'CI_ENVIRONMENT_SLUG')
expect(keys).not_to include('CI_ENVIRONMENT_URL') expect(keys).not_to include('CI_ENVIRONMENT_URL')
end end
end end
context 'when an URL was set' do context 'when an URL was set' do
before do let(:url) { 'http://host/test' }
build.update(environment_url: 'http://host/$CI_JOB_NAME')
before do
environment_variables << environment_variables <<
{ key: 'CI_ENVIRONMENT_URL', { key: 'CI_ENVIRONMENT_URL', value: url, public: true }
value: 'http://host/test',
public: true }
end end
it { environment_variables.each { |v| is_expected.to include(v) } } context 'when the URL was set from the job' do
before do
build.update(environment_url: 'http://host/$CI_JOB_NAME')
end
it_behaves_like 'containing environment variables'
end
context 'when the URL was not set from the job, but environment' do
before do
environment.update(external_url: url)
end
it_behaves_like 'containing environment variables'
end
end end
end end
......
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