Skip to content

Jenkins 2

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

  • a new latest version from baseBranch
  • a canary build from a pull request (only if your package manager plugin supports it)

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 { changeRequest(); }
          steps {
            sh "auto shipit"
          }
        }
        stage('Latest') {
          when { branch 'main' }
          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 main does not match any
            sh 'git checkout -b main'
            sh 'auto shipit'
          }
        }
      }
    }
    stage('Canary') {
      when { changeRequest() }
      steps {
        sh 'auto canary --pr $CHANGE_ID --build $BUILD_NUMBER'
      }
    }
  }
}
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 { changeRequest(); }
          steps {
            sh "auto shipit"
          }
        }
        stage('Latest') {
          when { branch 'main' }
          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 main does not match any
            sh 'git checkout -b main'
            sh 'auto shipit'
          }
        }
      }
    }
    stage('Canary') {
      when { changeRequest() }
      steps {
        sh 'auto canary --pr $CHANGE_ID --build $BUILD_NUMBER'
      }
    }
  }
}

Configuring Prereleases

For auto's prerelease to work you need to makes sure that you jenkins instance is configured correctly.

  1. Go to General > Projects > GitHub Organization and add a Behaviors that enables branch builds.

    A screenshot of the jenkins configuration

  2. Add this to the top of your Jenkinsfile.

    // Exit the branch build if not on "baseBranch" or a prerelease, but still do PR builds
    if (env.BRANCH_NAME != "master" && env.BRANCH_NAME != "next" && !env.BRANCH_NAME.startsWith('PR-')) {
      echo ("Aborting build. Only the master/next branch and PR builds run.")
      currentBuild.result = 'SUCCESS'
      return
     }
    // Exit the branch build if not on "baseBranch" or a prerelease, but still do PR builds
    if (env.BRANCH_NAME != "master" && env.BRANCH_NAME != "next" && !env.BRANCH_NAME.startsWith('PR-')) {
      echo ("Aborting build. Only the master/next branch and PR builds run.")
      currentBuild.result = 'SUCCESS'
      return
     }
    

    If your successful you will see two separate builds in your PRs: pr-merge and branch.

  3. Modify your canary stage to not build the prerelease branch

    stage('Canary') {
      when {
        allOf {
          changeRequest();
          expression { env.CHANGE_BRANCH != 'next' }
        }
      }
      steps {
        sh "auto shipit"
      }
    }
    stage('Canary') {
      when {
        allOf {
          changeRequest();
          expression { env.CHANGE_BRANCH != 'next' }
        }
      }
      steps {
        sh "auto shipit"
      }
    }
    
  4. Add a stage for the prerelease

    stage('Next') {
      when { branch 'next' }
      steps {
        sh 'git checkout -b next'
        sh "auto shipit"
      }
    }
    stage('Next') {
      when { branch 'next' }
      steps {
        sh 'git checkout -b next'
        sh "auto shipit"
      }
    }
    
  5. Open a PR from your prerelease branch into your baseBranch and enjoy your prerelease! 🎉

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)