Continuous Integration and Deployment with Jenkins

At first I found it difficult to work with Jenkins because most of the articles on installing and configuring it were out of date. Therefore, I am writing about this to make it easier for someone's work and to make it so that they do not have to go through what I had to go through installing it.





So, let's begin.





First of all, what is Jenkins?

Jenkins — , , , .





, , Jenkins ( ) , . PR master ( main).





Jenkins?

  • .





  • , .





  • Jenkins , , .





, CI/CD Node Jenkins. , , :





  1. GitHub node





  2. node GitHub





  3. Heroku  





  4. Jenkins





  5. - GitHub Jenkins





  6. Jenkins





  7. GitHub Jenkins





  8. Jenkins Heroku





  • GitHub. .





  • Ubuntu. Digital Ocean, $100, GitHub. Ubuntu . , Ubuntu 20.04, sudo-, SSH .





Heroku. .





  • GitHub. .





  • Ubuntu. Digital Ocean, $100, GitHub. Ubuntu . , Ubuntu 20.04, sudo-, SSH .





  • Heroku. .





, !





1 - GitHub node

GitHub . , jenkins-test



. README



.gitignore



Node. , public.





2 - node GitHub

, :





git clone <repository_url>
      
      



repository_url .





package.json



, cd , npm init



. , CLI .





, package.json, start test :





"start": "node index.js",
"test": "jest"
      
      



express node, , :





npm install express
      
      



index.js, node, :





const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.status(200).json("Hello world");
});

module.exports = app.listen(process.env.PORT || 4000, () =>
  console.log(`Running on http://localhost:4000`)
);
      
      



npm start



http://localhost:4000/ Node, Hello world.






, , CI, , .





, , , , jest supertest :





npm install --save-dev jest supertest
      
      



__test__ ( , ). __test__ index.test.js ( ).





const request = require("supertest");
const app = require("../index");

describe("Get route", () => {
  it("page should return hello world", async (done) => {
    const res = await request(app).get("/");
    expect(res.statusCode).toEqual(200);
    expect(res.body).toEqual("Hello world");
    done();
  });
});

afterAll(async () => {
  await app.close();
});
      
      



npm test



npm run test



, , () ():





, , GitHub.





git add .
git commit -m “initial commit
git push
      
      



3 - Heroku

Heroku.





New.





Create new app ( ).





App name ( ) (Choose a region), .





(Create app).





login () Heroku Heroku CLI. Heroku CLI, .





remote :





heroku git:remote -a heroku-app-name
      
      



:





git push heroku <github-branch-name>
      
      



, , , . open app ( ) Heroku, , .





4 - Jenkins

  non-root.





ssh username@droplet_ip_address
      
      



:





sudo apt-get update
      
      



java runtime:





sudo apt-get install default-jre
sudo apt-get install default-jdk
      
      



Jenkins.





wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > \
    /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
      
      



, Jenkins :





sudo systemctl start jenkins
      
      



Jenkins, :





sudo systemctl status jenkins
      
      



active:





Jenkins 8080, ufw:





sudo ufw allow 8080
      
      



ufw :





sudo ufw status
      
      



http://ip_address:8080 Jenkins, Unlock Jenkins.





Jenkins, . Administrator password ( ).





Customize Jenkins ( Jenkins), Install suggested plugins ( ).





Create First Admin User ( -). Username ( ), Password (), Full name ( ) E-mail address ( ) , Save and Continue ( ).





IP- , .. http://ip_address:8080, Save and Finish ( ).





, Jenkins ! Start using Jenkins ( Jenkins).





5 - - GitHub Jenkins

GitHub Settings (), Webhooks. Add webhooks ( -) url Jenkins /github-webhook/ Payload URL.





application/json Content-type.





Just the push event , -.





Active Add webhook. GitHub Jenkins.





6 - Jenkins

non-root.





ssh username@droplet_ip_address
      
      



root, :





sudo su
apt install npm
      
      



root npm, Jenkins . .





su jenkins
      
      



SSH :





ssh-keygen -t rsa
      
      



Enter , Enter.





:





cat ~/.ssh/id_rsa.pub
      
      



.





non-root .





authorized_keys :





sudo vim ~/.ssh/authorized_keys
      
      



id_rsa .





, , jenkins non-root, ssh. , .





7 - GitHub Jenkins

Jenkins Manage jenkins ( jenkins), Manage plugins ( ).





Available github Github Integration plugin ( Github).





Install without restart ( ), .





8 - Jenkins Heroku





, GitHub Jenkins, .





New Item ( ), Freestyle project OK.





, , , Configure .





, general (), Github project option ( Github) Github project link ( Github) (url , .git).





Source Code Management ( ), Git Repository URL .git ( url, ).





master main , .





Add repository ( ), , Heroku.





Heroku, App settings ( ) Heroku .





Jenkins Repository URL.





, Add, Heroku.





Jenkins , .





, Kind () - Username with password ( ), Scope () - global ().





username ( ) , , - . heroku .





Heroku Api key (Api-) Password 





 () Save (). Heroku Api key, Heroku, Account Settings ( ) , Api key. Password ().





Description () , .





Add (), .





, , . , .





advanced Name, . . jenkinsTest, .





Build Triggers GitHub hook trigger for GITScm polling ( GitHub GITScm).





Build Add build step ( ) Execute shell ( ). :





#!/bin/bash

ssh non-root-username@<droplet_ip_address>
alias proj="cd node-app-name"
git pull origin main
npm install
npm test || exit 1
      
      



Add post-build action ( ), Git Publisher Push Only If Build Succeeds ( ).





Add Branch ( ), Branch to Push Name, Heroku, Target remote name ( jenkinsTest, , ).





Save ().





, Build now ( ) !





GitHub. , Heroku.






-, JavaScript-:



JavaScript Developer. Basic

JavaScript Developer. Professional

Node.js Developer

React.js Developer








All Articles