Jenkins Job上传参数文件到GitHub

58wvjzkj  于 2023-04-05  发布在  Jenkins
关注(0)|答案(2)|浏览(212)

这个jenkins作业想法是,它将接收一个文件作为参数,然后必须将该文件推送到github
这是Jenkins管道,现在就有了

pipeline {
    agent {
         label 'linux'
        }
    parameters {
         file(
             name:'file.yml', 
             description:'YAML file'
            )
    }
        
    stages {
        stage('github clone'){
            steps {
         git url: 'https://token@github.com/user/repo.git', branch: 'branch_name'
            }
        }
        stage('push'){
            steps {
                    sh 'git add folder/folder/${file.yml}'
                    sh 'git commit -m "upload new file" '
                    sh 'git push origin branch_name
            }
        }
  
    }
}

并给了我这个错误

Started by user me
[Pipeline] Start of Pipeline
[Pipeline] node
Running on serverName in /home/jenkins/workspace/
[Pipeline] {
[Pipeline] stage
[Pipeline] { (github clone)
[Pipeline] git
The recommended git tool is: NONE
No credentials specified
Fetching changes from the remote Git repository
Checking out Revision 123456 (refs/remotes/origin/branch_name)
Commit message: ""
 > git rev-parse --resolve-git-dir /home/jenkins/workspace/branch_name/.git # timeout=10
 > git config remote.origin.url https://token@github.com/user/repo.git # timeout=10
Fetching upstream changes from https://token@github.com/user/repo.git
 > git --version # timeout=10
 > git --version # 'git version 2.25.1'
 > git fetch --tags --force --progress -- https://token@github.com/user/repo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/branch_name^{commit} # timeout=10
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 123456 # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D branch_name # timeout=10
 > git checkout -b branch_name 123456 # timeout=10
 > git rev-list --no-walk 123456 1 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (push)
[Pipeline] sh

我已经看过这个问题了
Git push using jenkins credentials from declarative pipeline是否可以使用Jenkins管道进行Git合并/推送
我对这份工作的期望是将文件成功上传到github
现在刚开始就失败了

vfhzx4xs

vfhzx4xs1#

我想这样的东西应该可以

pipeline {
    agent {
         label 'linux'
        }
    parameters {
         file(
             name:'file.yml', 
             description:'YAML file'
            )
    }
        
    stages {
        stage('github clone'){
            steps {
                sh """
                    git clone --branch branch_name https://token@github.com/user/repo.git
                """
            }
        }
        stage('push'){
            steps {
                sh """
                    git add folder/folder/${file.yml}
                    git commit -m "upload new file"
                    git push https://token@github.com/user/repo.git HEAD:branch_name
                """
            }
        }
  
    }
}
v1l68za4

v1l68za42#

我找到了一个解决方案,首先我下载了文件参数插件,并在各个阶段做了一些修改。这是最终的结果

pipeline {
agent {
     label 'linux'
    }
parameters {
    base64File 'File'
}
 stages {
    stage('github clone'){
        steps {
             git url: 'https://token@github.com/user/repo.git', branch: 'branch_name'
        }
    }
    stage('push yaml to github'){
        steps {
            withFileParameter('File') {
             sh '''
             cd /home/jenkins/workspace/branch_name/folder/folder
             cat ${File}
             cp ${File} new_file.yml 
             git add .
             git commit -m "upload new file"
             git push origin branch_name
            '''        
           }
        }
     }
   }
}

相关问题