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
check that there is a
.env
hidden file in thefastlane
folder of the project.The
.env
is the environment variable of the configured project instead of the system environment variable. IfAPI_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 theApplefile
To access to environment variables in swift you should use
ProcessInfo
. For instance, there is a sample code inFastfile.swift
:Now you can set the environment variable and run your lane:
Outputs: