Jenkins build and rollback tasks

Take the golang build project as an example.

New task

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

to configure

Edit the task you just created again


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 service
  7. Out of Service
  8. Restart service
  9. wait

Job Notifications

Parametric construction process

Construction and selection: parametric construction process

  1. GIT parameters
  2. Option parameters
    1. Configure build and rollback parameters. For selection during construction
  3. Character parameters
    1. Set and fill in the construction ID number.

Source code management

Git is generally used. Specify the branch use variable ${GIT_COMMIT} in branches to build, that is, the name of the GIT parameter used in the step of parameterized construction

Fill in a valid GIT address. And use the authorized user account to ensure access to the warehouse code.


The following two options are used:

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

Build the SHELL of the project

  1. Define variables
  2. Create or delete directory
  3. Judge whether the user is "build" or "rollback" for differential operations

Source code:

set -x
# Definition name
# Project compression name
# Project path 

# Backup path

if [ -f "${back_path}${program_filename}" ];then
	rm -f ${back_path}${program_filename}

ls -l $back_path

# The path of the current build ID

# Before deleting, you must judge whether it exists

if [ -f "${target_path}${program_filename}" ]; then 
	rm -rf "${target_path}${program_filename}"
    echo "${target_path}${program_filename} Deleted successfully"

# Create if backup path does not exist
if [ ! -d ${back_path_num} ]; then 
	mkdir -p $back_path_num
    echo "$back_path_num Created successfully"

# Create if project path does not exist
if [ ! -d ${target_path} ]; then 
	mkdir -p $target_path
    echo "$target_path Created successfully"

# Configure Go environment
set GOARCH=amd64
set GOOS=linux
export GO111MODULE=on
export GOPROXY=,direct

case $status in 
     	echo "Start building project..."
        # Generate executable
        /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 files
        rm -f ${program_filename}
        rm -f ${program_name}
        echo "Build complete"
    	echo "Version rollback.Rollback to $version edition"
        cd "${back_path}${version}"       
        # Copy the historical version of the file to the project directory
    	cp -f * "${target_path}"
        # The rollback version is also copied to the folder of the current version to facilitate the next rollback.
        cp -f * "${back_path_num}"

Copy to remote machine SHELL

Copy the jenkins packaged file to the remote machine.

Source code:

set -x
# entry name
# Project path
# Copy destination directory
# source file

# 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"
# Write the code you started the program.

echo "completed"

Maintain backup SHELL

The backup in jenkins cannot be used indefinitely, otherwise the disk will be unusable. The backup directory must be maintained.

We can set to retain 10 versions of data.

Source code:

# Project backups cannot be backed up indefinitely. Keep 10 versions of historical data.
reserved_num=10  #Number of files retained
date=$(date "+%Y%m%d-%H%M%S")

cd $file_dir   #Enter the backup directory
file_num=$(ls -l | grep '^d' | wc -l)   #There are currently several folders, that is, several backups

while(( $file_num > $reserved_num ))
    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--"

ls -l $file_dir

Publish operation


  1. Select: Build with Parameters
  2. Select branch
  3. Select status


  1. Select: Build with Parameters
  2. Select status: rollback
  3. Set version number


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. Once online exceptions occur, you need to roll back the version immediately. There can be no time delay.

Tags: Go git jenkins go-proxy

Posted by shahryar on Mon, 25 Apr 2022 18:29:54 +0300