How Can Do Roll Back a Job in Jenkins
Take the golang build project as an example.
New task
- Enter a task name
- Choice: build a free style software project
- Optional: you can also copy created tasks
- Confirm submission
to configure
Edit the task you just created again
describe
To describe your project, you can write the following information.
- entry name
- Deployed IP address
- Deploy remote directory
- Log path
- Configuration directory and file name
- Start the service
- Out of Service
- Restart service
- wait
Job Notifications
Parametric construction process
Construction selection: parametric construction process
- Git parameters
- Option parameters
- Configure the build and rollback parameters. Optional for build
- Character parameters
- Set and fill in the build ID number.
Source code management
Git is generally used, and variables are used in branches to build${GIT_COMMIT}
, that is, the GIT parameter name used in the parameterized construction process
Fill in a valid git address. And use the authorized user account to ensure that you can access the warehouse code.
structure
Two options are used:
- Execute shell
- Send files or execute commands over SSH
Building shell of the project
- Defining variables
- Delete or create a directory
- Judge whether the user is "build" or "roll back" for different operations
Source code:
set -x #Definition name program_name=go_test #Project compression name program_filename=${program_name}.tar.gz #Project path target_path="${WORKSPACE}/target/" #Backup path back_path="${WORKSPACE}/bak/" if [ -f "${back_path}${program_filename}" ];then rm -f ${back_path}${program_filename} fi ls -l $back_path #The path of the current build ID back_path_num="${back_path}${BUILD_NUMBER}" #You must judge whether it exists before deleting it if [ -f "${target_path}${program_filename}" ]; then rm -rf "${target_path}${program_filename}" echo "${target_ path}${program_ File name} deleted successfully" fi #Backup path不存在则创建 if [ ! -d ${back_path_num} ]; then mkdir -p $back_path_num echo "$back_ path_ Num created successfully" fi #Project path不存在则创建 if [ ! -d ${target_path} ]; then mkdir -p $target_path echo "$target_ Path created successfully" fi #Configure go environment set CGO_ENABLED=0 set GOARCH=amd64 set GOOS=linux export GO111MODULE=on export GOPROXY=https://goproxy.cn,direct case $status in deploy) Echo "start building project..." #Generate executable file /usr/local/go/bin/go build -mod=vendor -tags=jsoniter -o "${program_name}" . #Compression packaging tar -zcf ${program_filename} --exclude=docs ${program_name} #Copy the compressed program to the backup folder cp -f ${program_filename} ${back_path_num} #Copy the compressed program to the target file cp -f ${program_filename} ${target_path} #Delete the compressed package and binary file rm -f ${program_filename} rm -f ${program_name} Echo "build complete" ;; rollback) Echo "version rollback. Rollback to $version" cd "${back_path}${version}" #Copy the historical version of the file to the project directory cp -f * "${target_path}" #The rolled back version is also copied to the folder of the current version to facilitate the next roll back. cp -f * "${back_path_num}" ;; esac
Copy to remote machine shell
Copy the Jenkins packaged file to the remote machine.
Source code:
set -x #Project name app_name=go_test.tar.gz #Project path app_path=/data/tmp/ #Copy target directory origin_path=/root/target/ #Source file origin_filename=${origin_path}${app_name} #Unzip file sudo tar -zxvf ${origin_filename} -C ${app_path} #After copying, delete the source file if [ -f "${origin_filename}" ];then rm -f ${origin_filename} echo "${origin_filename} delete success" fi #Write the code for your startup program. echo "completed"
Maintain backup shell
The backup in Jenkins cannot be used indefinitely, otherwise the disk cannot be used. The backup directory must be maintained.
We can set up to keep 10 versions of data.
Source code:
#Project backup cannot be unlimited. Historical data of 10 versions are kept. reserved_ Num = 10 # number of reserved files file_dir=${WORKSPACE}/bak/ date=$(date "+%Y%m%d-%H%M%S") cd $file_ Enter the backup directory file_ Num = $(LS - L | grep '^ D' | WC - L) # how many folders are there, that is, how many backups while(( $file_num > $reserved_num )) do old_ File = $(LS - RT | head - 1) # get the oldest backup folder echo $date "Delete File:"$old_file rm -rf "${file_dir}$old_file" let "file_num--" done ls -l $file_dir
Publish operation
structure
- Select: build with parameters
- Select branch
- Select status
RollBACK
- Select: build with parameters
- Select status: rollback
- Set version number
summary
Jenkins ensures the continuous integration and construction of the project, accelerates the project construction process, and ensures human interference.
Version rollback is an essential function for online business. In case of online exception, the version needs to be rolled back immediately. There is no room for any delay.
In this paper, the blog group issued a multi article and other operational tools platformOpenWriterelease
How Can Do Roll Back a Job in Jenkins
Source: https://developpaper.com/jenkins-build-and-rollback-task/
0 Response to "How Can Do Roll Back a Job in Jenkins"
Post a Comment