Uninote
Uninote

任务详情

自动发布任务详情链接

任务执行

jenkins上配置一个自动发布到t4测试容器的job。

jenkins容器配置sshpass

apt-get install sshpass
sed -ri 's#.*StrictHostKeyChecking.*#StrictHostKeyChecking no#' /etc/ssh/ssh_config

jenkins创建job和配置

#!groovy
pipeline {
  agent any
  environment {
    repoUrl = "http://git.uninote.com.cn:3000/cyb/mynote-front-dist.git"
  }
  triggers {
    GenericTrigger(
     genericVariables: [
      [key: 'branch', value: '$.ref'],
     ],
     token: 't4fdafdafdafdafda',
     regexpFilterText: '$branch',
     regexpFilterExpression: '^(refs/heads/test)$',
     printContributedVariables: false,
     printPostContent: false
    )
  }

  stages {
    stage('getBranch'){
      steps{
        script{

            if("${branch}" != ""){
              println "----------webhook式触发-----------"
              branchName = branch - "refs/heads"
              branchName = sh(returnStdout: true,script: "echo ${branchName}|awk -F '/' '{print \$NF}'").trim()
              println "webhook触发的分支是: " + "${branchName}"
            }
    }
    }
    }

    stage('deploy'){
        steps{
            sh '''
                echo 'deploy'
                sshpass -p 'xxxxxxxxx' ssh -p 20033 root@121.196.47.141 "cd /home/www/front;git fetch origin $branch;git reset --hard FETCH_HEAD;sleep 1;pm2 restart node"
            '''
        }
    }
  }
}

gogs钩子配置

http://116.62.117.165:18081/generic-webhook-trigger/invoke?token=t4fdafdafdafdafda

后续调整

如果git上提交的分支是本地的分支,则更新本地代码。 pipeline 最终调整:

#!groovy
pipeline {
  agent any
  environment {
    repoUrl = "http://git.uninote.com.cn:3000/cyb/mynote-front-dist.git"
  }
  triggers {
    GenericTrigger(
     genericVariables: [
      [key: 'branch', value: '$.ref'],
     ],
     token: 't4fdafdafdafdafda',
     printContributedVariables: false,
     printPostContent: false
    )
  }

  stages {
    stage('getBranchName'){
      steps{
        script{

            if("${branch}" != ""){
              println "----------webhook式触发-----------"
              branchName = branch - "refs/heads"
              branchName = sh(returnStdout: true,script: "echo ${branchName}|awk -F '/' '{print \$NF}'").trim()
              println "webhook触发的分支是: " + "${branchName}"
            }
    }
    }
    }

    stage('deploy'){
                environment {
            branchName = "$branchName"
        }
        steps{
            sh '''
                echo 'deploy'
#cat > deploy.sh <<EOF
##!/bin/bash
#branchName=\$1
#cd /home/www/front
#remoteBranch=`git branch|grep '^*'|awk -F '*' '{print \$NF}'`
#if [[ "\$remoteBranch" =~ "\$branchName"  ]];then
#git reset --hard FETCH_HEAD
#sleep 1
#pm2 restart node
#echo deploy and restart node ok.
#fi
#EOF

echo > deploy.sh
echo '#!/bin/bash'>> deploy.sh
echo 'branchName=$1'>> deploy.sh
echo 'cd /home/www/front'>> deploy.sh
echo 'remoteBranch=`git branch|grep "^*"`'>> deploy.sh
echo 'if [[ "$remoteBranch" =~ "$branchName"  ]];then'>> deploy.sh
echo 'git fetch&& git reset --hard origin/$branchName'>> deploy.sh
echo 'sleep 1'>> deploy.sh
echo 'pm2 restart node'>> deploy.sh
echo 'fi' >> deploy.sh
                sshpass -p 'xxx.' scp -P 20033  ./deploy.sh root@121.196.47.141:/tmp/
                sshpass -p 'xxx..' ssh -p 20033 root@121.196.47.141 "bash /tmp/deploy.sh \"$branchName\""
            '''
        }
    }
  }
}
对获取分支的优化remoteBranch=`git rev-parse --abbrev-ref HEAD`

README

gogs git钩子

点赞(0) 阅读(11) 举报
目录
标题