Task 3: Deploying a solution - ArzArcanum/tech504 GitHub Wiki
Deploying a solution
Jenkins projects can be configured to deploy your solution to a production environment. In this guide, we will walk through the process of setting up the environment, and configuring a Jenkins job to deploy the Sparta test app to said environment. This guide assumes you have already set a deploy key on GitHub.
Set up the production environment
Firstly, we must configure a production environment so that our Jenkins project has somewhere to deploy the app to. Create a new EC2 and use the following settings:
-
Name: tech504-sam-app-vm
-
Amazon Machine Image: Ubuntu 22.04
-
Instance type: T-3 Micro
-
Key Pair: Create and assign a new key pair for granting Jenkins access to your EC2 instance
-
Auto assign public IP: Enable
-
Use existing firewall security group: sparta-app-sg, ramon-allow-shh-from-anywhere
-
User data: A script to install dependencies
# update packages echo "UPDATING PACKAGES =======================================================" sudo apt update echo "UPGRADING PACKAGES ======================================================" sudo DEBIAN_FRONTEND=noninteractive apt -yq upgrade # install dependencies echo "INSTALLING DEPENDENCIES =================================================" sudo apt install nginx -y sudo sed -ri 's~^[^#]\s*try_files.*~proxy_pass "http://127.0.0.1:3000";~' /etc/nginx/sites-available/default nginx -s reload sudo systemctl enable nginx sudo bash -c "curl -fsSL https://deb.nodesource.com/setup_20.x | bash -" sudo apt install nodejs -y sudo npm install pm2@latest -g
Set up the Jenkins project
- Create a new freestyle project, and go to the configuration page.
Next, we must provide our new jenkins project with access to our GitHub repo and specify which branch we want to clone.
- Under Source Code Management, check Git
-
Repository URL: provide the SSH remote for your Git repo (remove .git from the end)
-
Credentials → Add → Jenkins
- Select your jenkins-to-github Credentials from the drop-down box.
-
Branches to build → Add branch → */main
-
Repository browser: (auto)
-
- Under Build Triggers, check Build after other projects are built.
- Provide the name of the project to watch.
- Check Trigger only if build is stable.
Next, we want to specify some build steps. We want to add instructions for our Jenkins worker to copy the local repo over to our production environment, then SSH into the production environment and run the app.
-
Under Build Environment, check SSH Agent
- Provide your jenkins-to-ec2 private key.
-
Under Build steps, click Add build step → Execute shell
# Copy app from Jenkins workplace to prod environment EC2 instance # --delete flag deletes any files in the prod environmnet app folder that do not exist in the Jenkins one. rsync -av --delete -e "ssh -o StrictHostKeyChecking=no" app ubuntu@[prod-env-ip]:/home/ubuntu # SSH into the prod environment. ssh -o StrictHostKeyChecking=no ubuntu@[prod-env-ip] << 'EOF' # install sparta app echo "INSTALLING APP ==========================================================" cd app/ npm install # Install pm2 echo "INSTALLING PM2 ==========================================================" sudo npm install pm2@latest -g # Stop any running pm2 processes pm2 stop all # Run App echo "STARTING APP ============================================================" pm2 start app.js --name sparta_app EOF
-
Save changes.