Ensuring Proper Environment Variables for Landing Page Development #9
Replies: 1 comment
-
During the development phase of building your EDCopo landing page with the stack you've mentioned (Svelte for frontend, Rust for backend, PostgreSQL, Docker, and GitHub Actions for CI/CD), it's essential to have a robust environment variable management strategy. Environment variables are crucial for keeping sensitive information like API keys, database credentials, and other environment-specific configurations safe and separate from the source code. Managing Environment Variables in DevelopmentTo implement a robust environment variable setup, you can follow these best practices to manage environment variables in development, staging, and production environments. 1. Defining Environment VariablesIn a typical web application, environment variables can be defined in multiple places depending on the environment:
Let’s go step-by-step through setting up environment variables for each of these areas. 2. Environment Variables in Local DevelopmentFrontend (Svelte)In Svelte, environment variables are typically used for things like API URLs, public keys, or configurations that are safe to expose in the frontend. Svelte uses Vite as its bundler, which has built-in support for environment variables.
# frontend/.env
VITE_API_URL=http://localhost:8080/api
VITE_PUBLIC_KEY=your_public_key_here
VITE_APP_NAME=EDCopo Landing Page In your Svelte components or stores, you can access these variables like this: // Example usage in a Svelte component or store
const apiUrl = import.meta.env.VITE_API_URL;
console.log('API URL:', apiUrl);
Backend (Rust)For the backend, environment variables are typically used to store sensitive data, such as database credentials, API keys, and other configurations that should not be exposed to the client.
[dependencies]
dotenv = "0.15"
# backend/.env
DATABASE_URL=postgres://username:password@localhost:5432/edcopo_db
RUST_LOG=info
SECRET_KEY=your_secret_key_here
use dotenv::dotenv;
use std::env;
fn main() {
dotenv().ok(); // Load the .env file
// Access environment variables
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
println!("Database URL: {}", db_url);
}
# .gitignore
*.env PostgreSQL (Docker)When you're using Docker for local development, you can define the database connection details and other configuration variables inside your version: '3.7'
services:
backend:
build:
context: ./backend
environment:
- DATABASE_URL=postgres://user:password@postgres:5432/edcopo_db
- RUST_LOG=info
ports:
- "8080:8080"
depends_on:
- postgres
postgres:
image: postgres:latest
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: edcopo_db
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data: Here, 3. Environment Variables in CI/CD (GitHub Actions)When using GitHub Actions for CI/CD, it's essential to securely manage environment variables. These variables could include deployment credentials, API keys, or Docker-related secrets. You can securely store environment variables in GitHub Secrets, which are encrypted and only accessible to your workflows. Steps to add environment variables to GitHub Actions:
name: CI/CD Pipeline
on:
push:
branches:
- develop
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Set up Node.js for Svelte
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Install Dependencies
run: |
cd frontend
npm install
- name: Run Frontend Tests
run: |
cd frontend
npm test
- name: Set up Rust for Backend
uses: actions/setup-rust@v1
with:
rust-version: stable
- name: Install Backend Dependencies
run: |
cd backend
cargo build --release
- name: Run Backend Tests
run: |
cd backend
cargo test
- name: Deploy to Server
run: |
ssh -o StrictHostKeyChecking=no user@your-server-ip "
cd /path/to/your/app &&
git pull origin main &&
docker-compose down &&
docker-compose up -d
"
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} In this example, we’ve securely injected the
4. Production Environment (Cloudflare and Server)For the production environment, you should configure environment variables securely on your server or cloud environment. If you're using a Ubuntu server behind Cloudflare and deploying with Docker, you can manage environment variables as follows: Using Docker Compose for Production:You can use environment variables for production via a version: '3.7'
services:
backend:
build:
context: ./backend
environment:
- DATABASE_URL=${DATABASE_URL}
- SECRET_KEY=${SECRET_KEY}
ports:
- "8080:8080"
frontend:
build:
context: ./frontend
ports:
- "80:80"
postgres:
image: postgres:latest
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data: Using Cloudflare:If you're hosting on Ubuntu behind Cloudflare, the environment variables (e.g., To set environment variables on your server: # Set environment variables on your Ubuntu server
export DATABASE_URL=postgres://user:password@localhost:5432/edcopo_db
export SECRET_KEY=your_secret_key_here You can also configure environment variables in the systemd service file for production applications or in your cloud provider’s dashboard (AWS, GCP, DigitalOcean, etc.). ConclusionManaging environment variables is critical to your app’s security, maintainability, and ease of deployment. Here’s a recap of how to set up environment variables:
CI/CD.
By adhering to these practices, you can ensure that your development, staging, and production environments are secure and easy to manage. |
Beta Was this translation helpful? Give feedback.
-
when we are in development phase , we need solid ENV variable , so suppose we are going to implement landing page with our stack which mentioned before
Beta Was this translation helpful? Give feedback.
All reactions