Let's tackle this "Bundler: failed to load command: fastlane" error in GitHub Actions head-on. This usually means your GitHub Actions runner can't find the fastlane
gem, even though it's installed in your project. We'll fix this with a clear, step-by-step process.
Understanding the Problem:
The error message indicates a mismatch between where Bundler expects fastlane
and where it actually resides on the GitHub Actions runner. This often happens because the runner has its own isolated environment, and your locally installed gems aren't accessible.
The Solution: Explicitly Specifying the Fastlane Path
The most reliable fix is to explicitly tell Bundler where to find the fastlane
command within your GitHub Actions workflow. This involves modifying your .github/workflows/*.yml
file.
Step-by-Step Guide (Plug-and-Play):
-
Locate Your Workflow File: Find the YAML file defining your GitHub Actions workflow (e.g.,
.github/workflows/main.yml
). -
Identify the Fastlane Execution: Find the step where you're executing
fastlane
. It'll look something like this:
- name: Run Fastlane
run: bundle exec fastlane <your_lane>
-
Add the
PATH
Modification: We'll add a step before thefastlane
execution to modify the system'sPATH
environment variable. This tells the system where to look for thefastlane
executable. Crucially, this path needs to point to the location wherebundle exec fastlane
actually installs thefastlane
command. This is typically within the project'svendor/bundle/
directory.
- name: Set PATH for fastlane
run: echo ":$GITHUB_WORKSPACE/vendor/bundle/ruby/2.7.0/bin" >> $GITHUB_ENV
env:
GITHUB_ENV: ${{ toJson(env) }}
- name: Run Fastlane
run: bundle exec fastlane <your_lane>
Explanation of the Changes:
-
GITHUB_WORKSPACE
: This environment variable points to the root directory of your repository on the runner. -
vendor/bundle/ruby/2.7.0/bin
: This is the standard location where Bundler installs executables. Adjust the version number (2.7.0
) if your project uses a different Ruby version. You can easily find the correct version from the output ofbundle env
from your local environment. -
>> $GITHUB_ENV
: This appends the path to thePATH
environment variable. The use ofGITHUB_ENV
is very important here. This is the only way to persistently set environment variables in a GitHub Actions workflow. Setting the variable directly in therun:
step will not persist. -
env: GITHUB_ENV: ${{ toJson(env) }}
: This ensures that the updatedPATH
variable is properly formatted and applied.
Commit and Push: Commit the changes to your workflow file and push them to your GitHub repository. GitHub Actions will automatically detect the update and re-run your workflow.
Troubleshooting:
-
Incorrect Ruby Version: If the
vendor/bundle
path is still not found, double-check the Ruby version number in the path. Runbundle env
locally to confirm your Ruby version. -
Bundler Issues: Ensure your
Gemfile
correctly includesfastlane
and that you've runbundle install
locally before committing. -
Gemfile.lock: The
Gemfile.lock
is crucial. Make sure it's included in your repository and reflects your local environment. -
Permissions: While less common, ensure the GitHub Actions runner has the necessary permissions to access the
vendor/bundle
directory.
Advanced Techniques:
-
Using a Matrix: If you support multiple Ruby versions, consider using a matrix strategy to handle different
vendor/bundle
paths dynamically. -
Custom Actions: For better maintainability, consider creating a custom GitHub Action that encapsulates the
fastlane
execution and path setup.
Example with Matrix:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ["2.7.0", "3.0.0"]
steps:
- uses: actions/checkout@v3
- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Install dependencies
run: bundle install
- name: Set PATH for fastlane
run: |
echo ":$GITHUB_WORKSPACE/vendor/bundle/ruby/${{ matrix.ruby-version }}/bin" >> $GITHUB_ENV
echo "GEM_HOME=$GITHUB_WORKSPACE/vendor/bundle/ruby/${{ matrix.ruby-version }}" >> $GITHUB_ENV
env:
GITHUB_ENV: ${{ toJson(env) }}
- name: Run Fastlane
run: bundle exec fastlane <your_lane>
Remember, consistency is key. Always run bundle install
locally before pushing changes to avoid discrepancies between your local environment and the GitHub Actions runner. With these steps, you'll get your fastlane
running smoothly in your GitHub Actions workflow.