Skip to content

Commit 3f932e1

Browse files
committed
Remove branch and commit parameters and add environment
1 parent ad7d098 commit 3f932e1

File tree

2 files changed

+50
-79
lines changed

2 files changed

+50
-79
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/actions/common/buildkite_upload_pipeline_action.rb

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,23 @@ module Fastlane
22
module Actions
33
class BuildkiteUploadPipelineAction < Action
44
DEFAULT_ENV_FILE = File.join('.buildkite', 'shared-pipeline-vars').freeze
5-
DEFAULT_COMMIT = 'HEAD'.freeze
65

76
def self.run(params)
8-
env_file = params[:env_file]
97
pipeline_file = params[:pipeline_file]
10-
branch = params[:branch]
11-
commit = params[:commit]
8+
env_file = params[:env_file]
9+
environment = params[:environment]
1210

1311
UI.user_error!("Pipeline file not found: #{pipeline_file}") unless File.exist?(pipeline_file)
14-
UI.user_error!('You should not provide both `branch` and `commit`') if !branch.nil? && commit != DEFAULT_COMMIT
1512
UI.user_error!('This action can only be called from a Buildkite CI build') unless ENV['BUILDKITE'] == 'true'
1613

17-
UI.message "Adding steps from `#{pipeline_file}` to the current build (#{"branch: `#{branch}`, " if branch}commit: `#{commit}`)"
18-
19-
env_vars = {
20-
'BUILDKITE_BRANCH' => branch,
21-
'BUILDKITE_COMMIT' => commit
22-
}.compact
14+
UI.message "Adding steps from `#{pipeline_file}` to the current build"
2315

2416
if env_file && File.exist?(env_file)
2517
UI.message(" - Sourcing environment file beforehand: #{env_file}")
2618

27-
sh(env_vars, "source #{env_file.shellescape} && buildkite-agent pipeline upload #{pipeline_file.shellescape}")
19+
sh(environment, "source #{env_file.shellescape} && buildkite-agent pipeline upload #{pipeline_file.shellescape}")
2820
else
29-
sh(env_vars, 'buildkite-agent', 'pipeline', 'upload', pipeline_file)
21+
sh(environment, 'buildkite-agent', 'pipeline', 'upload', pipeline_file)
3022
end
3123
end
3224

@@ -51,17 +43,10 @@ def self.available_options
5143
type: String
5244
),
5345
FastlaneCore::ConfigItem.new(
54-
key: :branch,
55-
description: 'The branch you want to run the pipeline on',
56-
optional: true,
57-
type: String
58-
),
59-
FastlaneCore::ConfigItem.new(
60-
key: :commit,
61-
description: 'The commit hash you want to run the pipeline on',
62-
optional: true,
63-
default_value: DEFAULT_COMMIT,
64-
type: String
46+
key: :environment,
47+
description: 'Environment variables to load when running `pipeline upload`, to allow for variable substitution in the YAML pipeline',
48+
type: Hash,
49+
default_value: {}
6550
),
6651
]
6752
end

spec/buildkite_upload_pipeline_action_spec.rb

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
describe Fastlane::Actions::BuildkiteUploadPipelineAction do
44
let(:pipeline_file) { 'path/to/pipeline.yml' }
5-
let(:branch) { 'feature-branch' }
6-
let(:commit) { 'abc123' }
7-
let(:commit_default) { Fastlane::Actions::BuildkiteUploadPipelineAction::DEFAULT_COMMIT }
85
let(:env_file) { 'path/to/env_file' }
96
let(:env_file_default) { Fastlane::Actions::BuildkiteUploadPipelineAction::DEFAULT_ENV_FILE }
7+
let(:environment) { { 'AKEY' => 'AVALUE' } }
8+
let(:environment_default) { {} }
109

1110
before do
1211
allow(File).to receive(:exist?).with(anything)
@@ -17,70 +16,58 @@
1716
describe 'parameter validation' do
1817
it 'raises an error when pipeline_file is not provided' do
1918
expect do
20-
run_described_fastlane_action(branch: branch)
19+
run_described_fastlane_action(
20+
environment: environment
21+
)
2122
end.to raise_error(FastlaneCore::Interface::FastlaneError, /pipeline_file/)
2223
end
2324

2425
it 'raises an error when pipeline_file does not exist' do
2526
allow(File).to receive(:exist?).with(pipeline_file).and_return(false)
2627
expect do
27-
run_described_fastlane_action(
28-
pipeline_file: pipeline_file,
29-
branch: branch
30-
)
28+
run_described_fastlane_action(pipeline_file: pipeline_file)
3129
end.to raise_error(FastlaneCore::Interface::FastlaneError, /Pipeline file not found/)
3230
end
3331

34-
it 'raises an error when both branch and commit are provided' do
32+
it 'raises an error when not running on Buildkite' do
3533
allow(File).to receive(:exist?).with(pipeline_file).and_return(true)
34+
allow(ENV).to receive(:[]).with('BUILDKITE').and_return(nil)
35+
3636
expect do
37-
run_described_fastlane_action(
38-
pipeline_file: pipeline_file,
39-
branch: branch,
40-
commit: commit
41-
)
42-
end.to raise_error(FastlaneCore::Interface::FastlaneError, /You should not provide both `branch` and `commit`/)
37+
run_described_fastlane_action(pipeline_file: pipeline_file)
38+
end.to raise_error(FastlaneCore::Interface::FastlaneError, /This action can only be called from a Buildkite CI build/)
4339
end
4440

45-
it 'uses the default value for commit when not provided' do
41+
it 'passes the environment hash to the shell command' do
4642
allow(File).to receive(:exist?).with(pipeline_file).and_return(true)
4743
expect(Fastlane::Action).to receive(:sh).with(
48-
{ 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default },
44+
environment,
4945
'buildkite-agent', 'pipeline', 'upload', pipeline_file
5046
)
5147
expect_upload_pipeline_message
5248

5349
run_described_fastlane_action(
5450
pipeline_file: pipeline_file,
55-
branch: branch
51+
environment: environment
5652
)
5753
end
5854

59-
it 'uses the provided value for the commit' do
55+
it 'passes the environment hash to the shell command also with an env_file' do
6056
allow(File).to receive(:exist?).with(pipeline_file).and_return(true)
57+
allow(File).to receive(:exist?).with(env_file).and_return(true)
6158
expect(Fastlane::Action).to receive(:sh).with(
62-
{ 'BUILDKITE_COMMIT' => commit },
63-
'buildkite-agent', 'pipeline', 'upload', pipeline_file
59+
environment,
60+
"source #{env_file.shellescape} && buildkite-agent pipeline upload #{pipeline_file.shellescape}"
6461
)
65-
expect_upload_pipeline_message(expected_branch: nil, expected_commit: commit)
62+
expect_upload_pipeline_message
63+
expect_sourcing_env_file_message(env_file)
6664

6765
run_described_fastlane_action(
6866
pipeline_file: pipeline_file,
69-
commit: commit
67+
environment: environment,
68+
env_file: env_file
7069
)
7170
end
72-
73-
it 'raises an error when not running on Buildkite' do
74-
allow(File).to receive(:exist?).with(pipeline_file).and_return(true)
75-
allow(ENV).to receive(:[]).with('BUILDKITE').and_return(nil)
76-
77-
expect do
78-
run_described_fastlane_action(
79-
pipeline_file: pipeline_file,
80-
branch: branch
81-
)
82-
end.to raise_error(FastlaneCore::Interface::FastlaneError, /This action can only be called from a Buildkite CI build/)
83-
end
8471
end
8572

8673
describe 'pipeline upload' do
@@ -90,61 +77,55 @@
9077

9178
it 'calls the right command to upload the pipeline without env_file' do
9279
expect(Fastlane::Action).to receive(:sh).with(
93-
{ 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default },
80+
environment_default,
9481
'buildkite-agent', 'pipeline', 'upload', pipeline_file
9582
)
9683
expect_upload_pipeline_message
9784

98-
run_described_fastlane_action(
99-
pipeline_file: pipeline_file,
100-
branch: branch
101-
)
85+
run_described_fastlane_action(pipeline_file: pipeline_file)
10286
end
10387

10488
it 'calls the right command to upload the pipeline with env_file' do
10589
allow(File).to receive(:exist?).with(env_file).and_return(true)
10690
expect(Fastlane::Action).to receive(:sh).with(
107-
{ 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default },
91+
environment_default,
10892
"source #{env_file.shellescape} && buildkite-agent pipeline upload #{pipeline_file.shellescape}"
10993
)
11094
expect_upload_pipeline_message
111-
expect(Fastlane::UI).to receive(:message).with(/Sourcing environment file beforehand: #{env_file}/)
95+
expect_sourcing_env_file_message(env_file)
11296

11397
run_described_fastlane_action(
11498
pipeline_file: pipeline_file,
115-
env_file: env_file,
116-
branch: branch
99+
env_file: env_file
117100
)
118101
end
119102

120103
it 'skips sourcing env_file when it does not exist' do
121104
non_existent_env_file = 'path/to/non_existent_env_file'
122105
allow(File).to receive(:exist?).with(non_existent_env_file).and_return(false)
123106
expect(Fastlane::Action).to receive(:sh).with(
124-
{ 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default },
107+
environment_default,
125108
'buildkite-agent', 'pipeline', 'upload', pipeline_file
126109
)
127110
expect(Fastlane::UI).not_to receive(:message).with(/Sourcing environment file/)
128111

129112
run_described_fastlane_action(
130113
pipeline_file: pipeline_file,
131-
env_file: non_existent_env_file,
132-
branch: branch
114+
env_file: non_existent_env_file
133115
)
134116
end
135117

136118
it 'uses a default env_file when no env_file is provided' do
137119
allow(File).to receive(:exist?).with(env_file_default).and_return(true)
138120
expect(Fastlane::Action).to receive(:sh).with(
139-
{ 'BUILDKITE_BRANCH' => branch, 'BUILDKITE_COMMIT' => commit_default },
121+
environment_default,
140122
"source #{env_file_default} && buildkite-agent pipeline upload #{pipeline_file.shellescape}"
141123
)
142124
expect_upload_pipeline_message
143-
expect(Fastlane::UI).to receive(:message).with(/Sourcing environment file beforehand: #{env_file_default}/)
125+
expect_sourcing_env_file_message(env_file_default)
144126

145127
run_described_fastlane_action(
146-
pipeline_file: pipeline_file,
147-
branch: branch
128+
pipeline_file: pipeline_file
148129
)
149130
end
150131
end
@@ -156,16 +137,21 @@
156137

157138
expect do
158139
run_described_fastlane_action(
159-
pipeline_file: pipeline_file,
160-
branch: branch
140+
pipeline_file: pipeline_file
161141
)
162142
end.to raise_error(StandardError, 'Upload failed')
163143
end
164144
end
165145

166-
def expect_upload_pipeline_message(expected_branch: branch, expected_commit: commit_default)
146+
def expect_upload_pipeline_message
147+
expect(Fastlane::UI).to receive(:message).with(
148+
"Adding steps from `#{pipeline_file}` to the current build"
149+
)
150+
end
151+
152+
def expect_sourcing_env_file_message(env_file)
167153
expect(Fastlane::UI).to receive(:message).with(
168-
"Adding steps from `#{pipeline_file}` to the current build (#{expected_branch ? "branch: `#{expected_branch}`, " : ''}commit: `#{expected_commit}`)"
154+
/Sourcing environment file beforehand: #{env_file}/
169155
)
170156
end
171157
end

0 commit comments

Comments
 (0)