Skip to content

Instantly share code, notes, and snippets.

@kenanhancer
Last active January 12, 2022 20:16
Show Gist options
  • Save kenanhancer/61fbbcf4828064e7f0cef5946328c145 to your computer and use it in GitHub Desktop.
Save kenanhancer/61fbbcf4828064e7f0cef5946328c145 to your computer and use it in GitHub Desktop.
Jenkinsfile which publish npm package and push to git
import org.eclipse.jgit.transport.URIish
def repositoryPath = "kenan.visualstudio.com/DefaultCollection/TestProject/_git/IconSet"
def url = "https://$repositoryPath"
def branch = "master"
def credentialsId = '44995f55-2f49-4cad-905d-b06c32b5e5bd'
node {
stage('Initialize'){
env.NODEJS_HOME = "${tool 'nodejs'}"
// on linux / mac
env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
}
stage('Clone repository') {
/* Let's make sure we have the repository cloned to our workspace */
//checkout scm
git branch: branch, credentialsId: credentialsId, url: url
/*
try {
sh "git checkout -b temp-branch"
} catch(Exception ex) {
sh "git branch -D temp-branch"
sh "git checkout -b temp-branch"
}
*/
//sh "git checkout $branch"
//sh "git merge temp-branch"
//sh "git branch -d temp-branch"
}
stage('Publish Npm Package'){
publishNpmPackage()
}
stage('Push to Git'){
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: credentialsId, usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]) {
String encoded_gitPassword = java.net.URLEncoder.encode(env.GIT_PASSWORD, "UTF-8")
String gitUserName = java.net.URLEncoder.encode(env.GIT_USERNAME, "UTF-8")
sh("git push https://${gitUserName}:${encoded_gitPassword}@$repositoryPath")
}
}
}
def publishNpmPackage(){
sh "echo \"//npm.dev.btek/:username=kenanhancer\" >> ~/.npmrc"
sh "echo \"//npm.dev.btek/:_password=UGxhY43455TUwIQ==\" > ~/.npmrc"
sh "echo \"//npm.dev.btek/:email=kenanhancer@hotmail.com\" >> ~/.npmrc"
sh "echo \"//npm.dev.btek/:always-auth=true\" >> ~/.npmrc"
sh "npm set registry http://npm.dev.kenan"
sh "npm version-tag patch"
sh "npm publish"
}
@farhanjiwani
Copy link

Hey @kenanhancer,
Heads up... you have your sensitive info here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment