Use jenkins+saltstack+sh to deploy projects to multiple servers

The configuration of jenkins (this is only used as an interface, use it to manage and execute the salt command, here is the project directory shop.51ekt.com as an example)

1. Use parameterized construction of the project to realize whether to release or rollback:

2. Build action to execute shell script

sls file writing of saltstack

The deploy.sls file is as follows

deploy:
  cmd.script:
    - source: salt://deploy.sh
    - args: "{{pillar['deploy']}} {{pillar['space']}} {{pillar['project']}} {{pillar['repertory']}} {{pillar['version']}}"
    - user: www
    - group: www

The above function is to execute the deploy.sh script in the resource directory set in the configuration file. The parameters are in args, and the content in sh is executed with the permissions of www user and user group www.

shell scripting

#!/bin/sh

deploy=$1
workspace=$2
#Get dynamically through jenkins
project=$3
repertory=$4
version=$5

case $deploy in
  publish)
      #init
      if [ ! -d ${workspace} ]; then
        mkdir -p $workspace
      fi
    
      #echo $project
      #initialize the project
      if [ ! -d ${project} ]; then
        cd $workspace && git clone $repertory
        composer install
      else
        cd $project && git pull
        #composer update
      be
    ;;
  rollback)
      if [ ! -d ${project} ]; then
        echo "you need to create project first"
        exit 2
      else
        cd $project
        if [ -z ${version} ]; then 
          echo  " rollback previous version " 
          git reset --hard HEAD^
         else 
          echo  " rollback specified version " 
          git reset --hard $version 
         fi  
      fi 
    ;;
  *)
  exit
    ;;
esac

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691018&siteId=291194637