Integrate Fastlane to iOS project: Lesson 10
Last lesson, we talk about how to upload the build to TestFlight. This time, we will show you how to got a notification message from slack when the build is complete.
Create Webhooks in Slack
If we want to use Fastlane to tigger a message to your Slack group, it needs to set up a Slack App and Webhooks first.
- Create a channel in slack, named as ios_automation
- Go to https://api.slack.com/apps
- Click Create New App button to create your own app (this is not your workspace)
- Enter the app name (e.g. ios_bookshop_app) and select the slack workspace
- Click Incoming Webhook in left hand side menu
- Make sure the service is turned on
- Click Add New Webhook to Workspace
- Select channel ios_automation
- Click Copy button to copy the URL
Now, you have created your Slack Webhooks, it will help you to send the message to your slack selected channel.
Send message to Slack by lane
Open you Fastfile and add the following lane
lane :send_msg_to_slack do
slack(
slack_url: "<your_url_of_webhooks>",
channel: "#ios_automation",
success: true,
message: "Hello World",
payload: {
"π Build Date" => Time.new.to_s,
"π Environment" => "env name"
},
default_payloads: [:git_branch, :test_result, :last_git_commit_hash],
)
end
Run bundle exec fastlane ios send_msg_to_slack
and you will the following screen.
success
- means the left hand side bar color
message
- the message you send to your slack channel
payload
- custom fields for you to write down any information you want to send
default_payloads
- the default payload provided by slack action
For other value, you can run fastlane action slack
for more detail.
Integrate slack message to your project
We will send the message to slack when Fastlane start or complete the following action
- start and complete pod install
- start and complete the archive
- start and complete the Crashlytics and Hockey App update
- start and complete the TestFlight upload
In order to do that, we will do the following change in our Fastfile
and env related file.
// Fastfile
private_lane :send_msg_to_slack do |options|
slack(
slack_url: "<your_url_of_webhooks>",
channel: "#ios_automation",
success: true,
message: options[:message],
payload: {
"π Build Date" => Time.new.to_s,
"π Environment" => ENV['ENV_NAME']
},
default_payloads: [:git_branch, :test_result, :last_git_commit_hash],
)
end
lane :build_app do
check_project_pod
build_and_complie
upload_to_crashlytics
upload_to_hockey
end
lane :submit_to_testflight do
check_project_pod
build_and_complie
upload_to_testflight
enddef check_project_pod
slack_message(message: "Start Check Pod")
cocoapods
slack_message(message: "Finish Check Pod")
enddef build_and_complie
slack_message(message: "Start Build")
match()
gym()
slack_message(message: "Complete Build")
enddef upload_to_crashlytics
slack_message(message: "Start to upload to Crashlytics")
crashlytics()
upload_symbols_to_crashlytics()
slack_message(message: "Finish to upload to Crashlytics")
enddef upload_to_hockey
slack_message(message: "Start to upload to Hockey")
hockey()
slack_message(message: "Finish to upload to Hockey")
enddef upload_to_testflight
slack_message(message: "Start to upload to TestFlight")
pilot()
slack_message(message: "Finish to upload to TestFlight")
end
// .env_dev_adhoc
ENV_NAME = Dev
// .env_testing_adhoc
ENV_NAME = Testing
// .env_testing_release
ENV_NAME = Testing
We set the send_msg_to_slack
as private_lane
since we donβt want the lane is called by outside. It should only be called when the Fastlane is tiggered. The |options|
in lane means the passing parameters, we will pass the message as paramter to send_msg_to_slack
to show that.
The def
is used to show the start message, execute the action, show the complete message. You can use any emoji in your slack message like π or π.
Whatβs next?
Next lesson, we will introdcue the scan() action to run the internal test.