Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/geolocation / Jenkinsfile
Size: Mime:
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils

def defaultConfig = [lint: true, test: true, build: true, publish: false]

node ('jenkins') {
  properties([
    parameters([
      booleanParam(name: 'lint', defaultValue: false, description: 'Execute linting stage'),
      booleanParam(name: 'test', defaultValue: false, description: 'Execute test stage'),
      booleanParam(name: 'build', defaultValue: false, description: 'Execute build stage'),
      booleanParam(name: 'confirm', defaultValue: false, description: 'Confirm manual run\n🚨 Please review you settings carefully!')
    ])
  ])

  stage('checkout') {
    config = defaultConfig + [branch: env.BRANCH_NAME, tag: env.TAG_NAME]

    config.hash = checkout(scm).GIT_COMMIT
    config.shortHash = sh(returnStdout: true, script: "git rev-parse --short ${config.hash}").trim()
    try {
      config.lastVersion = sh(returnStdout: true, script: "git describe --tags --abbrev=0").trim()
    } catch(Exception e) { 
      config.lastVersion = readJSON(file:'./package.json').version ?: '0.0.0'
    }
    

    if (params.confirm) {
      config << params  
    } else if (config.tag) {
      config << [publish: true, version: toSemantic(config.tag)]
    } else if (config.branch == 'master') {
      config << [publish: true, version: toSemantic("${config.lastVersion}-${config.shortHash}")]
    } else if (config.branch.toLowerCase().startsWith('release')) {
      config << [publish: true, version: toSemantic("${config.lastVersion}-${config.shortHash}")]
    }

    unstable = false
    env.GIT_COMMIT = config.hash

    echo """
    =====================================================                                                
    lint       | ${!config.lint ? '❌' : '✅' }
    test       | ${!config.test ? '❌' : '✅' }
    build      | ${!config.build ? '❌' : '✅' }
    publish    | ${!config.publish ? '❌' : '✅'}
    -----------------------------------------------------
    version    : ${config.version}
    hash       : ${config.hash}
    =====================================================
    """
  }

  stage ('prepare') {
    if (!(config.lint || config.test || config.build)) return skipStage()

    sh 'yarn install'
  }

  stage ('lint') {
    if (!config.lint) return skipStage()

    try {
      sh 'yarn run lint -f checkstyle -o checkstyle.xml'
    } catch (e) {
      currentBuild.result = 'UNSTABLE'
    }
  }

  stage ('build') {
    if (!config.build) return skipStage()
    
    sh 'yarn run build'
  }

  stage ('test') {
    if (!config.test) return skipStage()

    sh 'yarn run test --ci'
  }
  

  stage ('reports') {
    if (!(config.lint || config.test)) return skipStage()

    if (config.test) {
       junit 'coverage/junit.xml'
    }
    if (config.lint) {
      step([$class: 'CheckStylePublisher', pattern: 'checkstyle.xml'])
    }
  }

  stage ('publish') {
    if (!config.publish) return skipStage()

    withCredentials([usernamePassword(credentialsId: 'npm-publish-credentials', usernameVariable: 'NPM_REGISTRY', passwordVariable: 'NPM_AUTH_TOKEN')]) {
      pkg = readJSON(file:'./package.json')
      pkg.version = "" + config.version
      pkg.publishConfig = [registry: "" + "https:${env.NPM_REGISTRY}"]
      writeJSON(file:'./package.json', json: pkg)

      sh 'npm config set $NPM_REGISTRY:_authToken $NPM_AUTH_TOKEN'
      sh 'npm publish ./'
      sh 'npm config delete $NPM_REGISTRY:_authToken'
    }
  }
}

def skipStage() {
  return Utils.markStageSkippedForConditional(STAGE_NAME)
}

def toSemantic(String version) {
  def dashes = version.split('-', 2);
  def parts = dashes[0].tokenize('.')
  def semanticParts = [major: 0, minor: 0, patch: 0]

  if(parts.size() > 0) semanticParts.major = parts[0].replaceAll("[^0-9]", "") ?: 0
  if(parts.size() > 1) semanticParts.minor = parts[1].replaceAll("[^0-9]", "") ?: 0
  if(parts.size() > 2) semanticParts.patch = parts[2].replaceAll("[^0-9]", "") ?: 0
  if(dashes.size() > 1) semanticParts.patch = "${semanticParts.patch}-${dashes[1]}"

  return "${semanticParts.major}.${semanticParts.minor}.${semanticParts.patch}"
}