Auto Deploy multiple environments using Github Actions
Deploying development and production environments to an EC2 instance using GitHub Actions self-hosted runners.
After setting up the Pipeline for one environment it is sometimes quite confusing to add multiple environments, as there was no proper guide available to sort this out. After researching for hours and looking back and forth to the documentation, I was able to accomplish this.
In this tutorial, we’ll set up multiple environments like development, production, etc. Adding any other environments will be followed by the same steps.
If you are configuring it for the first time then you can follow this tutorial for basic setup with auto deployments using GitHub Actions.
Get Sanad’s stories in your inbox
Join Medium for free to get updates from this writer.
Adding Runners
- Add individual runners for each environment.
- GitHub Repo > Settings > Actions > Runners > New Self-Hosted Runner.
- You can skip the ‘validate the hash’ step.
- Replace the last step command with
sudo ./svc.sh installandsudo ./svc.sh start. - Repeat the same step for each runner.
Add Label
- Labels are responsible for pushing to specific environments.
- Create labels for each environment. Settings > Actions >Runners > Click on any Runners > setting icon > Add label, If not available create and add.
- Repeat the same for production.
Add the YAML file
- This file is configured for Nodejs, you can change it as per your requirement.
name: Deploy on Push
on:
push:
branches:
- dev
- main
jobs:
dev_deploy:
runs-on: [self-hosted, development]
if: github.ref == 'refs/heads/dev'
strategy:
matrix:
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run:
npm ci
prod_deploy:
runs-on: [self-hosted, production]
if: github.ref == 'refs/heads/main'
strategy:
matrix:
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run:
npm ciWe will not dive into the explanation as it is covered in the previous part.
- For multiple environments
runs-onis modified by adding labels. ifcondition will check for the branch and deploy it to the respective runner with the help of label names.`dev-deployandprod-deploycontains the deployment steps you can add your own.
By the end of this, you are successfully able to deploy multiple environments. If you have any questions let me know in the comments below.








