Skip to content

Commit 0b29fe9

Browse files
authored
[Actions] Add setup_circle_ci action (fastlane#10415)
This adds a setup_circle_ci action to make it super easy to get setup with fastlane on CircleCI 2.0
1 parent b8948d8 commit 0b29fe9

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
module Fastlane
2+
module Actions
3+
class SetupCircleCiAction < Action
4+
def self.run(params)
5+
unless should_run?(params)
6+
UI.message "Not running on CI, skipping `setup_circle_ci`"
7+
return
8+
end
9+
10+
setup_keychain
11+
setup_output_paths(params)
12+
end
13+
14+
def self.setup_output_paths(params)
15+
unless ENV["FL_OUTPUT_DIR"]
16+
UI.message "Skipping Log Path setup as FL_OUTPUT_DIR is unset"
17+
return
18+
end
19+
20+
root = Pathname.new(ENV["FL_OUTPUT_DIR"])
21+
ENV["SCAN_OUTPUT_DIRECTORY"] = (root + "scan").to_s
22+
ENV["GYM_OUTPUT_DIRECTORY"] = (root + "gym").to_s
23+
ENV["FL_BUILDLOG_PATH"] = (root + "buildlogs").to_s
24+
ENV["SCAN_INCLUDE_SIMULATOR_LOGS"] = true.to_s
25+
end
26+
27+
def self.setup_keychain
28+
unless ENV["MATCH_KEYCHAIN_NAME"].nil?
29+
UI.message "Skipping Keychain setup as a keychain was already specified"
30+
return
31+
end
32+
33+
keychain_name = "fastlane_tmp_keychain"
34+
ENV["MATCH_KEYCHAIN_NAME"] = keychain_name
35+
ENV["MATCH_KEYCHAIN_PASSWORD"] = ""
36+
37+
UI.message "Creating temporary keychain: \"#{keychain_name}\"."
38+
Actions::CreateKeychainAction.run(
39+
name: keychain_name,
40+
default_keychain: true,
41+
unlock: true,
42+
timeout: 3600,
43+
lock_when_sleeps: true,
44+
password: ""
45+
)
46+
47+
UI.message("Enabling match readonly mode.")
48+
ENV["MATCH_READONLY"] = true.to_s
49+
end
50+
51+
def self.should_run?(params)
52+
Helper.is_ci? || params[:force]
53+
end
54+
55+
#####################################################
56+
# @!group Documentation
57+
#####################################################
58+
59+
def self.description
60+
"Setup the keychain and match to work with CircleCI"
61+
end
62+
63+
def self.details
64+
[
65+
"- Creates a new temporary keychain for use with match",
66+
"- Switches match to `readonly` mode to not create new profiles/cert on CI",
67+
"- Sets up log and test result paths to be easily collectible",
68+
"",
69+
"This action helps with CircleCI integration, add this to the top of your Fastfile if you use CircleCI"
70+
].join("\n")
71+
end
72+
73+
def self.available_options
74+
[
75+
FastlaneCore::ConfigItem.new(key: :force,
76+
env_name: "FL_SETUP_CIRCLECI_FORCE",
77+
description: "Force setup, even if not executed by CircleCI",
78+
is_string: false,
79+
default_value: false)
80+
]
81+
end
82+
83+
def self.authors
84+
["dantoml"]
85+
end
86+
87+
def self.is_supported?(platform)
88+
[:ios, :mac].include?(platform)
89+
end
90+
91+
def self.example_code
92+
[
93+
'setup_circle_ci'
94+
]
95+
end
96+
97+
def self.category
98+
:misc
99+
end
100+
end
101+
end
102+
end
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
describe Fastlane do
2+
describe Fastlane::Actions::SetupCircleCiAction do
3+
describe "#setup_output_paths" do
4+
before do
5+
stub_const("ENV", { "FL_OUTPUT_DIR" => "/dev/null" })
6+
end
7+
8+
it "sets the SCAN_OUTPUT_DIRECTORY" do
9+
described_class.setup_output_paths(nil)
10+
expect(ENV["SCAN_OUTPUT_DIRECTORY"]).to eql("/dev/null/scan")
11+
end
12+
13+
it "sets the GYM_OUTPUT_DIRECTORY" do
14+
described_class.setup_output_paths(nil)
15+
expect(ENV["GYM_OUTPUT_DIRECTORY"]).to eql("/dev/null/gym")
16+
end
17+
18+
it "sets the FL_BUILDLOG_PATH" do
19+
described_class.setup_output_paths(nil)
20+
expect(ENV["FL_BUILDLOG_PATH"]).to eql("/dev/null/buildlogs")
21+
end
22+
end
23+
24+
describe "#should_run" do
25+
context "when running on CI" do
26+
before do
27+
expect(Fastlane::Helper).to receive(:is_ci?).and_return(true)
28+
end
29+
30+
it "returns true when :force is true" do
31+
expect(described_class.should_run?({ force: true })).to eql(true)
32+
end
33+
34+
it "returns true when :force is false" do
35+
expect(described_class.should_run?({ force: false })).to eql(true)
36+
end
37+
end
38+
39+
context "when not running on CI" do
40+
before do
41+
expect(Fastlane::Helper).to receive(:is_ci?).and_return(false)
42+
end
43+
44+
it "returns false when :force is not set" do
45+
expect(described_class.should_run?({ force: false })).to eql(false)
46+
end
47+
48+
it "returns true when :force is set" do
49+
expect(described_class.should_run?({ force: true })).to eql(true)
50+
end
51+
end
52+
end
53+
54+
describe "#setup_keychain" do
55+
context "when MATCH_KEYCHAIN_NAME is set" do
56+
it "skips the setup process" do
57+
stub_const("ENV", { "MATCH_KEYCHAIN_NAME" => "anything" })
58+
expect(Fastlane::UI).to receive(:message).with "Skipping Keychain setup as a keychain was already specified"
59+
described_class.setup_keychain
60+
end
61+
end
62+
63+
describe "Setting up the environment" do
64+
before do
65+
stub_const("ENV", {})
66+
allow(Fastlane::Actions::CreateKeychainAction).to receive(:run).and_return(nil)
67+
end
68+
69+
it "sets the MATCH_KEYCHAIN_NAME env var" do
70+
described_class.setup_keychain
71+
expect(ENV["MATCH_KEYCHAIN_NAME"]).to eql("fastlane_tmp_keychain")
72+
end
73+
74+
it "sets the MATCH_KEYCHAIN_PASSWORD env var" do
75+
described_class.setup_keychain
76+
expect(ENV["MATCH_KEYCHAIN_PASSWORD"]).to eql("")
77+
end
78+
79+
it "sets the MATCH_READONLY env var" do
80+
described_class.setup_keychain
81+
expect(ENV["MATCH_READONLY"]).to eql("true")
82+
end
83+
end
84+
end
85+
end
86+
end

0 commit comments

Comments
 (0)