Skip to content

Commit 2e731e7

Browse files
committed
Add buildkite_metadata action
1 parent 7b58e95 commit 2e731e7

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module Fastlane
2+
module Actions
3+
class BuildkiteMetadataAction < Action
4+
def self.run(params)
5+
# Set/Add new metadata values
6+
params[:set]&.each do |key, value|
7+
sh('buildkite-agent', 'meta-data', 'set', key.to_s, value.to_s)
8+
end
9+
10+
# Return value of existing metadata key
11+
sh('buildkite-agent', 'meta-data', 'get', params[:get].to_s) unless params[:get].nil?
12+
end
13+
14+
#####################################################
15+
# @!group Documentation
16+
#####################################################
17+
18+
def self.description
19+
'Set/Get metadata to the current Buildkite build'
20+
end
21+
22+
def self.details
23+
<<~DETAILS
24+
Set and/or get metadata to the current Buildkite build.
25+
26+
Has to be run on a CI job (where a `buildkite-agent` is running), e.g. typically by a lane
27+
that is triggered as part of a Buildkite CI step.
28+
29+
See https://buildkite.com/docs/agent/v3/cli-meta-data
30+
DETAILS
31+
end
32+
33+
def self.available_options
34+
[
35+
FastlaneCore::ConfigItem.new(
36+
key: :set,
37+
env_name: 'BUILDKITE_METADATA_SET',
38+
description: 'The hash of key/value pairs of the meta-data to set',
39+
type: Hash,
40+
optional: true,
41+
default_value: nil
42+
),
43+
FastlaneCore::ConfigItem.new(
44+
key: :get,
45+
env_name: 'BUILDKITE_METADATA_GET',
46+
description: 'The key of the metadata to get the value of',
47+
type: String,
48+
optional: true,
49+
default_value: nil
50+
),
51+
]
52+
end
53+
54+
def self.return_value
55+
'The value of the Buildkite metadata corresponding to the provided `get` key. `nil` if no `get` parameter was provided.'
56+
end
57+
58+
def self.authors
59+
['Automattic']
60+
end
61+
62+
def self.is_supported?(platform)
63+
true
64+
end
65+
end
66+
end
67+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'spec_helper'
2+
3+
describe Fastlane::Actions::BuildkiteMetadataAction do
4+
it 'calls the right command to set a single metadata' do
5+
expect(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'set', 'foo', 'bar')
6+
7+
res = run_described_fastlane_action(set: { foo: 'bar' })
8+
expect(res).to be_nil
9+
end
10+
11+
it 'calls the commands as many times as necessary when we want to set multiple metadata at once' do
12+
expect(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'set', 'key1', 'value1')
13+
expect(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'set', 'key2', 'value2')
14+
15+
metadata = {
16+
key1: 'value1',
17+
key2: 'value2'
18+
}
19+
run_described_fastlane_action(set: metadata)
20+
end
21+
22+
it 'calls the right command to get the value of metadata, and returns the right value' do
23+
expect(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'get', 'foo')
24+
allow(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'get', 'foo').and_return('foo value')
25+
26+
res = run_described_fastlane_action(get: 'foo')
27+
expect(res).to eq('foo value')
28+
end
29+
30+
it 'allows both setting and getting metadata in the same call' do
31+
# Might not be the main way we intend to use this action… but it's still supported.
32+
expect(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'set', 'key1', 'value1')
33+
expect(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'set', 'key2', 'value2')
34+
expect(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'get', 'key3')
35+
allow(Fastlane::Action).to receive(:sh).with('buildkite-agent', 'meta-data', 'get', 'key3').and_return('value3')
36+
37+
new_metadata = {
38+
key1: 'value1',
39+
key2: 'value2'
40+
}
41+
res = run_described_fastlane_action(set: new_metadata, get: 'key3')
42+
43+
expect(res).to eq('value3')
44+
end
45+
end

0 commit comments

Comments
 (0)