Jenkins 2

The following config declares the release action that run on all branches. The job will either release:

  • a new latest version from master
  • a canary build from a pull request (only on the main fork and if your package manager plugin implements them)

Jenkinsfile

⚠️You must use some sort of step that implements skip ci functionality. Otherwise you will get stuck in a release loop!


pipeline {
  environment {
    NPM_TOKEN = credentials('NPM_TOKEN')
    GH_USER = credentials('GH_USER')
    GH_TOKEN = credentials('GH_TOKEN')
  }
  stages {
    stage('Check Skip CI') {
      steps {
        script {
          result = sh (script: "git log -1 | grep '.*\\[skip ci\\].*'", returnStatus: true)
          if (result == 0) {
              echo ("'Skip CI' spotted in git commit. Aborting.")
              currentBuild.result = 'ABORTED'
              error('Exiting job');
          }
        }
      }
    }
    stage('Prepare repository') {
      steps {
        // Jenkins will do weird things to "origin" in PR and branch build
        // To get around this reset the "origin" back to normal
        // This is only necessary if you are making next/pre-releases
        sh 'git config --unset-all remote.origin.fetch'
        sh 'git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*'
        // Ensure you have all branches and tags
        sh 'git fetch origin --tags'
      }
    }
    stage('Install') {
      steps {
        sh 'yarn install --frozen-lockfile'
      }
    }
    stage('Build') {
      steps {
          sh 'yarn build'
      }
    }
    stage('Publish') {
      parallel {
        stage('Canary') {
          when {
            allOf {
              changeRequest();
              expression { env.CHANGE_BRANCH != 'next' }
            }
          }
          steps {
            sh "auto shipit"
          }
        }
        stage('Next') {
          when { branch 'next' }
          steps {
            sh 'git checkout next'
            sh "auto shipit"
          }
        }
        stage('Latest') {
          when { branch 'master' }
          steps {
            // Jenkins will leave you in a detached HEAD state during builds
            // Make sure to checkout your baseBranch here or the push will fail!
            // The error will look like the following:
            // error: src refspec master does not match any
            sh 'git checkout master'
            sh 'auto shipit'
          }
        }
      }
    }
    stage('Canary') {
      when { changeRequest() }
      steps {
        sh 'auto canary --pr $CHANGE_ID --build $BUILD_NUMBER'
      }
    }
  }
}

Troubleshooting

If you are having problems make sure you have done the following:

  • GH_TOKEN is set
  • Any other secrets for plugins are set (Ex; NPM_TOKEN with the NPM plugin)