Pretty fastlane notifications

I recently started using fastlane on a daily basis and I already have some nice things configured that I wanted to share hoping to be helpful for others.

If you're new to the fastlane tools, they are built to automate processes in your CI/CD workflow.

This post covers a scenario where fastlane runs as part of CI workflows exclusively. The goal is to run tests for every master (or PR) push for example, and we'd like to be notified (mentioned) on Slack when they're failing.

Slack

Configure your Slack in your Fastfile with setting your webhook URL to the proper environment variable.

before_all do
 ENV["SLACK_URL"] = "https://hooks.slack.com/services/[webhook-url]"
end

This is how a basic test lane looks like:

  desc "Run tests"
  lane :test do |options|
    scan(
      scheme: "MyAppScheme",
      device: options[:device]
    )
  end

If you'd like to automate running test with this lane, add fastlane test "iPhone6" for example to a script, or job in your CI server's build flow.

The next step is to create notifications with Slack when the tests are failing. scan sends Slack notifications by default.

My problem was that it only returned with the number of failing tests/all tests, and I wanted to add a link to the generated xcpretty report so we could press on it and check which test cases failed exactly and why.

I've checked scan's implementation and what I came up with first was adding the output directory's URL of the scan action to the Slack message.

scan(
  slack_channel: "ios-ci",
  slack_message: "Some of our tests failed, here's the report: #{ENV["SCAN_OUTPUT_DIRECTORY"]}",
  slack_only_on_failure: true
)

Awesome! This way I was able to receive the location of the generated html report file on our CI machine, which is fine, but not really useful from remote. So it means that you have to expose this path to your environment and save the file as a build artifact in a later step of the build flow.

With Bitrise it's just adding an artifact deploy step with setting the desired path. Set the this step should run if a previous step fails switch on, to make sure you're going to export your reports even if the tests are failing.

Mentions

My teammate had this idea to get our git author emails from scan's output and map it to our Slack usernames. This way it will mention the person in the channel who pushed the commit that breaks our tests, so they can be notified right away. To achieve this, I've added a function to our Fastfile that maps our emails to our usernames.

def slackifyEmail(email)
  email.to_s.sub! "[email protected]", "@vasarhelyia",
  ...
  return email
end

If my terrible Ruby hurts your eyes, let me know how to make it more pretty. 💅🏾

I applied the mapping on the git_author_email field of slack.

error do |lane, exception|
    slack(
      message: exception.to_s,
      success: false,
      username: "CI bot",
      channel: slackifyEmail(Actions.git_author_email.to_s),
      default_payloads: [:lane, :test_result, :git_branch, :last_git_commit]
    ) if is_ci? && ENV['SLACK_URL']
  end

Now it'll send direct messages in the name of the CI bot whenever some of the tests failed after a push event.

What I wanted to do in the first place was to send these notifications to a channel where everyone's present from the team. There's a link_names flag on the Slack API that turns simple texts like @vasarhelyia to mentions. This way everyone could see the failure, but the committer would even receive a mention in the given channel. I couldn't find a way to set that flag through fastlane, let me know if you're aware of the trick!

Otherwise I'm gonna send a PR with the possibility of setting link_names.

That's it for now, I'll follow up if there's any update. Ping me for questions about our usage, and find fastlane on Twitter for more info about fastlane in general.