We just finished deploying Jenkins, how do we need to set up a build? For example, we have a project, first we need to download the project from Git to the local, and then package the sent target service, and decompress it after arriving at the target server.

Let’s implement a process like this:

First we will create a new Freestyle Project

Once in the project, we can configure the branch so that you can select the branch at deployment time

After the configuration, the deployment results are as follows

Note: If git does not have the GitBucket module installed, you can install the GitBucket Plugin in the plugins management system

If you plan the workspace address and want to configure a workspace specific for each project, click Advanced

Select the use custom namespace that corresponds to the location where the script generated the code below. This will be mentioned later

Then we started source control

It is important to note that the branch is specified using the $GIT_TAG we set earlier when we configured the parameters. In this way, the parameters selected during our rebuild will be passed in correctly.

Once configured, we can configure the build, and we can write a script here. Pass in a variable called $GIT_TAG. $GIT_TAG is git’s internal variable, which will be explained in a later article.

This script will go to the workspace and package it. The $GIT_TAG passed in can be spliched into the packaged name to distinguish which branch is the package. The advantage of this is that if you have multiple versions of the API under your project, and each version of the API forms a branch, you can update an API in this way.

For example, if you have a ThinkPHP project with multiple versions of the API, each API can be a Git branch or a repository. So when we pass in $GIT_TAG, we can publish it to different paths depending on the value.

Thinkphp ├─Application │ ├─Common │ ├─Conf │ │ ├─Controller │ │ ├─Model │ │ ├─Tag Public Tag directory │ ├─Home Home module │ ├─Admin as well as Home │ ├─ Install As well as Home │ ├─ Statics ├─Template View file directory │ ├─Public │ ├─Home Front Home View directory │ ├─Admin With Home │ ├─User with Home ├─Upload Public Upload directory │ ├─images │ │ ├ ─ avatar picture directory │ │ ├ ─ ueditor ueditor editor to upload the images directory │ │ │... │ │... ├─Api_v1.0 ├─Api_v2.0 ├─Runtime ├─ThinkPHP Framework System DirectoryCopy the code

Here is a simple compressed package script to demonstrate this process.

#! / bin/bash PROJECT_HOME = # # # environment variable project the main directory/var/lib/Jenkins/workspace/ENV = dev # # packaging environment parameters is introduced into branch version RELEASE = $1 # The import format is origin/v1.0.1 replaced with v1.0.1 PROJECT_VERSION=${RELEASE#*/} # project directory PROJECT_PATH=jenkins-test-build01 # package name PACKAGE_NAME=${PROJECT_PATH}-${PROJECT_VERSION} # DEPLOY_NAME=${PROJECT_VERSION} # Switch to the project directory CD ${PROJECT_HOME} Create [! -e target] && [! -d target] && mkdir target rm -rf target/* # Run the tar -czf target/${PACKAGE_NAME}.tar.gz ${PROJECT_PATH} echo command "Display package information: ls -lh target/${PACKAGE_NAME}.tar.gzCopy the code

Then we do the post-build operation and send the packaged project to the target machine. Here we need to use SSH Publishers. You need to install the Publish Over SSH plugin in advance. If you already have it installed, click Configure Jenkins, System Configuration.

First we need to configure an SSH KEY

I usually use a password, but I can also use a Key. The slave is authenticated by key

Then, configure the SSH Server

We can first select a name, then configure hostname, user name, login password or key which is the SSH key we configured above. Finally, we will configure a Remote Directory. What is the use of this Remote Directory

With SSH configured, we can go back to post-build operations to configure the post-build copy of the remote directory

The relative path of the workspace + configuration is the path of the compressed file packed. Target /jenkins-test-build01*.tar.gz = workspace + target/jenkins-test-build01*.tar.gz = workspace + target/jenkins-test-build01*.tar.gz = workspace + target/jenkins-test-build01*.tar.gz = workspace + target/jenkins-test-build01*.tar.gz Is/var/lib/Jenkins/workspace/target/Jenkins – test – build01 *. Tar. Gz this path, writing the script, and the path to the corresponding path.

Remove prefix: indicates the prefix of the source file, for example, now we only transfer all files in the target folder, but the target folder itself does not need to appear on the remote server, so it needs to be removed. And we’ll demonstrate that later

Remote directory: the directory of the Remote server that will be created if it does not exist.

Exec Command: A script on a remote server can be configured. For example, we deploy a vue project, in the build script, can line NPM package, and then tar command production compression package,

Remote Directory copies nginx paths and packages them with scripts.

Once the configuration is complete, we can run it for a try

Is successful. Then we go to the directory of the target server

jenkins-test-build01-main.tar.gz
[root@web-01 html]# pwd
/opt/test/html
[root@web-01 html]# 
Copy the code

/opt/test: /opt/test: /opt/test: /opt/test So, let’s try it, Remove the Remove Prefix option, run it and look at the project path again.

[root@web-01 target]# ls
jenkins-test-build01-main.tar.gz
[root@web-01 target]# pwd
/opt/test/html/target
[root@web-01 target]# 
Copy the code

You can see that there is a target path under the HTML, that is, the path that did not clear the file prefix in the Source Files.

All right, let me summarize. Today we talked about a general idea of how to create a freestyle build, and the basic features of Git configuration, working directory configuration, how to run scripts and how to deploy to remote servers. This must be the main directory problem, if the configuration is wrong, some errors are not easy to troubleshoot. For example, if there is a problem with your working directory configuration, the SSH Publishers module, cp is not the source file. At this point, it will only connect to the target server, but it will not copy successfully, but it will not report any errors. Take the picture below.

The only difference is probably SSH: Transferred 0 file(s) here, it doesn’t transfer the content. Most of the time, we find that when the configuration does not take effect, it is a path problem. If you have any questions, or suggestions, you can leave a message to me, or follow my public account Xiao Yuan and his friends, looking forward to common progress with you.