The most detailed in the entire network, Jenkins cooperates with GitLab branches to automatically merge/create (ultra-fine organization)


Preface

Introduction to GitFlow Workflow

Gitflow workflow defines a strict branching model around project release. It is relatively complex, but provides a robust framework for managing large projects and is very suitable for managing the release and maintenance of large projects.

Throughout the entire development cycle, the master and develop branches have always existed. The master branch can be regarded as a stable branch, while the develop branch is a relatively stable branch.

Feature development will be carried out on the feature branch, press releases will be carried out on the release branch, and bug fixes will be carried out on the hotfix branch. This also effectively avoids the coupling and interference of different types of development work at the code level.

GitFlow workflow optimization

The results of hotfix and release must be merged into master and develop. Why? Because their modification results will continue to affect subsequent development and maintenance, they must be merged to ensure code consistency.

When an online project needs to roll back a version, or simply record an iterative version, we often tag the master branch, for example:

Function release: release_20190101_number of versions in the month
BUG fixes: hotfix_20190101_number of fixes
This article is based on GitFlow workflow and will use Jenkins to cooperate with GitLab to achieve the following automation tasks:

After the master branch code is updated, the code is automatically merged into the develop branch.
After the master branch code is updated, it is automatically tagged in the master branch submission.

Creation of Jenkins automatic task Job

Jenkins is an open source continuous integration tool written in Java. It can communicate with Git, monitor Git's merge and push events, and trigger the execution of specified tasks (jobs) of Jenkins, such as executing unit tests.

What's more: when the code changes, it can trigger packaging deployment, performance testing, interface testing, monitoring, log analysis, etc.

Any aspect of project release can be completed automatically without much manual intervention, which helps reduce repetitive processes to save time and workload.

How to create a Jenkins job

The creation process of the automatic task Jenkins Job is listed below for reference. The creation process is as follows:

First, create a task, enter a name, select "Build a free-style software project" and click OK.

Insert image description here

General: Fill in the project name and description; Label Expression is a node (container, node) configured in Jenkins admin. A Label Expression is a group of dockers. When multiple different jobs are executed at the same time, parallelism can be achieved.

Insert image description here

Source code management: fill in the project address in GitLab in the Repository URL;

Branches to build The Branch Specifier here is the branch of the code automatically pulled by Jenkins when the Jenkins Job is triggered. You can fill in a fixed specified branch, such as master, or you can write a regular expression.

Alternatively, you can fill it out ${gitlabSourceBranch}. If filled in ${gitlabSourceBranch}, it means reading the source branch of the Merge Request from git. Using this variable, you cannot manually click "Build Now" of the Job to execute the job, because this variable cannot be read and can only be triggered through the git push event.

The specific selection can be based on the application scenario.

Insert image description here

Build trigger: What is specified in the screenshot below is that this job is only triggered when a push event occurs and the target branch is master. This can be configured specifically according to your needs.

Filter branches by name: Trigger the job only when the target branch is master.

Insert image description here

Build environment & build: Checking the first item in the build environment means clearing the workspace before each build starts; our automation script can be configured in the Execute shell Command during the build.

We can put these scripts in the project root directory or in a GitLab repository to manage the scripts in a unified manner (script examples are attached at the end of the article).

Insert image description here

Insert image description here

Post-build operation (Editable Email Notification): used to configure email reminders.

Disable Extended Email Publisher: When checked, the email will not be sent;
Project Recipient List: Recipient address; multiple recipient email addresses are separated by commas;
Project Reply-To List: The address of the person who is allowed to reply; the default is $DEFAULT_REPLYTO;
Content Type: the type of email document, you can set HTML and other formats;
Default Subject: the default email title; can also be used $DEFAULT_SUBJECT;
Default Content: the default email content, you can write HTML files here and reference some variables inside Jenkins; you can also use the default content : $DEFAULT_CONTENT;
Attach Build Log: Whether the sent email contains logs.
You need to pay attention to the configuration in Triggers. It is generally configured to send an email when the job execution fails.

Insert image description here

Insert image description here

How Jenkins Job is related to Git

Find the configuration as shown below in the Settings of the GitLab project: check "Active" and specify the specified Jenkins url to be triggered when Git Push or mr is created/updated/merged. The Project name is the Job name, user name, and password configured in Jenkins. It is the jenkins account and password.

Insert image description here

Overall step-by-step review

Prepare a project project on GitLab;
install Jenkins and related GitLab plug-ins;
configure GitLab access permissions for Jenkins;
create a Job on Jenkins, corresponding to the project project in step 1;
configure Jenkins in the GitLab project;
modify the source code of the project project and submit it to On GitLab;
check whether the Jenkins build task will trigger the configured script.

Script example

The following is an example of a script that automatically tags master branch submissions after the master branch code is updated. It is for reference only:
(Tag label naming rules: release_current date_current month version_current quarter version_current year version)

#!/bin/sh
echo **********************************Start********************************
date
# 获取最近一次远程 master 提交的 commit id
sha1=`git rev-parse remotes/origin/master^{
     
     commit}`
# 获取姓名及邮箱,来配置git提交者信息
name=`git show --pretty=%an $sha1 | awk 'NR==1{print}'`
email=`git show --pretty=%ce $sha1 | awk 'NR==1{print}'`
echo '################# 当前提交人信息:'
echo $name 
echo $email 
git config --global user.name $name
git config --global user.email $email

# 获取 merge 的源分支前缀
function getOriginPrefix(){
    
    
  # 获取分支所属
  info_sha1=`git show $sha1 | grep 'Merge:' | cut -d' ' -f3`
  info_branch=`git branch -r --contains $info_sha1`
  # 判断是否 hotfix 分支
  isHotfix=`echo "${info_branch}" | grep 'origin/hotfix'`
  if [ -n "$isHotfix" ]; then 
    echo 'hotfix'
  else
    echo 'release'
  fi
}
originBra=$(getOriginPrefix)
echo '################# 获取的源分支前缀为:' $originBra

# 获取最近一次创建的标签
latestTag=`git for-each-ref --sort=-taggerdate --format "%(tag)" refs/tags | grep $originBra | head -n 1`
# 获取最近标签的年
latestYear=`echo "${latestTag}" | awk -F_ '{print substr($2,1,4)}'`
# 获取最近标签的月
latestMonth=`echo "${latestTag}" | awk -F_ '{print substr($2,5,2)}'`
# 获取最近标签的季度
latestQuarter=`echo "${latestMonth}" | awk '{print int(($0-1)/3)+1}'`

# 获取当年
currentYear=`date +%Y`
# 获取当月
currentMonth=`date +%m`
# 获取当日
currentDay=`date +%Y%m%d`
# 获取当前季度
currentQuarter=`echo $currentMonth | awk '{print int(($0-1)/3)+1}'`

# 计算当月版本号
if [ $latestMonth -eq $currentMonth ]; then 
  currentMonthVersion=`echo "${latestTag}" | awk -F_ '{print $3+1}'`
else
  currentMonthVersion='1'
fi

# 计算当季度版本号
if [ $latestQuarter -eq $currentQuarter ]; then 
  currentQuarterVersion=`echo "${latestTag}" | awk -F_ '{print $4+1}'`
else
  currentQuarterVersion='1'
fi

# 计算当年版本号
if [ $latestYear -eq $currentYear ]; then 
  currentVersion=`echo "${latestTag}" | awk -F_ '{print $5+1}'`
else
  currentVersion='1'
fi

# 获取最终标签名 
newVersion=$originBra'_'$currentDay'_'$currentMonthVersion'_'$currentQuarterVersion'_'$currentVersion

# 创建标签
git tag -a $newVersion -m '提交人: '$name
git push origin --tags
newTag=`git tag -l | grep $newVersion`
echo '################# 最近创建的标签为:' $latestTag
echo '################# 自动计算的标签为:' $newVersion
echo '################# 自动创建的标签为:' $newTag
echo **********************************End**********************************
The following is the most comprehensive software testing engineer learning knowledge architecture system diagram in 2023 that I compiled.

1. Python programming from entry to proficiency

Please add image description

2. Practical implementation of interface automation projects

Please add image description

3. Web automation project actual combat

Please add image description

4. Practical implementation of App automation project

Please add image description

5. Resumes of first-tier manufacturers

Please add image description

6. Test and develop DevOps system

Please add image description

7. Commonly used automated testing tools

Please add image description

8. JMeter performance test

Please add image description

9. Summary (little surprise at the end)

Don't let the shadow of failure obscure your confidence. Every effort is a step towards success. Persevere in your struggle and remain immovable, and you will create your own brilliance and glory. Believe in yourself, victory is not far away!

Struggle is a firm beacon that cuts through the fog, and moving forward bravely is the key to overcoming difficulties. If you keep working hard, your dreams will light up the road to the future. Believe in your own strength and work hard, and you will create unlimited possibilities!

In hardships and challenges, cultivate a tough mentality; in the face of wind and rain and pressure, sharpen a strong will. Believe in your abilities and move forward bravely, and you will surpass yourself and open a glorious chapter in life!

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/133350367