How Can Do Roll Back a Job in Jenkins

Take the golang build project as an example.

cicd

New task

  1. Enter a task name
  2. Choice: build a free style software project
  3. Optional: you can also copy created tasks
  4. Confirm submission

image-20210203153829359

image-20210203153954925

to configure

Edit the task you just created again

image-20210203154135859

describe

To describe your project, you can write the following information.

  1. entry name
  2. Deployed IP address
  3. Deploy remote directory
  4. Log path
  5. Configuration directory and file name
  6. Start the service
  7. Out of Service
  8. Restart service
  9. wait

image-20210203171056828

Job Notifications

Parametric construction process

Construction selection: parametric construction process

  1. Git parameters
    1. image-20210203171232726
  2. Option parameters
    1. Configure the build and rollback parameters. Optional for build
    2. image-20210203171306056
  3. Character parameters
    1. Set and fill in the build ID number.
    2. image-20210203171413932
    3. image-20210203171503041

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.

image-20210203171635027

structure

Two options are used:

  1. Execute shell
  2. Send files or execute commands over SSH

image-20210203171832703

Building shell of the project

  1. Defining variables
  2. Delete or create a directory
  3. Judge whether the user is "build" or "roll back" for different operations

image-20210203172045718

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.

image-20210203172124561

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.

image-20210203172414790

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

  1. Select: build with parameters
  2. Select branch
  3. Select status

image-20210203172706624

RollBACK

  1. Select: build with parameters
  2. Select status: rollback
  3. Set version number

image-20210203172929159

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel