Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Auto Deploy multiple environments using Github Actions

--

Deploying development and production environments to an EC2 instance using GitHub Actions self-hosted runners.

Press enter or click to view image in full size
Photo by Richy Great on Unsplash

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.
Press enter or click to view image in full size
  • You can skip the ‘validate the hash’ step.
Press enter or click to view image in full size
  • Replace the last step command with sudo ./svc.sh install and sudo ./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.
Press enter or click to view image in full size
  • Repeat the same for production.
Press enter or click to view image in full size

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 ci

We will not dive into the explanation as it is covered in the previous part.

  • For multiple environments runs-on is modified by adding labels.
  • if condition will check for the branch and deploy it to the respective runner with the help of label names.`
  • dev-deploy and prod-deploy contains 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.

--

--

Sanad
Sanad

Written by Sanad

Sharing thoughts and knowledge that might be helpful✨

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development