Article summary
A mobile application automation tool called fastlane furnishes an extensive library of actions that aids you in packaging up your app and shipping it to your users. Luckily, most of the actions you’ll need are included out of the box. But for the ones that aren’t, this tool also provides an easy way to create your own.
On my current project, I needed to run an already-existing bash script as part of our fastlane app deployments. I noticed that the fastlane configuration file, Fastfile
, was written in Ruby. So, my first thought was to wrap the path to the bash script in backticks and move on… until I found out about a better way. Here, let me show you!
Setup
First you’ll need to have fastlane already set up in your project. Then you can create a new custom fastlane action by opening a terminal, changing directories to your project, and running the following command:
bundle exec fastlane new_action
The new_action
script will prompt you to give your new action a name. We’ll call ours my_custom_bash
Note: the name must be in lowercase, words must be joined by “_” and periods excluded.
Once the script succeeds, you’ll find a new actions directory within your fastlane files. Inside the actions directory is a file with the name we specified earlier. This is the file we will need to edit.
$ tree fastlane
fastlane
├── Fastfile
├── Pluginfile
├── README.md
├── actions
│ └── my_custom_bash.rb
└── report.xml
Modification
Open my_custom_bash.rb
in your favorite code editor.
Upon opening the file you’ll see a Ruby class that inherits from Action
with a handful of class methods that we will need to implement.
The self.run(params)
class method is what will execute our custom bash script.
def self.run(params)
UI.message "Running a custom bash script"
sh "./path/to/the-custom-bash.sh"
end
We are using the fastlane sh action to actually run our custom bash script. This will run our bash script synchronously.
If the-custom-bash.sh
script exits with a non-zero exit code, fastlane will throw an error and will stop your current deployment process. So, be sure to consider your success and fail cases in your script and exit 0 appropriately.
Next, edit the self.description
class method. This should be a concise description, 80 characters or less, stating what your action does.
For the purposes of our script, we’ll clear out the self.details
, self.avaibile_options
, self.output
class methods because our action is not expecting any configuration options and will not need to export any variables.
Lastly, we’ll just need to edit self.is_supported?(platform)
. By default, the platform parameter will be one these symbols [:ios, :mac, :android]
. The method return value must be a boolean value that fastlane inspects to determine if this action should run for the given platform. Our custom bash script is platform-agnostic, so we’ll just return true regardless of what platform symbol is passed in.
Save the file!
Verification
Let’s verify that fastlane recognizes our new custom action.
$ bundle exec fastlane actions | grep my_custom_bash
| my_custom_bash | A short description with <= 80 characters of what this action | Nathan Papes |
We verified that fastlane knows about our action. Now, we can run my_custom_bash
independently with:
bundle exec fastlane run my_custom_bash
Or inside of a Fastfile
.
Fastlane has been a major time saver for our app deployments. I’m pleased with the actions that come included with the tool, but I’m even more ecstatic about the tooling provided to create your own custom actions for those one-off scenarios like running a custom bash script. It includes all the facilities to make your deployments a painless process so you can focus on more important things.