Commit 2e5b4dbf authored by Stan Hu's avatar Stan Hu

Gracefully handle duplicate dotenv keys

parent 269db476
......@@ -33,13 +33,13 @@ module Ci
end
def parse!(artifact)
variables = []
variables = {}
artifact.each_blob do |blob|
blob.each_line do |line|
key, value = scan_line!(line)
variables << Ci::JobVariable.new(job_id: artifact.job_id,
variables[key] = Ci::JobVariable.new(job_id: artifact.job_id,
source: :dotenv, key: key, value: value)
end
end
......@@ -49,7 +49,7 @@ module Ci
"Dotenv files cannot have more than #{dotenv_variable_limit} variables"
end
variables
variables.values
end
def scan_line!(line)
......
......@@ -169,6 +169,11 @@ The `dotenv` report collects a set of environment variables as artifacts.
The collected variables are registered as runtime-created variables of the job,
which you can use to [set dynamic environment URLs after a job finishes](../environments/index.md#set-dynamic-environment-urls-after-a-job-finishes).
If duplicate environment variables are present in a `dotenv` report:
- In GitLab 14.6 and later, the last one specified is used.
- In GitLab 14.5 and earlier, an error occurs.
The exceptions to the [original dotenv rules](https://github.com/motdotla/dotenv#rules) are:
- The variable key can contain only letters, digits, and underscores (`_`).
......
......@@ -37,6 +37,32 @@ RSpec.describe Ci::ParseDotenvArtifactService do
end
end
context 'when dotenv variables have duplicate variables' do
let!(:artifact) { create(:ci_job_artifact, :dotenv, job: build) }
let(:blob) do
<<~EOS
KEY1=VAR1
KEY2=VAR2
KEY2=VAR3
KEY1=VAR4
EOS
end
before do
allow(artifact).to receive(:each_blob).and_yield(blob)
end
it 'latest values get used' do
subject
expect(subject[:status]).to eq(:success)
expect(build.job_variables.as_json).to contain_exactly(
hash_including('key' => 'KEY1', 'value' => 'VAR4'),
hash_including('key' => 'KEY2', 'value' => 'VAR3'))
end
end
context 'when parse error happens' do
before do
allow(service).to receive(:scan_line!) { raise described_class::ParserError, 'Invalid Format' }
......
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