Skip to content

Instantly share code, notes, and snippets.

@Sorix
Last active December 23, 2023 14:26
Show Gist options
  • Save Sorix/21e61347f478ae2e83ef4d8a92d933af to your computer and use it in GitHub Desktop.
Save Sorix/21e61347f478ae2e83ef4d8a92d933af to your computer and use it in GitHub Desktop.
Example of Package.swift with environment variables support
// swift-tools-version:4.0
import PackageDescription
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
enum Enviroment: String {
static let `default`: Enviroment = .development
case development, production
static func get() -> Enviroment {
if let envPointer = getenv("ENV_TYPE") {
let env = String(cString: envPointer)
return Enviroment(rawValue: env) ?? .default
} else {
return .default
}
}
}
var dependencies: [Package.Dependency] = [
.package(url: "https://github.com/vapor/vapor.git", from: "3.1.0"),
.package(url: "https://github.com/Sorix/whirr-feedback-api-model", .branch("develop"))
]
var appTargetDependencies: [Target.Dependency] = ["Vapor", "FluentSQLite", "APIModel", "GooglePlaces"]
switch Enviroment.get() {
case .production:
dependencies.append(.package(url: "https://github.com/vapor/fluent-postgresql.git", from: "1.0.0"))
appTargetDependencies.append("FluentPostgreSQL")
case .development:
dependencies.append(.package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0"))
appTargetDependencies.append("FluentSQLite")
}
let package = Package(
name: "backend",
dependencies: dependencies,
targets: [
.target(name: "GooglePlaces", dependencies: ["Vapor"]),
.target(name: "App", dependencies: appTargetDependencies),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App"])
]
)
@karthiikmk
Copy link

where will we be setting ENV_TYPE ?

@Fab1n
Copy link

Fab1n commented Jan 25, 2023

I have the same question

@karthiikmk
Copy link

@Fab1n we can either set it in repo secrets,
If we are running local then its under xcode -> scheme -> env variables.

@Fab1n
Copy link

Fab1n commented Jan 30, 2023

@Fab1n we can either set it in repo secrets,
If we are running local then its under xcode -> scheme -> env variables.

Xcode scheme env variables didn’t work for me

@Fab1n
Copy link

Fab1n commented Jan 30, 2023

You can pass env variables directly to xcodebuild or if using fastlane you can set env vars in Fastfile. Wasn’t able to use xcode scheme env vars. Maybe someone can check again.

@tkafka
Copy link

tkafka commented Jun 13, 2023

Xcode scheme env variables are for app run, not for build time, that's why this doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment