DEV Community

Milan Dikkumburage
Milan Dikkumburage

Posted on

How to set up CI/CD Pipeline for a node.js app from GITLAB to deploy on WP Engine

WP Engine(https://wpengine.com/) is a very popular WordPress hosting platform, it has a lot of great features like super simple backups and auto staging site creation and replication.
But still there’s no proper guide for how to implement CICD Pipeline with GITLAB on WP Engine

Create a deployment pipeline

In this example, I’ll be using Gitlab, yet the principle applies to any of the major Git hosting services. Before we create a deployment pipeline, let’s consider how we would like it to flow.

1.A developer works on a feature on their local machine.
2.This developer pushes their code to a development Git repository with a tag.
3.When the feature is ready, a merge request is created to update the dev branch.
4.we can run the pipeline after tag created.
5.I used tag method.beacuse in case if any erros in we could revert back to previous deployment

Installing Prerequisites

1 How to install nodejs (https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04)
I recommend the option 3 .Installing Node Using the Node Version Manager . beacuse you can switch node version anytime in this method.

  1. Install composer (https://getcomposer.org/download/)

Add the pipeline

To get the pipeline created, add a new file to the root of your project named .gitlab-ci.yml. Then populate with this:

Then you have to add the yml file mention variable in your CICD pipeline variable.
All mention variable are used to run deployment commands.You can modify thoese command according to your deployment.

========================================================

image: node:12.16.1

before_script:

  • apt-get update
  • apt-get install zip --assume-yes
  • mkdir -p ~/.ssh
  • echo "$WPENGINE_DEV_PVT_KEY" | tr -d '\r' > ~/.ssh/id_rsa
  • chmod 600 ~/.ssh/id_rsa
  • eval "$(ssh-agent -s)"
  • 'echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

stages:

  • QA-CODE
  • QA-DB
  • STG-CODE
  • STG-DB

QA-CODE:
stage: QA-CODE
script:
- mkdir -p ~/script
- echo -e "$QACODE" > ~/script/QACODE.sh
- echo -n "Deploying to code CMS Server..."
- ssh-add <(echo "$WPENGINE-DEV-PVT-KEY")
- zip build.zip * .[^.]* -r
- scp -P 22 build.zip QA-user@QA-wpengineserver.com:/home/qa/qa_server/qa_cms_tmp/
- scp -P 22 -o StrictHostKeyChecking=no ~/script/QACODE.sh QA-user@QA-wpengineserver.com:/home/qa/qa_server/script
- ssh -p 22 -o StrictHostKeyChecking=no QA-user@QA-wpengineserver.com "chmod +x /home/qa/qa_server/script/QACODE.sh"
- ssh -p 22 -o StrictHostKeyChecking=no QA-user@QA-wpengineserver.com "sh/home/qa/qa_server/script/QACODE.sh"

only:
- tags
when: manual

QA-DB:
stage: QA-DB
script:
- mkdir -p ~/script
- echo -e "$QADB" > ~/script/QADB.sh
- echo -n "Deploying to code CMS Server..."
- ssh-add <(echo "$WPENGINE-DEV-PVT-KEY")
- scp -P 22 -o StrictHostKeyChecking=no ~/script/QADB.sh QA-user@QA-wpengineserver.com:/home/qa/qa_server/script
- ssh -p 22 -o StrictHostKeyChecking=no QA-user@QA-wpengineserver.com "chmod +x /home/qa/qa_server/script/QADB.sh"
- ssh -p 22 -o StrictHostKeyChecking=no QA-user@QA-wpengineserver.com "sh /home/qa/qa_server/script/QADB.sh"

only:
- tags
when: manual

STG-CODE:
stage: STG-CODE
script:
- mkdir -p ~/script
- echo -e "$STGCODE" > ~/script/STGCODE.sh
- echo -n "Deploying to code CMS Server..."
- ssh-add <(echo "$WPENGINE-STG-PVT-KEY")
- zip build.zip * .[^.]* -r
- scp -P 22 build.zip STG-user@STG-wpengineserver.com:/home/stg/stg_server/qa_cms_tmp/
- scp -P 22 -o StrictHostKeyChecking=no ~/script/STGCODE.sh QA-user@QA-wpengineserver.com:/home/stg/stg_server/script
- ssh -p 22 -o StrictHostKeyChecking=no STG-user@STG-wpengineserver.com "chmod +x /home/stg/stg_server/script/STGCODE.sh"
- ssh -p 22 -o StrictHostKeyChecking=no STG-user@STG-wpengineserver.com "sh/home/stg/stg_server/script/STGCODE.sh"

only:
- tags
when: manual

QA-DB:
stage: STG-DB
script:
- mkdir -p ~/script
- echo -e "$STGDB" > ~/script/STGDB.sh
- echo -n "Deploying to code CMS Server..."
- ssh-add <(echo "$WPENGINE-STG-PVT-KEY")
- scp -P 22 -o StrictHostKeyChecking=no ~/script/STGDB.sh STG-user@STG-wpengineserver.com:/home/stg/stg_server/script
- ssh -p 22 -o StrictHostKeyChecking=no STG-user@STG-wpengineserver.com "chmod +x /home/stg/stg_server/script/STGDB.sh"
- ssh -p 22 -o StrictHostKeyChecking=no STG-user@STG-wpengineserver.com "sh /home/stg/stg_server/script/STGDB.sh"

only:
- tags
when: manual

========================================================

GitHub URL https://github.com/milan-dikkumburage/wpengine-cicd/blob/7c0978ec26461ca941440d07c89c12397185f1b1/mail.yml

Like, share and follow me 😍 for more content:

Script https://github.com/milan-dikkumburage/wpengine-cicd/tree/dev/script

Top comments (1)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Hey friend, nice post! πŸ‘‹

You might want to double-check your formatting in this post, it looks like some things didn't come out as you intended. Here's a formatting guide in case you need some help troubleshooting. Best of luck and thanks again for sharing this post!