purpose

Docker + Jenkins deployment system was built locally, and code automatic deployment was realized through Github webhook.

The preparatory work

The installationdocker

MAC: docs.docker.com/docker-for-… Windows: docs.docker.com/docker-for-… liunx:

Curl -fssl get.docker.com -o get-docker.sh sudo sh get-docker.sh --mirror AliyunCopy the code

Console input docker -v, the version is displayed, the installation is successful

Docker Version 20.10.5, Build XXXXXCopy the code

The installationjenkins

Console input Docker Search Jenkins, select jenkinsci/ BlueOcean image installation, relatively stable.

Console inputdocker pull jenkinsci/blueoceanEnter after downloadingdocker imagesIf the following information is displayed, the installation is successful

Start thejenkins

On the console, enter docker run -d –name docker-Jenkins -p 8008:8080 -p 505:50000 JenkinSCI/BlueOcean to start the container. The id of the container is returned if the container is successfully started

If the browser opens local port 8008, the account will be unlocked normallyjenkinspage

Obtain the initialization password

The next step is to enter the container and get the password. Take the container I just started as an example. Console type docker exec-it docker-Jenkins bash to enter easy, and then type cat /var/jenkins_home/secrets/initialAdminPassword

Enter the password returned by the console to enter the configuration page, where you select the recommended plug-in to install. Some plug-ins may fail to be installed, and try again

Create a user

After the plug-in is installed, enter the information to create a user

Continue to click Next. At this time, you need to restart. Restart the user to log in to the newly created user

configuration

Next, configure Github and Jenkins

Generate an SSH public key in the container

Enter the Docker-Jenkins container, enter ssh-keygen -t rsa -c [email protected], and press Enter to generate the public key

☞ Add public key github

Top right profile picture -> Settings -> leftSSH and GPG keys -> New SSH key

Title Optional. Enter the copied public key in the key field

Add private key ☞ Jenkins

Note in this step that you need to create a Jenkins credential with the private key you just generated

Note: the start and tag contents of the private key should also be copied

Home -> Left System Administration -> Manage Credentials -> Add Credentials

Note: Select SSH Username with private key, description and Username optional

Create a task

Home -> New Task -> Enter the task name -> Build a free-style software project -> Confirm normal conditions to see this page

Source control go to Git -> enter the remote repository address -> select the previously added credentials -> branch specify master -> save

Note: this step will test the connection after entering the remote warehouse. If the credentials are not selected, an error message will be reported. After selecting the credentials, if the configuration is correct, the error message will disappear after a short time, indicating that the remote warehouse is successfully connected. If the error message persists, the project may fail to be built. In this case, check whether the configuration is correct. The branch information that I’m specifying here is master

Try project construction

If the configuration is correct, you can view the Finished build status as Finished: SUCCESS



At this point a manual build is implemented, followed by an automatic build

Automatic configuration

Github-webhook is the main way to complete the automated deployment. In short, Github-Webhook informs Jenkins to build. Wikipedia is cited

A Webhook in Web development is a method of adding or changing the behavior of a Web page or Web app through a common callback. These callbacks can be maintained, modified, and managed by third party users and developers who are not associated with the original development of the site or application. The term Webhook was first coined by Jeff Lindsay at the Computer Science Hook Project in 2007.

My understanding is similar to “publish and subscribe model “, Github – Webhook releases updates, Jenkins listens, receives notification to update. No matter publishing or listening, you need to identify and associate each other in some way. Therefore, you need to configure both ends. Top right -> Settings -> Left Developer Settings -> Personal Access Tokens -> Generate new Token Note Fill in the blanks, check the following two boxes and click Generate token



Note the token generated by copy, which disappears when it leaves

Configuration making

Configure the repository’s Web-hooks: Repository home -> Settings -> left webhooks -> Add webhooks

Note: The Payload URL here can only be a public domain name. This is a local Jenkins environment, so you need to use the Intranet penetration tool. I used dragonfly mapping.

Payload URL: Enter the public domain name mapped from the Jenkins container running port. + /github-webhook/ Content Type: Select Application /json. Secret: fill in the token that is just generated

configurationjenkins

First create a credential: Home -> System Administration -> Manage Credentials -> Add Credentials -> Select Secret Text



Then configure the change certificate:

Home -> System Configuration -> GitHub -> Advanced -> override Hook URL -> enter web-hook address -> select the created credentials



After the configuration is complete, the code repository is updatedjenkinsIt builds automatically

Packaging project

The project code needs to be packaged before it can be used correctly. Next, on the Jenkins side, conduct the code construction operation home page -> System Management -> Plug-in Management -> Optional plug-ins -> Select NodeJS and Publish Over SSH -> Install -> wait for the installation to be completed

  • NodeJS: code execution environment
  • Publish Over SSH: Send the package to the server

Pull code to package

Configure the global firstNodeJSPlug-in:

Home -> System Administration -> Global Tools Configuration -> NodeJS -> New



Added in the build projectNodeJSBuild environment:

Project Configuration -> Build Environment -> Select the newly configured Node environment -> Build -> Execute shell -> Install dependencies -> Package -> Compress

echo "hello world"
npm install
npm run build
cd dist
tar zcvf dist.tar.gz ./*
Copy the code

After the configuration, manually click Build and check the log to check the build status. If the operation succeeds, you can enter the docker-Jenkins container to check whether the dist. Tar. gz file exists

Send to the server

Global configurationpublish over sshThe plug-in

Publish over SSH – SSH Servers

Here I use the user password login, can also use SSH to connect to the server. After entering, you can test the connection

Project Configuration

Project Configuration -> Post-build Operations -> SSH Server -> Transfers

Source files: sent files (relative path is the current project path) Remove prefix: the prefix to be removed Remote directory: the Remote server directory (no error will be reported) Exec Command: Script operations to be performed on the server after the server is successfully sent, such as decompressing and deleting redundant filesCopy the code

Paste script code

CD Directory for sending files tar ZXVF dist.tar.gz rm -rf dist.tar.gzCopy the code

Finally try pushing code to verify automated build deployment ~

Normally, the server will receive the sent file. At this point, the local automatic build is successful