Facebook Pixel

How do I handle environment variables in CI/CD?

For manual setup, SSH into EC2 and update .env. For automated setup, store env values as GitHub Secrets and use the envs parameter in the SSH action to write them to .env during deployment.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in Setting Up CI/CD for EC2 Deployment GitHub Actions for Node.js

Create a .github/workflows/deploy.yml file that triggers on push to main. Steps: checkout code, setup Node.js, npm install, npm test, then SSH into EC2 to git pull, npm install, and pm2 restart. Store EC2 host IP and SSH key as GitHub Secrets.

Add EC2_HOST (your EC2 public IP or domain) and EC2_SSH_KEY (the contents of your .pem key file, including BEGIN and END lines). Go to repository Settings → Secrets and variables → Actions → New repository secret.

On push to main, GitHub runs the workflow on a runner. It checks out the code, installs dependencies, runs tests, then uses appleboy/ssh-action to SSH into EC2. On EC2, it runs git pull, npm install, and pm2 restart to deploy the new code.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.