Search code examples
gitjenkinsgithubjenkins-pipelinepipeline

Git push from jenkins pipeline


I want to push my code using jenkins pipeline, so is there any way to use git push with HTTPS from jenkins?

Can we use GitHub personal token for git push?


Solution

  • There are many different ways to achieve this goal. I use sh steps to run git commands.

    After staging and committing the changes locally, the push command will look like

    def repoUrlWithAuth = "https://<username>:<token>@github.com/<username>/<repo>.git"
    def sourceBranch = "<branch-to-push-to>"
    
    git push --repo=${repoUrlWithAuth} --set-upstream ${repoUrlWithAuth} ${sourceBranch}
    

    If the initial cloning is done using https://<username>:<token>@github.com/<username>/<repo>.git, then the credentials will remain on the git config and you don't need to include --repo=${repoUrlWithAuth}.

    If the initial cloning has the correct local-remote branch mapping, then you don't need to include --set-upstream ${repoUrlWithAuth} ${sourceBranch}

    There is another answer here that might be of help to figure this out.