skip to Main Content

I am new to fastlane, trying to read environment variable in fastfile.swift which has following

import Foundation

class Fastfile: LaneFile {
    .
    .
    .

    func createRCBuildLane() {
        beforeAll()
        ensureGitBranch(branch: "qa")
        let commitMessage = "RC_Build_Version_" + getVersionNumber(target: "Example App") + "_Build_" + getBuildNumber()
        addGitTag(buildNumber: .userDefined(commitMessage), force: true)
        pushGitTags(force: true)
        createPullRequest(apiToken: "*****", repo: "Example/iOS_ble_client", title: commitMessage, base: "master")
        slackMessage(withMessage: "Example App RC Release:n" + commitMessage)
    }

    .
    .
    .

    func beforeAll() {
//        updateFastlane()
    }

    func matchDevelopmentCertificateLane() {
        match(type: "development")
    }

    func matchAdHocCertificateLane() {
        match(type: "adhoc")
    }

    func matchDistributionCertificateLane() {
        match()
    }

    func slackMessage(withMessage message: String) {
        slack(
            message: .userDefined(message),
            channel: "#example-ios",
            slackUrl: "https://hooks.slack.com/services/###/###/###",
            payload: ["Version:": getVersionNumber(target: "Example App"), "Build:": getBuildNumber()],
            success: true
        )
    }
}

I want to pass apiToken via ENV and not to hardcode it. can anyone point me to right direction here ?

I tried

    func createRCBuildLane() {
        beforeAll()
        ensureGitBranch(branch: "qa")
        let commitMessage = "RC_Build_Version_" + getVersionNumber(target: "Example App") + "_Build_" + getBuildNumber()
        addGitTag(buildNumber: .userDefined(commitMessage), force: true)
        pushGitTags(force: true)
        createPullRequest(apiToken: ENV["API_TOKEN"], repo: "Example/iOS_ble_client", title: commitMessage, base: "master")
        slackMessage(withMessage: "Example App RC Release:n" + commitMessage)
    }

but getting this error Fastfile.swift:37:37: cannot find 'ENV' in scope

2

Answers


  1. check that there is a .env hidden file in the fastlane folder of the project.
    The .env is the environment variable of the configured project instead of the system environment variable. If API_TOKEN is in the .env,there is no problem with the configuration, you can see if the specified environment variable value can be obtained in the Applefile

    Login or Signup to reply.
  2. To access to environment variables in swift you should use ProcessInfo. For instance, there is a sample code in Fastfile.swift:

    class Fastfile: LaneFile {
        
        func testLane() {
            desc("Description of what the lane does")
            
            if let apiToken = ProcessInfo.processInfo.environment["API_TOKEN"] {
                NSLog("API TOKEN: (apiToken)")
            }
            else {
                NSLog("Error: No token.")
            }
        }
    }
    

    Now you can set the environment variable and run your lane:

    $ export API_TOKEN='CACD1B9D-56AA-41F0-BDE3-457DDA30A8D4'
    $ fastlane testLane 
    

    Outputs:

    ...
    [13:56:58]: $ ./FastlaneRunner lane testLane swiftServerPort 2000 > /dev/null
    [13:56:59]: ▸ 2022-05-31 13:56:59.494 FastlaneRunner[34213:828339] API TOKEN: CACD1B9D-56AA-41F0-BDE3-457DDA30A8D4
    [13:56:59]: fastlane.tools finished successfully 🎉
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search