Variáveis de Ambiente
Defina variáveis de ambiente diretamente no seu invowkfile. Estas ficam disponíveis para seus scripts durante a execução.
Uso Básico
{
name: "build"
env: {
vars: {
NODE_ENV: "production"
API_URL: "https://api.example.com"
DEBUG: "false"
}
}
implementations: [{
script: {content: """
echo "Building for $NODE_ENV"
echo "API: $API_URL (debug: $DEBUG)"
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
}
Sintaxe de Variáveis
Variáveis são pares chave-valor de strings:
vars: {
// Simple values
NAME: "value"
// Numbers (still strings in shell)
PORT: "3000"
TIMEOUT: "30"
// Boolean-like (strings "true"/"false")
DEBUG: "true"
VERBOSE: "false"
// Paths
CONFIG_PATH: "/etc/myapp/config.yaml"
OUTPUT_DIR: "./dist"
// URLs
API_URL: "https://api.example.com"
}
Todos os valores são strings. O shell interpreta conforme necessário.
Valores Literais e Expansão no Shell
Os valores em env.vars são injetados literalmente como strings, sem expandir referências ${VAR} nem substituições de comando $(...). Quando precisar de valores padrão, concatenação ou substituição de comandos, avalie-os dentro de script, onde o shell executa:
{
name: "info"
env: {
vars: {
PROJECT_NAME: "myapp"
DEFAULT_LOG_LEVEL: "info"
}
}
implementations: [{
script: {content: """
# Expand variables inside the script (not in env.vars)
HOME_CONFIG="${HOME}/.config/${PROJECT_NAME}"
EFFECTIVE_LOG_LEVEL="${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}"
echo "Config: $HOME_CONFIG (log: $EFFECTIVE_LOG_LEVEL)"
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
}
Níveis de Escopo
Nível Raiz
Disponível para todos os comandos:
env: {
vars: {
PROJECT_NAME: "myproject"
VERSION: "1.0.0"
}
}
cmds: [
{
name: "build"
// Gets PROJECT_NAME and VERSION
implementations: [...]
},
{
name: "deploy"
// Also gets PROJECT_NAME and VERSION
implementations: [...]
}
]
Nível de Comando
Disponível para comando específico:
{
name: "build"
env: {
vars: {
BUILD_MODE: "release"
}
}
implementations: [...]
}
Nível de Implementação
Disponível para implementação específica:
{
name: "build"
implementations: [
{
script: {content: "npm run build"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
env: {
vars: {
NODE_ENV: "production"
}
}
},
{
script: {content: "go build ./..."}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
env: {
vars: {
CGO_ENABLED: "0"
}
}
}
]
}
Nível de Plataforma
Variáveis por plataforma:
// Platform-specific env requires separate implementations
implementations: [
{
script: {content: "echo $PLATFORM_CONFIG"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}]
env: {
vars: {
PLATFORM_CONFIG: "/etc/myapp"
PLATFORM_NAME: "Linux"
}
}
},
{
script: {content: "echo $PLATFORM_CONFIG"}
runtimes: [{name: "native"}]
platforms: [{name: "macos"}]
env: {
vars: {
PLATFORM_CONFIG: "/usr/local/etc/myapp"
PLATFORM_NAME: "macOS"
}
}
},
{
script: {content: "echo %PLATFORM_CONFIG%"}
runtimes: [{name: "native"}]
platforms: [{name: "windows"}]
env: {
vars: {
PLATFORM_CONFIG: "%APPDATA%\\myapp"
PLATFORM_NAME: "Windows"
}
}
}
]
Combinado com Arquivos
Variáveis sobrescrevem valores de arquivos env:
env: {
files: [".env"] // Loaded first
vars: {
// These override .env values
OVERRIDE: "from-invowkfile"
}
}
Sobrescrita via CLI
Sobrescreva em tempo de execução:
# Single variable
invowk cmd build --ivk-env-var NODE_ENV=development
# Multiple variables
invowk cmd build --ivk-env-var NODE_ENV=dev --ivk-env-var DEBUG=true --ivk-env-var PORT=8080
Variáveis da CLI têm a maior prioridade.
Exemplos do Mundo Real
Configuração de Build
{
name: "build"
env: {
vars: {
NODE_ENV: "production"
BUILD_TARGET: "es2020"
SOURCEMAP: "false"
}
}
implementations: [{
script: {content: "npm run build"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}
Configuração de API
{
name: "start"
env: {
vars: {
API_HOST: "0.0.0.0"
API_PORT: "3000"
API_PREFIX: "/api/v1"
CORS_ORIGIN: "*"
}
}
implementations: [{
script: {content: "go run ./cmd/server"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}
Valores Dinâmicos
{
name: "release"
env: {
vars: {
// Static configuration lives in env.vars
APP_NAME: "myapp"
}
}
implementations: [{
script: {content: """
# Compute dynamic values inside the script
GIT_SHA="$(git rev-parse --short HEAD)"
GIT_BRANCH="$(git branch --show-current)"
BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
BUILD_ID="${GIT_BRANCH}-${GIT_SHA}"
echo "Building $APP_NAME ($BUILD_ID) at $BUILD_TIME"
go build -ldflags="-X main.version=$BUILD_ID" ./...
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
}
Configuração de Banco de Dados
{
name: "db migrate"
env: {
vars: {
DB_HOST: "localhost"
DB_PORT: "5432"
DB_NAME: "myapp"
DB_USER: "postgres"
}
}
implementations: [{
script: {content: """
DATABASE_URL="postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
migrate -database "$DATABASE_URL" up
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
}
Ambiente de Container
Variáveis são passadas para containers:
{
name: "build"
env: {
vars: {
TARGET_OS: "linux"
TARGET_ARCH: "amd64"
BUILD_MODE: "release"
}
}
implementations: [{
script: {content: "echo $TARGET_OS/$TARGET_ARCH in $BUILD_MODE mode"}
runtimes: [{name: "container", image: "debian:stable-slim"}]
platforms: [{name: "linux"}]
}]
}
Melhores Práticas
- Mantenha
env.varsliterais: Aplique fallbacks${VAR:-default}dentro descript, onde o shell os expande - Mantenha secrets fora: Não hardcode secrets; use arquivos env ou secrets externos
- Documente variáveis: Adicione comentários explicando cada variável
- Use nomenclatura consistente: Convenção
UPPER_SNAKE_CASE - Escopo apropriado: Raiz para compartilhado, comando para específico
Próximos Passos
- Env Files - Carregar de arquivos .env
- Precedence - Entender ordem de sobrescrita