# Jenkins

> Send VisualCI builds from a Jenkins pipeline. The CLI reads the commit, branch and pull request from the Git plugin and multibranch pipelines.

Source: https://visualci.com/docs/ci/jenkins/
All of VisualCI's documentation: https://visualci.com/llms.txt

Store a project token as a **Secret text** credential, say `visualci-token`. The CLI reads the commit from `GIT_COMMIT`, the branch from `CHANGE_BRANCH`, `BRANCH_NAME` or `GIT_BRANCH`, and the pull request from `CHANGE_ID`.

```groovy
pipeline {
  agent any
  environment {
    VISUALCI_TOKEN = credentials('visualci-token')
  }
  stages {
    stage('Test') {
      steps {
        sh 'curl -fsSL https://visualci.com/install.sh | VISUALCI_VERSION=0.2.5 VISUALCI_INSTALL_DIR="$WORKSPACE/bin" sh'
        sh '''
          export PATH="$WORKSPACE/bin:$PATH"
          export VISUALCI_BUILD_ID=$(visualci build create)
          npm test -- -u   # write snapshots instead of failing; see the snapshot guides
          visualci upload --from auto src tests
          visualci build finalize
        '''
      }
    }
  }
}
```

Each `sh` step is its own shell, so keep the build's commands in one step, or pass `VISUALCI_BUILD_ID` on with `withEnv`.

## Parallel stages

Create the build once, then give each parallel stage a shard number:

```groovy
stage('Start VisualCI') {
  steps {
    script {
      env.VISUALCI_BUILD_ID = sh(script: 'bin/visualci build create --shards 2', returnStdout: true).trim()
    }
  }
}
stage('Tests') {
  parallel {
    stage('Unit') { steps { sh 'npm run test:unit -- -u && bin/visualci upload --from auto src && bin/visualci build finalize --shard 0' } }
    stage('Browser') { steps { sh 'npm run test:e2e && bin/visualci upload screenshots && bin/visualci build finalize --shard 1' } }
  }
}
```

See [Other CI providers](https://visualci.com/docs/ci/other.md) for the environment variables and how shards work.
