Under the current environment, GitLab is the code hosting platform chosen by most enterprises. With Jenkins’ construction support for GitLab, rapid deployment in the specified environment can be realized. This paper proposes two deployment environments (development environment and formal environment).

For Jenkins installation, refer to the previous article on Jenkins installation in Linux

The preparatory work

Plug-in to prepare

With the Jenkins environment ready for use, go to System Administration – Plug-in Management – Optional Plug-ins

Our project relies on Gitlab, so we need to install related plug-ins: search for “Gitlab” and choose to install gitLab Plugin and GitLab Hook Plugin

Our build needs to branch the Git repository and specify the branch with the branch Parameter: search for “git Parameter” and install the plug-in

After the installation is complete, restart Jenkins for the plug-in to take effect

Node Tool Configuration

The front-end framework based on the current trend is inseparable from the Node environment and NPM package manager, so we need to configure the Node environment for Jenkins in advance and enter the system management-Global tool configuration

Click “Add nodeJs” to specify a version of nodeJs and provide an alias (whatever name you want to recognize), then click “Save” to exit. It is also possible to add multiple versions of Node environments here, specifying which node environment is currently running the build during the build process. We use v15.4.0, the latest stable Node release at press time, as an example.

Create a project /job

Create a project

Enter the project name, select Free Style (item 1), and enter the next detailed configuration form

The job configuration

Fill in the project build description

chooseParameterize the build process

Note that we are using the parameter configuration functionality provided with the Git parameter installed in preparation. Add parameter -git parameter: branch, description: branch, type: branch

Git parameter also supports many other parameters, such as flag and commit

Finally, we add a text-type parameter: remark for build remarks

Textual parameters belong to Jenkins built-in parameters, which can be further studied according to their own needs and interests. The commonly used ones include credentials parameters, text parameters, Boolean type parameters, runtime parameters, etc., which can meet the requirements of parameter injection in the construction process and provide programing construction schemes

Source code management

Here we select the Git source and enter the Repository URL, which is the remote address of our GitLab project

Note that gitLab authorization is required here, and a Jenkins credential needs to be created first in credential addition. If the credential created is global, it can be permanently used in other projects of Jenkins platform once created. You can also create credentials in the global configuration (System Configuration - Global configuration), I won’t go into details here.

Add credentials: Select global credentials, the type we use here is userName with password (meaning you provide gitlab’s userName and password, Jenkins takes it to gitlab to ask for all the required permissions), you can also select gitlab API token, This mode is more secure, tokens need to be created and managed in GitLab, interested can try. Enter the username, password, and ID to submit

Then we select the credential just added in credential add, source management is completed

${branch} = ${branch} = ${branch} = ${branch} = ${branch} = ${branch} = ${branch}

Source library browser, we can set the Jenkins local path to check out the source code, usually fill in the project root directory.

Build trigger

In terms of building triggers, depending on what form you want the build to be triggered, such as every push, through a restful API, through the Jenkins platform, through a timer, etc. In this example, we will execute the build through a shell script, leaving the triggers out for now. [OK, continue….]

Build environment

Here we need to select the Node environment, which is the version of node configured in preparation, check Provide Node & NPM bin/ Folder to PATH, and select the desired Node environment

build

The last and most important step

Add build steps, select Execute shell, and then we fill in the shell script for the build process

#! /bin/bash echo '====== out shell start ======' echo '====== play on branch: Echo = = = = = = '${branch} echo ${remark} sh/var/lib/Jenkins/workspace/my project name/Jenkins - deploy. Sh echo' = = = = = = the out shell end = 'echo' = = = = = = = = = = = start the upload files = = = = = = '# # test environment deployment path devPath = root @ my host IP: / data/application/my project name - dev' # # Production environment Deployment path masterPath=' root@@My host IP address :/data/ Application/My project name 'if [${branch} == 'origin/master'] then echo $masterPath SCP  -r ./dist/* $masterPath fi if [ ${branch} == 'origin/dev' ] then echo $devPath scp -r ./dist/* $devPath fiCopy the code

My project name/Jenkins -deploy.sh: content

#! /bin/bash echo '=====shell start====' node -v npm -v ls -al npm i pwd npm run buildCopy the code

Sh file that specifies the full build process for the project, and then releases the build to the test environment according to the git branch parameter. If it is the Master branch, publish it to production

One thing to note here: SCP needs the server password to copy files to the specified server. Since the silent shell script is executed here and there is no interactive password entry, we need to configure the password-free SSH login of Jenkins’ server on the server, and pay attention to using Jenkins user. Since Jenkins performs construction tasks and shell scripts as the host user, the specific configuration can be searched by Jenkins, and there are a lot of online users.

save

Finally, we save it, and our job is created

And then let’s verify that

Execute a build

Find our Job project on Jenkins home page and go to the details page

Click Build with Parameter (if we didn’t configure the build parameter, this will be Build)

Fill in the Branch parameter: Select the branch you want to build (the branch list here is synchronized from gitLab facts) and fill in the remark parameter

Begin to build

On the Build details page, you can actually see the build console output. Wait a moment, and if you see the Finished: SUCCESS of the Build console, you have successfully completed the build task.

If an error occurs, you can troubleshoot the problem according to the error log. You are also welcome to discuss it through the comment section or issue or other contact ways at the end of this article


More about me

  • 💻 blog
  • 🐱 making
  • 🔨 nuggets
  • 👱 about me
  • 🐒 CSDN

Wechat official account

For Moon – Welcome to our attention