Integrate Fastlane to iOS project: Lesson 10

Integrate slack message notification to iOS project

Mark
3 min readSep 10, 2018

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.

  1. Create a channel in slack, named as ios_automation
  2. Go to https://api.slack.com/apps
  3. Click Create New App button to create your own app (this is not your workspace)
  4. Enter the app name (e.g. ios_bookshop_app) and select the slack workspace
  5. Click Incoming Webhook in left hand side menu
  6. Make sure the service is turned on
  7. Click Add New Webhook to Workspace
  8. Select channel ios_automation
  9. Click Copy button to copy the URL
Step 3: Enter app name and workspace
Step 5 & Step 6

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

  1. start and complete pod install
  2. start and complete the archive
  3. start and complete the Crashlytics and Hockey App update
  4. 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
end
def check_project_pod
slack_message(message: "Start Check Pod")
cocoapods
slack_message(message: "Finish Check Pod")
end
def build_and_complie
slack_message(message: "Start Build")
match()
gym()
slack_message(message: "Complete Build")
end
def upload_to_crashlytics
slack_message(message: "Start to upload to Crashlytics")
crashlytics()
upload_symbols_to_crashlytics()
slack_message(message: "Finish to upload to Crashlytics")
end
def upload_to_hockey
slack_message(message: "Start to upload to Hockey")
hockey()
slack_message(message: "Finish to upload to Hockey")
end
def 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.

--

--