To fail a Jenkins job based on the outcome of your Quality gate takes a bit of setting up but is a great way to track the health of your projects from Jenkins. This is achieved with our SFDX plugin.
Requirements
You will need:
- Jenkins
Setup
- In Jenkins, create a credential containing your CodeScan token (learn how to find this here).
- Create a new Pipeline.
- In the Pipeline script section you will need to paste in the code with the highlighted variables changed. These are:
- Your
credential_name
should be the name of the credential you created. my_project
should be the project key you would like to assign to your project.my_organisation
should be your Organisation Key (learn how to find this here).
- Your
node {
stage('Pull from Git') {
git 'https://wherever.com/me/my-repo.git'
}
withCredentials([string(credentialsId: 'credential_name
', variable: 'codescan_token')]) {
stage('CodeScan') {
sh '''
export PATH="$(pwd)/.sfdx-install/bin/:$PATH"
if [ ! -f .sfdx-install/bin/sfdx ]; then
mkdir .sfdx-install
wget -q https://developer.salesforce.com/media/salesforce-cli/sfdx-linux-amd64.tar.xz -O .sfdx-install/sfdx.tar.xz
tar xJf .sfdx-install/sfdx.tar.xz -C .sfdx-install --strip-components 1
echo y|sfdx plugins:install sfdx-codescan-plugin
fi
sfdx codescan:run --token=$codescan_token --projectkey=my_project
--organization=my_organisation
exit $?
''' }
} }
- Run the pipeline. If everything is set up correctly and your Quality Gate passes, you will be able to see you pipeline pass.
- If your Quality Gate fails, you will see the error in the CodeScan stage of the build.