0

the idea of this jenkins job is that it will receive a file as parameter and then this file must be push to github

This is the Jenkins Pipeline a have at the momento

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
            }
        }
  
    }
}

and is given me this error

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

I already look this questions

Git push using jenkins credentials from declarative pipeline Is it possible to Git merge / push using Jenkins pipeline

What I'm expecting from the job is to upload the file successfully to github

Right now it fail at the beginning

2 Answers2

0

I think something like this should do the work

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
                """
            }
        }
  
    }
}
0

I found a solution, first I download the file parameter plugin and made some changes in the stages. This is the final result

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
            '''        
           }
        }
     }
   }
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 03 '23 at 17:16