运行 一组 linux 在 Jenkins 中使用 Jenkinsfile 的命令

Run a set of linux commands using Jenkinsfile in Jenkins

我必须创建一个 jenkins 作业来自动执行某些任务,这些任务将执行某些操作,例如更新 public 站点、将 public 版本更改为最新的 public 版本、更新软件public 站点和重新启动服务器这些包括某些操作,例如将文件复制到 tmp 文件夹、登录到本地服务器、转到文件夹并解压缩文件等。 我创建了 jenkinsfile 如下:

pipeline {
    options {
        skipDefaultCheckout()
        timestamps()
    }
    parameters {
        string(name: 'filename', defaultValue: 'abc', description: 'Enter the file name that needs to be copied')
        string(database: 'database', defaultValue: 'abc', description: 'Enter the database that needs to be created')
        choice(name: 'Run', choices: '', description: 'Data migration')
    }
    agent {
        node { label 'aws && build && linux && ubuntu' }
    }
    triggers {
        pollSCM('H/5 * * * *')
    }
    stages {
        stage('Clean & Clone') {
            steps {
                cleanWs()
                checkout scm
            }
        }
        stage('Updating the public site'){
            steps{
                sh "scp ./${filename}.zip <user>@<server name>:/tmp"
                sh "ssh <user>@<server name>"
                sh "cp ./tmp/${filename}.zip ./projects/xyz/xyz-site/"
                sh "cd ./projects/xyz/xyz-site/ "
                sh "unzip ./${filename}.zip"
                sh "cp -R ./${filename}/* ./"
        }
        stage('Changing public version to latest public release') {
            steps {
                sh "scp ./${filename}.sql.gz <user>@<server name>:/tmp"
                sh "ssh <user>@<server name>"
                sh "mysql -u root -p<PASSWORD>"
                sh "show databases;"
                sh "create database ${params.database};"
                sh "GRANT ALL PRIVILEGES ON <newdb>.* TO 'ixxyz'@'localhost' WITH GRANT OPTION;"
                sh "exit;"
                sh "zcat tmp/${filename}.sql.gz | mysql -u root -p<PASSWORD> <newdb>"
                sh "db.default.url="jdbc:mysql://localhost:3306/<newdb>""    
                sh "ps aux|grep monitor.sh|awk '{print "kill "}' |bash"             
            }
        }
        stage('Updating Software on public site') {
            steps {
                sh "scp <user>@<server>:/tmp/abc<version>_empty_h2.zip"
                sh "ssh <user>@<server name>"
                sh "su <user>"
                sh "mv tmp/<version>_empty_h2.zip ./xyz/projects/xyz"
                sh "cd xyz/projects/xyz"
                sh "cp latest/conf/local.conf <version>_empty_h2/conf/"         
            }    
        }
        stage('Restarting Server') {
            steps {
                sh "rm latest/RUNNING_PID"
                sh "bash reload.sh"
                sh "nohup bash monitor.sh &"       
            }           
        }
    }
}


有什么方法可以动态获取根文件夹中的zip文件名吗?我用了${filename}.zip ,但是好像不行

另外,有没有更好的方法来使用 jenkins 执行这些操作?非常感谢任何帮助。

您可以将每个阶段的所有步骤写在一个 shell 脚本中,并在一个阶段下执行。
关于 filename.zip 或者您可以将其作为 parameter 并将此值传递给您的阶段。或者您也可以使用 find 命令作为 shell 命令或 shell 脚本在当前目录中查找 .zip 文件。
find <dir> -iname \*.zip
find . -iname \*.zip .

示例:

pipeline {
    options {
        skipDefaultCheckout()
        timestamps()
    }
    parameters {
         
         string(name: 'filename', defaultValue: 'abc', description: 'Enter the file name that needs to be copied')
        choice(name: 'Run', choices: '', description: 'Data migration')
    }
    stage('Updating the public site'){
            steps{
                sh "scp ./${params.filename}.zip <user>@<server name>:/tmp"
                ...
            }
        }
}

要根据您的问题在特定位置执行脚本,您可以将 dir 与放置脚本的路径一起使用。
或者你也可以直接给路径 sh label: 'execute script', script:"C:\Data\build.sh"

stage('Your stage name'){
            steps{
                   script {
                           // Give path where your scripts are placed
                           dir ("C:\Data") {
                             sh label: 'execute script', script:"build.sh <Your Arguments> "
                ...
                            }
                   }
                
            }
        }