Skip to content

Instantly share code, notes, and snippets.

@keirbowden
Created April 16, 2018 12:39
Show Gist options
  • Save keirbowden/32c4dd3478fe9b7d4bf5b4cced7ce951 to your computer and use it in GitHub Desktop.
Save keirbowden/32c4dd3478fe9b7d4bf5b4cced7ce951 to your computer and use it in GitHub Desktop.
Node script to execute a deployment and return 0 for success or 1 for failure.
#!/usr/local/bin/node
var fs=require('fs');
var child_process=require('child_process');
var username='keir.bowden@sfdx.deploy';
var deployParams=['force:mdapi:deploy', '-d', 'Orchestrate/src',
'-u', username, '--json'];
var result=execHandleError('sfdx', deployParams);
var status=result.result.status;
while (-1==(['Succeeded', 'Canceled', 'Failed'].indexOf(status))) {
var msg='Deployment ' + status;
if ('Queued'!=status) {
msg+=' (' + result.result.numberComponentsDeployed + '/' +
result.result.numberComponentsTotal + ')'
}
console.log(msg);
var reportParams=['force:mdapi:deploy:report', '-i', result.result.id,
'-u', username, '--json'];
var result=execHandleError('sfdx', reportParams);
status=result.result.status;
}
var exitVal=0;
console.log('Deployment ' + result.result.status);
if ('Failed'===result.result.status) {
if (result.result.details.componentFailures) {
// handle if single or array of failures
var failureDetails;
if (Array.isArray(result.result.details.componentFailures)) {
failureDetails=result.result.details.componentFailures;
}
else {
failureDetails=[];
failureDetails.push(result.result.details.componentFailures);
}
for (var idx=0; idx<failureDetails.length; idx++) {
var failure=failureDetails[idx];
console.log('Error: ' + failure.fileName +
': Line ' + failure.lineNumber +
', col ' + failure.columnNumber +
' : '+ failure.problem);
}
}
exitVal=1;
}
process.exit(exitVal);
function execHandleError(cmd, params) {
try {
var err=fs.openSync('/tmp/err.log', 'w');
resultJSON=child_process.execFileSync(cmd, params, {stdio: ['pipe', 'pipe', err]});
result=JSON.parse(resultJSON);
fs.closeSync(err);
}
catch (e) {
fs.closeSync(err);
// the command returned non-zero - this may mean the metadata operation
// failed, or there was an unrecoverable error
// Is there an opening brace?
var errMsg=''+fs.readFileSync('/tmp/err.log');
var bracePos=errMsg.indexOf('{');
if (-1!=bracePos) {
resultJSON=errMsg.substring(bracePos);
result=JSON.parse(resultJSON);
}
else {
throw e;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment