jenkins

作者: AEGQ | 来源:发表于2017-05-28 17:48 被阅读216次

参考:


示例:


  • Scripts not permitted to use staticMethod
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod hudson.Util getTimeSpanString long

打开: http://jenkins_server:8080/scriptApproval/
点击: approval
  • 使用仓库中私有镜像
docker.withRegistry('http://*.*.*.*:5000', '187e5968-78a4-4233-8eea-b77f9559159b') 
{
    sh 'docker pull *.*.*.*:5000/****:**'
}
  • Jenkinsfile
#!/usr/bin/env groovy

properties ([
    [$class: 'PipelineTriggersJobProperty',triggers: [[$class: 'SCMTrigger',scmpoll_spec: "* * * * *"]]],
    [$class: 'DisableConcurrentBuildsJobProperty',]
])

node ('10.*.*.*'){
    
        def commit_message=GetCommitMessage()
        currentBuild.displayName = "$commit_message"

        stage('checkout&update'){
            withCredentials([usernamePassword(credentialsId: '1dd9d6b6-c1d8-4aa6-9ba2-61041218d5fb', passwordVariable: 'password', usernameVariable: 'username')]) {
                sh 'svn co --username $username --password $password svn://****'
                sh 'svn co --username $username --password $password svn://****'
                if (fileExists('.uptime')){
                    sh 'svn up --username $username --password $password ./*'
                    sh 'svn up --username $username --password $password ./*'
                }
            } 
        }

        if (IsAffectedFilesInFolder("web") || !fileExists('.uptime'))
        {
            echo "IsFolderInaffectedFiles : affectedFiles in 'web'!"
            stage('Build'){
                docker.image('node:7.7').inside() {
                    sh 'cd web && npm run build'
                }
            }
        }

        if ( !fileExists('.uptime') ){

            stage('deploy'){
                    //等待数据库启动完成
                    sh '''{
                    COUNTER=0
                    while :
                    do
                        echo "请耐心等待30秒,以保证数据库正常启动!"
                        sleep 30
                        COUNTER=`expr $COUNTER + 1`
                        ok=`docker exec mysql_db_1 ss -tanl | grep 3306`
                        if [ -n "$ok" ];then
                           break
                        fi
                        if [ 2 -eq $COUNTER ];then
                          echo "ERROR: MYSQL 未启动!"
                          exit 1
                        fi
                    done
                    }'''
                    //更新.uptime时间戳
                    sh 'echo $(TZ=Asia/Shanghai date +%Y%m%d%H%M) > ./.uptime'
            }
        }else{

            stage('upgrade mysql'){

                    sh '''{
                        FILES=./mysql/sqls/*
                        UPTIME=`cat ./.uptime`

                        for f in ${FILES}; do
                            timestamp=$(expr $f : ".*\\([0-9]\\{12\\}\\).*")
                            if [ $timestamp -ge $UPTIME ]; then
                               docker exec mysql_db_1  mysql -uroot -pmysql57 -e "use cop;source /sqls/${f##*/};"
                            fi 
                        done
                        
                        
                        echo $(TZ=Asia/Shanghai date +%Y%m%d%H%M) > ./.uptime
                    }'''
                    CheckContainerStatus('mysql_db_1')
            }


            stage("upgrade parallel"){
                    parallel '1': { 
                        sh '''{
                            
                        }'''

                    }, '2': {
                        sh '''{
                            
                        }'''
                    }
            }
        }
}

// stage('Test'){

//  node('10.*.*.*'){
//      checkout([
//          $class: 'SubversionSCM', 
//          locations: 
//              [
//                  [credentialsId: '1dd9d6b6-c1d8-4aa6-9ba2-61041218d5fb', 
//                   depthOption: 'infinity', 
//                   ignoreExternalsOption: false, 
//                   local: '.', 
//                   remote: 'svn://***'
//                   ]
//              ], 
//          workspaceUpdater: [$class: 'UpdateUpdater']
//      ])
//      def text = bat(encoding: 'GBK', returnStdout: true, script: 'ant -buildfile build.xml')
//      echo "$text"
//      step([$class: 'Publisher', reportFilenamePattern: 'test-output\\testng-results.xml'])
//  }

// }

def CheckContainerStatus(String containers) {
    sleep 1
    def num_all="`echo ${containers}|wc -w`"
    def num_ok="`docker inspect -f {{.State.Status}} ${containers} | grep -c 'running'`"
    
    sh  """
        if [ $num_ok -ne $num_all ];then
           echo 'ERROR: ${containers} at wrong status!'
           exit 1
        fi
    """ 
}

@NonCPS
def GetCommitMessage() {
    
  def text = ""
  for (changeSetList in currentBuild.changeSets) {
    for (changeSet in changeSetList) {
        if (!"${changeSet.msg}".contains("db backup")){
            text += "${changeSet.author.fullName} ${changeSet.msg}\n"
        }
    }
  }
  return text
}

@NonCPS
def IsAffectedFilesInFolder(String path) {
  def text = ""
  for (changeSetList in currentBuild.changeSets) {
    for (changeSet in changeSetList) {
      for (file in changeSet.affectedFiles) {
        if ("${file.path}".contains("$path")){
            return true
        }
      }
    }
  }
  return false
}

相关文章

网友评论

      本文标题:jenkins

      本文链接:https://www.haomeiwen.com/subject/usultxtx.html