# Integrating Jenkins

Source: https://www.jawsdeploy.net/guides/integrating-jenkins

Use the PowerShell SDK in a Jenkinsfile, or call the REST API directly from a `sh` step.

Jenkins works with Jaws Deploy through PowerShell or shell steps in a declarative or scripted Jenkinsfile. Both rely on a Jenkins credential storing the API key.

## Add this stage at the end of the pipeline

Store the service account ID and API key as a username/password credential. `withCredentials` exposes them as the environment variables the SDK reads and masks them in console output.

```
stage('Create release') {
  steps {
    withCredentials([usernamePassword(credentialsId: 'jaws-deploy-service-account',
                                      usernameVariable: 'JAWS_API_LOGIN',
                                      passwordVariable: 'JAWS_API_PASSWORD')]) {
      pwsh '''
        Import-Module ./JawsDeploySdk.psm1   # github.com/JawsDeploy/powershell-sdk
        New-JawsRelease `
            -projectId       "<project id>" `
            -version         "1.0.$env:BUILD_NUMBER" `
            -packageVersions @{ "Checkout.Web" = "1.0.$env:BUILD_NUMBER" }
      '''
    }
  }
}
```

## Without PowerShell

If the Jenkins agents are Linux without PowerShell installed, call the REST API directly with `curl` from an `sh` step. The behaviour is identical.

> **// Multi-branch pipelines - Only create releases on the branches that should produce deployable artefacts.**
> 
> Wrap the release stage in a `when { branch 'main' }` block (declarative) or an `if (env.BRANCH_NAME == 'main')` guard (scripted). Otherwise every PR build will attempt to publish a release version that may not deploy cleanly.

