Schadenfreude
Schadenfreude

Reputation: 1910

Can't run "fastlane" command via Jenkinsfile

I wrote a Gradle task which executes the fastlane supply command via the command line. The task looks like this:

task uploadToPlayStore(type: Exec) {
    group = "upload"
    commandLine "fastlane", "supply",
            "--aab", "$project.rootDir/app/build/outputs/bundle/upload/${archiveFile}.aab",
            "--mapping", "$project.rootDir/app/build/outputs/mapping/upload/mapping.txt",
            "--skip_upload_apk", "true",
            "--skip_upload_metadata", "true",
            "--skip_upload_images", "true",
            "--skip_upload_screenshots", "true",
            "--track", "internal",
            "--package_name", "$appPackage",
            "--json_key", "~/Documents/key.json"
}

This task runs successfully from the terminal on the Jenkins slave I'm running it on, and via Android Studio as a Gradle task and via its terminal by ./gradlew uploadToPlayStore. At first it wasn't running properly via Android Studio's terminal until I did a Invalidate Cache and Restart of Android Studio. It was giving this error:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:uploadToPlayStore'.
...............
Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'fastlane''
...............
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'fastlane'
...............
Caused by: java.io.IOException: Cannot run program "fastlane" (in directory "/Users/..."): error=2, No such file or directory
...............
Caused by: java.io.IOException: error=2, No such file or directory

Currently when I try to run my Jenkins Multibranch Pipeline I get the same error I previously got in Android Studio. From what I've found so far, it appears that Jenkins can't find the fastlane command for some reason. I tried setting an Environment Variable for the build Node where I'm building my projects, but I still get the same error.

This is the stage in the Jenkinsfile where I run my Fastlane task:

stage('Upload To Google Play Store') {
            steps {
                script {
                    if (env.BRANCH_NAME.startsWith("release")) {
                        sh './gradlew uploadToPlayStore --stacktrace'
                    } else {
                        echo 'Skipping stage since we\'re not on a release branch...'
                    }
                }
            }
        }

Update (1): When running the command as "~/.rvm/rubies/ruby-2.4.2/bin/fastlane", "supply", ... with the --debug flag active I get the following stacktrace:

22:54:51.135 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTING
22:54:51.137 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Waiting until process started: command '/Users/bild_nachine/.rvm/rubies/ruby-2.4.2/bin/fastlane'.
22:54:51.147 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTED
22:54:51.147 [INFO] [org.gradle.process.internal.DefaultExecHandle] Successfully started process 'command '/Users/bild_nachine/.rvm/rubies/ruby-2.4.2/bin/fastlane''
22:54:51.148 [DEBUG] [org.gradle.process.internal.ExecHandleRunner] waiting until streams are handled...
22:54:51.150 [ERROR] [system.err] env: ruby_executable_hooks: No such file or directory
22:54:51.152 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: FAILED
22:54:51.152 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Process 'command '/Users/bild_nachine/.rvm/rubies/ruby-2.4.2/bin/fastlane'' finished with exit value 127 (state: FAILED)

Update (2): After about a day of debugging I found out that when I run the gem env command via the Script Console of the Jenkins node I get a different RUBYGEMS version, gem paths and shell paths. I'm currently trying to make the Jenkins node use the Ruby Gems installed on the machine and not its own (or something in those lines).

Update (3): After about 3 days of debugging I finally managed to figure out exactly what's wrong. The problem is that when running a Jenkins pipeline job, you can't really set the environment variables via the standard web interface and plugins (like EnvInject). Also Mac OS has a built-in ruby environment, which is used by the Jenkins node by default, on which I couldn't install the Fastlane Gem.

Upvotes: 3

Views: 3514

Answers (2)

Schadenfreude
Schadenfreude

Reputation: 1910

The solution was as follows:

  1. Install RVM or RBEnv on your Jenkins slave
  2. Install Fastlane
  3. Add the proper environment variables to your Jenkinsfile

Upvotes: 2

chedabob
chedabob

Reputation: 5881

You usually need to set up the PATH manually, as the Jenkins slave does not pick up the full path.

It can be done through the Jenkins dashboard on the slave's configuration page (Manage Jenkins -> Manage Nodes -> -> Configure).

You can find out where fastlane has installed by running which fastlane on the slave.

Upvotes: 0

Related Questions