Jenkins realizes those pits in the continuous integration

Source: Internet
Author: User

Small partners in the "we have been together over the years of continuous integration ~jenkins+perland shell script" in detail in the optimized job flow, the next summary of the construction of this job encountered in the shell script and Jenkins problems.

1. Compare two files with special characters < will lead to Jenkins job failure;

Workaround: Filter special characters < use Diff–b to remove the last dislocation line, more diff use please refer to diff--help;

-------------1)

[[Email protected]]$ diff current.txt Base.txt

1,2d0

<annzan

<wadexu

52c50

<davima

---

>davima

-----------2)

[Email protected]]$ diff-b current.txt base.txt

1,2d0

<annzan

<wadexu

-------------3)

[Email protected]]$ diff-b current.txt Base.txt | grep "<" |sed ' s/^<//g '

Annzan

Wadexu

2. awk outputs as a delimiter fragment, such as Echo $PROJECT _list|awk–f "," ' {print '} '. The Project_list traversal output, need to parameterize the number of output segments, $ $id is certainly not possible;

Workaround: Echo $PROJECT _list|awk- v id= $id -F "," ' {print$id} '

I've tried this before. Cut this command to separate the string, echo $PROJECT _list|cut-f $id-D "," but there is a problem, when the delimiter is not in project_list, regardless of the ID is a few results are itself. So the cut command is not able to do what we want to traverse all of project, unless mandatory project_list must have a delimiter.

3. Invoking the next job with the Curl command greatly improves the flexibility of our job process, while also using data JSON to pass parameter values to the next job, which allows us to parameterize the entire process.

Curl-x POST--user "jadmin:71103407"-s Http://vm10686.global.test.net:8080/view/Template/job/Template_Service_ Commit_auto_build/build--data json= ' {"parameter": [{"Name": "Project_Name", "value": ' $projectName ' "},{" name ":" Pipeline_num "," Value ":" ' $BUILD _number ' "}]} '

Note: value is appended to the double quotation mark, and if it is a parameter, it needs to be enclosed in single quotation marks outside of the parameter name.

4. Here's a look at the sinkhole we've encountered, single quotes, double quotes, and then by the way, the anti-quotes and backslashes. [Turn from the internet]

1. Single quotation mark (')  
# grep Susan Phonebook
Susan Goldberg 403-212-4921
Susan Topple 212-234-2343
If we're looking for aSusangoldberg, you cannot directly usegrep Susan Goldberg Phonebookcommand,grepwill putGoldbergand thePhonebookas a file to search for 
# grep ' Susan Gold ' phonebook
Susan Goldberg 403-212-4921
whenShellwhen the first single quotation mark is encountered, it ignores any special characters that fall behind until the closing quotation mark 

2. double quotation marks ("")
double quotation marks are similar to single quotes, except that they are not so strict. Single quotes TellShellall special characters are ignored, and double quotes only require the majority, specifically, four special characters enclosed in double quotes are not ignored:$,\,`,!,that is, double quotes will explain the special meaning of the string.,and single quotes use strings directly.If you use double quotation marks to assign a string to a variable and feed it back, there is actually no difference from the direct feedback variable. If you want to query a string that contains spaces, you often use double quotation marks.  
# x=*
# echo $x
hello.sh menus.sh misc.sh Phonebook tshift.sh
# echo ' $x '
$x
# echo "$x"
* 
This example shows the difference between quotation marks, single quotes, and double quotes. In the last case, double quotes tellShellthe variable name is replaced within the quotation marks, soShellput$xReplace with *, because the file name is not replaced in double quotes, so the * is passed as the value to be displayed toEcho. further clarification is required for the first case,ShellThere is no file name substitution when assigning a value to a variable (as can be seen from the third case), the exact order in which the steps occur is as follows:ShellScan the command line toxthe value is set to an asterisk *; 
ShellScan the command line again, touch the asterisk *, and replace it with the list of files in the current directory; 
ShellStart ExecutionEchocommand to pass the file list as a parameter to theEcho.
The prioritization of this assignment is important:ShellReplace the variable first, then replace it with the file name, and finally handle the line as a parameter 

3. Anti-quote (')  
command substitution refers to theShellability to interpolate the standard output of a command anywhere on a command line. ShellThere are two ways to replace the command:Shellcommand with an inverted quote or$(...)structure, wherein,$(...)format wasPOSIXstandard support, also facilitates nesting.  
# echo the date and time is ' date '
The date and time isthree6Month06:10:35CST 2005
# echo Your Current working directory is $ (PWD)
Your Current working directory Is/home/annzan/test

4. backslash backslash-escaped (\)  
Backslashes are generally used as escape characters,or escape character, LinuxifEchoto make escape characters work,need to use- eOptions,and the escape character to use double quotation marks 
echo-e "\ n"
Another effect of a backslash,is when the backslash is used for the last character in a row,Shelluse the backslash as a continuation line, which is often used when entering long commands in a few lines.

5. There are a lot of murders caused by single double quotes, such as the following brackets, if followed by brackets, note the addition of spaces, but the result is always wrong:


The whole day to find the question of double quotation marks, the string comparison needs to put $test in double quotes, or the outside with a square bracket:


6. The difference between the use of grep and Egrep

1) grep [option] pattern filename Note: If the pattern is an expression or more than two words, it needs to be quoted. Can be single or double quotation marks, the difference is that single quotation marks cannot reference a variable and double quotation marks can.

2) egrep= GREP-E can use the basic regular expression, and can also use extended expressions. Note the difference.
To extend an expression:
+ Match one or more previous characters, at least one previous character.
? Matches 0 or more of the preceding characters.
A|b|c match A or B or C
() character group, such as: Love (able|ers) matches loveable or lovers.
(..) (..) \1\2 template Matching. \1 represents the template in the first template, \2 the second parenthesis.
X{m,n} =x\{m,n\} x has a number of characters between M and N.

Please refer to Grep–help for specific use. Egrep--Help


7. When the parameter name is an argument, the value is evaluated with eval. For example, each of the ConfigFile defines two PROJECT git_project_name, prefixed with projectname:

Proa_git_project_name=git_proa

Prob_git_project_name=git_prob

When you use PROJECT NAME as a parameter, $ ($PROJECT _name_git_project_name) is not written in this notation and is correctly evaluated as follows: Git_project_name=${project_name} "_git_ Project_Name "

git_project_name=$ (eval echo\${$GIT _project_name})

8. The hero uses the Perl language to write the packaging installation and other core functions, need to execute these scripts, the script path needs to be added to the path, otherwise the core code will not be executed:

1) Add paths to the path path

CD ${scripts_home}

chmod 755 *

Export path= $PATH: ' pwd '

2) directly modify the. bashrc file

[[email protected] ~]$ vim. BASHRC

The file is opened as follows
#. BASHRC
# Source Global Definitions
if [-F/ETC/BASHRC]; Then
. /etc/bashrc
Fi
# User specific aliases and functions
Export Path=/home/annzan/bin: $PATH
-----Add your own path, save and exit before you have: $PATH
[email protected] ~]$. . BASHRC
. Equivalent to source for modification to take effect
[email protected] bin]$ echo $PATH
/home/annzan/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin

9. The above mentioned. bashrc file, just by the way. The difference between the. BASHRC and. bash_profile Files:

/etc/profile: This file sets the environment information for each user of the system, and the file is executed when the user logs on for the first time. and collect the shell settings from the configuration file of the/ETC/PROFILE.D directory;
/ETC/BASHRC: Executes this file for each user running Bashshell. When the Bashshell is opened, the file is read;
~/.bash_profile: Each user can use this file to enter shell information dedicated to their own use, when the user logs on, the file is only executed once! By default, he sets some environment variables to execute the user's. bashrc file;
~/.BASHRC: This file contains bash information dedicated to your Bashshell, which is read when you log in and every time you open a new shell;
~/.bash_logout: Execute the file each time you exit the system (exit Bashshell);
In addition, the variables set in/etc/profile (global) can be applied to any user, while the variables set in ~/.BASHRC, etc. (local) only inherit variables from/etc/profile, they are "parent-child" relationships.

~/.bash_profile is interactive, login way into bash run
~/.BASHRC is an interactive non-login way into bash run
Usually the two settings are roughly the same, so the former usually calls the latter.

Settings to take effect: can be restarted to take effect, you can also use the command: source

10. The comparison between decimals is compared with BC, not integer, and can not be compared simply with greater than less than;

If [$ (echo "$code _coverage_integer< $SONAR _min_coverage" | BC) ] or

if (($ (echo "$code _coverage_integer< $SONAR _min_coverage" | BC) ))

Note: The following two formats are available for data comparison:

The following are the file, string, and arithmetic comparison operators:

11. Line-changing issues;

1) String Wrapping:

Mail_body= "Hi,${project_name}" "PROJECT member"$ ' \ n '"Alerts"

2) Echo writes the file wrap:

echo- e "exportconfig_time=${current_time}\n

exportproject_list=${project_list}\n

exportshared_folder=~/jenkins/common\n "> Config.txt

12. Through the Git commit hash to determine whether the code is updated, take the current commit hash and base hash comparison, there is a different, packaged installation, and put the current commithash into the base file; Similarly, take out all the current author list, In contrast to the base list, remove all authors of the code modifications.

git log--max-count=1--pretty=format: "%H" > ${current_md5_file}

Gitlog--pretty=format: "Author:%an" > ${current_author_list}

Refer to GIT log paras: https://www.kernel.org/pub/software/scm/git/docs/git-log.html

13. Take the value of a specified node from the Pom.xml file:


For example <name>testservice</name>, take Testservice value:

1) sed-n '/name/{s/.*<name>\ (. *\) <\/name>.*/\1/p;q;} ' Pom.xml

2) catpom.xml|sed-n '/name/p ' |awk-f "[<>]" ' {print $} '

If the value is a number, for example <num>12</num, we can get it by taking a number;

Cat Pom.xml | Sed-n '/num/p ' | Sed-r ' s/([^0-9]* ([0-9]*)) {1}.*/\2/'

However, when NUM displays a value of 1,000, it can only fetch 1 instead of 1,000. So the above two methods are more effective.

The JSON object is evaluated as follows:

JSON object:

Response= ' {' name ' = ' Annzan ', ' value ' = ' Test '} '

Use the following method to take a value:

function Jsonvalue () {

Key=$1

Value=$2

Awk-f "[,:}]" ' {for (i=1;i<=nf;i++) {if ($i ~/' $KEY ' \042/) {print $ (i+1)}}} ' | Tr-d ' "| Sed-n ${value}p

}

If you want to take the value Annzan in name, call the Jsonvalue method and enter the corresponding key value:

15. In a Linux system, you can use the Mount command to mount a disc image file, a removable hard disk, a USB flash drive, and a Windows network share and a UNIX NFS network share, which mainly describes the NFS file sharing in the Linux system;

1) First look at the file that needs to be mounted disk address: df/mnt/ifs10001

2) Create the appropriate folder in the system you want to mount: mkdir/mnt/ifs10001

3) Implement mount shared file: Mount-t NFS isiloncwy0001.transfer.test.net:/ifs/data/transfer/clients/mnt/ifs10001

Refer to http://www.jb51.net/os/RedHat/1109.html for details

The time was delayed when Jenkins set up the job on time, such as when I set it to 4 o'clock in the afternoon, but found no movement, and then found that every day was 4 points 15 running job. And the time for this setting is the time of the Jenkins server, not the native time. Set the format as follows:

For example, set the job to execute every Monday through Friday, 3 o'clock. Set timed tasks: H 3 * 1-5

In Jenkins, most of the jobs before the job need to export environment variables, we can not need to manually write shell script, you can set the Node property on the Jenkins Nodeconfig page, such as:

18. In addition to setting environment variables at node, we can set up global settings in the Jenkins system, such as maven_opts or account password, $ENV {USERNAME} is the calling account;

19. Since we are now using git to manage the code base, we need to checkout the code in Jenkins, first set up the GIT account and configure the key in Jenkins's managecredentials, such as:

Jenkins realizes those pits in the continuous integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.