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

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 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.

structure

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
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}"


# 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"
fi 


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

# Create if project path does not exist
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
        /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"
    ;;
    
    rollback)
    	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}"
    ;;
esac

Copy to remote machine SHELL

Copy the jenkins packaged file to the remote machine.

Source code:

set -x
# entry name
app_name=go_test.tar.gz
# Project path
app_path=/data/tmp/
# Copy destination 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 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
file_dir=${WORKSPACE}/bak/
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 ))
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

RollBACK

  1. Select: Build with Parameters
  2. Select status: rollback
  3. 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. 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