preface

Want to build your own continuous integration platform for front-end projects? Follow me to build my own Jenkins continuous integration platform of multi-project, multi-branch and multi-environment from 0. The article is a little long, patience to look down believe you will have a harvest!

preparation

The preparation of the instruments

  • Centos8 One Ali cloud server
  • Xshell (Win)

Linux commands are used

  • uname
  • yum
  • mv
  • wget
  • systemctl
  • docker

Xshell logs in to the server

Enter the host IP address, click Connect, and enter the password

Verify that docker can be installed

Docker requires a CentOS kernel version later than 3.10. Run the uname -r command to check your current kernel version.

[root@CentOS~]# uname -r

4.18.0-147.5.1. El8_1. X86_64





Copy the code

Change the yum source to Ali Cloud

Backup the old source

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

Copy the code

Download the latest source

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

Copy the code

To generate cache

yum makecache

Copy the code

update

yum update

Copy the code

Install the docker

yum install -y yum-utils

Copy the code

Add the docker source

yum-config-manager \

    --add-repo \

    https://download.docker.com/linux/centos/docker-ce.repo

Copy the code

Install the docker

yum install docker-ce

Copy the code

Change the docker image source

vim /etc/docker/daemon.json

Copy the code

Join Ali Cloud source address

{

    "registry-mirrors": ["https://6kx4zyno.mirror.aliyuncs.com"]

}

Copy the code

Re-read configuration

systemctl daemon-reload

Copy the code

Restart the docker

systemctl restart docker

Copy the code

Install Jenkins

Download Jenkins Mirror

docker pull jenkins

Copy the code

Start the Jenkins

Since Jenkins is the container, you map the configuration files inside the container to the host. Set port to 9090 and map Jenkins_HOME to host /home/jenkins_home.

docker run -d --name jenkins -p 9090:8080 -v /home/jenkins_home:/var/jenkins_home jenkins

Copy the code

You can view the running container through Docker PS. (Docker ps-a displays all containers, including those not running)

[root@CentOS home]# docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                               NAMES

ec6a4da6b83f        jenkins             "/ bin/observatory - / usr/l..."About a minute ago Up About a minute 50000/ TCP, 0.0.0.0:9090->8080/ TCP Jenkins

[root@CentOS home]#

Copy the code

Docker logs Image name View startup logs

docker logs jenkins -f

Copy the code

Jenkins initialization

After the server starts, enter http://server IP address :9090/


If no, check whether the firewall port is open. If the cloud server is used, check the security group Settings

You need to enter a password to start Jenkins for the first time. You need to enter the container to obtain the password. Passwords in the/var/jenkins_home/secrets/initialAdminPassword.

Enter Jenkins container

docker exec -it jenkins /bin/bash

Copy the code

Access password cat/var/jenkins_home/secrets/initialAdminPassword

[root@CentOS jenkins_home]# docker exec -it jenkins /bin/bashroot@ec6a4da6b83f:/# cat /var/jenkins_home/secrets/initialAdminPassword68eed23ad39541949972468e4f2ce1fdroot@ec6a4da6b83f:/#



Copy the code

After entering the password, install the required plug-in, in the installation process due to network reasons will appear some plug-in installation failure, this can be ignored

Set Jenkins’ default login account and password

Failed to install the plug-in

Some error messages may appear in the upper right corner of Jenkins’ homepage, mainly indicating that some plugins needed by Jenkins are not installed, or Jenkins version is too low, the plugins cannot be used. At this time, we need to upgrade Jenkins first and do an upgrade.Select Automatic UpgradeReboot Jenkins to complete the upgrade.

docker restart jenkins

Copy the code

Update the plugin

Change the source

https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

Copy the code
  • Click Submit after replacing the source.
  • Then enter the plug-in management page to reinstall the failed plug-in.
  • Keep plug-ins up to date.Plug-ins to install:
  • Localization: Chinese (Simplified) Localization package
  • Publish Over SSH
  • DingTalk nailing notifications
  • NodeJS Plugin

Configuration Jenkins

Global tools configure Git, Node


git

Enter the container whereis git to find the Git installation path.

docker exec-it Jenkins bash // Enter the container

whereis git

 

Copy the code

git: /usr/bin/git /usr/share/man/man1/git.1.gz

node

Install the Node version based on your needs

The system configuration

Server Configuration

  • Name Name – this will be used when building
  • Hostname Server address
  • The Username Username
  • Remote Directory Indicates the Directory for uploading files. The default root Directory is /home/www. If you do not have a WWW directory, you need to create your own server password

After the Configuration is complete, click the Test Configuration button. If the Configuration is normal, Success will be displayed. Otherwise, you can adjust the Configuration parameters according to the error information.

nailing

Nails are mainly used to build notifications. Before configuration, you need to add a custom robot to the nails group. The smart Swarm Assistant needs to be enabled Test the nails after they are configured

Free style software projects

Click New Task

Git configuration prod is a variable used to specify branches belowThe name of the global tool node is used for NodeJs InstallationBuild select shell

pwd



ls



node -v



npm -v



git --version



java -version



echo 'Build version number :'${BUILD_NUMBER}



npm install -g yarn -registry=https://registry.npm.taobao.org



yarn -v



#--pure-lockfile Specifies that the server install does not generate yarn.lock to prevent server and local code conflicts

yarn install --pure-lockfile 



yarn build



pwd



ls



echo '----- the files listed above are files in the jenkin service workspace directory -------'

Copy the code

Upload the build to the server


Enter the project folder /home/www



echo 1. Enter the project folder: --'



cd  /home/www



pwd



Package compressed project files



echo '-- 2. Package compressed project files: --'



Create webpre-copy folder automatically. Mkdir -p a/b/c Create a multi-level directory



mkdir /home/www/webpre-copy



tar -zcvf  /home/www/webpre-copy/${JOB_NAME}-${BUILD_ID}.tar.gz  *



# View the compressed backup file



cd  /home/www/webpre-copy



echo '-- 3. The following is the version of the project that has been backed up:

ls

Copy the code

Click Save and you’ll see your packed file in /home/www/webpre on your host

Create a few more tasks to categorize

If you use nginx to point the root directory to /home/www, you can see this page. Go to http://xxxx/webpre

At the end

Docker + Jenkins to build a multi-point, multi-project, multi-environment simple configuration, if you are interested in understanding docker+nginx to build their own static server, please pay attention to me, there will be follow-up updates!