Salesforce deployment status from Jenkins
Introduction
There was a scenario on a project I worked on, which used multiple Jenkins Pipelines to carry out build validation and deployments in Salesforce.
There were times when a deployment or validation became stuck on the Salesforce environment. Ultimately, Salesforce cannot run parallel validations / deployments; it incorporates a queue and will add a new job to the queue until the first job has completed. Thus, there was a requirement to check the deployment status in Salesforce from a Jenkins Pipeline; if a job was in progress or queued then the overall Jenkins Pipeline would fail.
I wrote a simple bash script and added this to a Jenkins JOB which helped in this occasion.
Shell script
This script achieved the following:
- Login to Salesforce environment
- Extract Salesforce deployment Id from Jenkins logs
- Check deployment status
Login to Salesforce environment
- Utilises the Salesforce Login API over SOAP
- More information - Login API
XML login payload message sent over SOAP is provided below.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">
<soapenv:Body>
<urn:login>
<urn:username>${sf_username}</urn:username>
<urn:password>${sf_password}</urn:password>
</urn:login>
</soapenv:Body>
</soapenv:Envelope>
The above XML payload is sent over to the Salesforce environment endpoint:
soap_url="$sf_url/services/Soap/u/$api_version"
sf_url
is the URL you use to access your Salesforce environmentapi_version
is the API version you are using on the environment (can be the latest).
curl
Using curl this can be sent over by using the following.
response=`curl -s $soap_url -H "Content-Type:text/xml;charset=UTF-8" -H "SOAPAction:login" -d @.login.xml`
Make sure you set the http headers noted by the ‘-H’ in the command above, else the API will reject the request.
If the login is successful, you will get a session id from Salesforce which can be used in all subsequent API calls.
Extract Salesforce deployment Id from Jenkins logs
- Utilises the Jenkins API - to extract a deployment Id from Jenkins build logs
Check deployment status
- Utilises the Salesforce check deployment status API over SOAP
- More information - Check Deployment Status
Extracting a Jenkins log from the API is easy enough, but make sure you have your Jenkins API token otherwise the curl command below will not work.
curl -s --user $jenkins_username:$jenkins_api_token $jenkins_url/$job_name/$last_build_number/consoleFull > $job_name-$last_build_number-output.log
The deployment id can be extracted via.
deployment_id=cat "$job_name-$last_build_number-output.log" | grep -w "Request ID for the current deploy task" | awk {'print $11'}`
Here is the XML payload which needs to be sent over in the request to Salesforce to get the deployment status of a specific deployment.
Note. the deployment_id and session id requires updating to a valid id.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:met="http://soap.sforce.com/2006/04/metadata">
<soapenv:Header>
<met:SessionHeader>
<met:sessionId>$sessionId</met:sessionId>
</met:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<met:checkDeployStatus>
<met:asyncProcessId>$deployment_id</met:asyncProcessId>
<met:includeDetails>false</met:includeDetails>
</met:checkDeployStatus>
</soapenv:Body>
</soapenv:Envelope>
If the request is successful, the deploy status returned will fall under either one of the following states.
"InProgress"
"Succeeded"
"SucceededPartial"
"Failed"
"Canceling"
"Canceled"
Further reading.