0

I have a node.js job running on a Bamboo server, every time i complete a pull request. The problem is that even if it fails i get a successful build, be it exception, npm package error or any other kind of error. It is annoying to check if build is successful or not every time. So the question is, is there a way to make sure a node.js job makes build fail?

const sfdx = require('sfdx-node');

const ORG_NAME = 'CH_JWT';
const FIRST_DEPLOY_PATH = 'force-app/main/default';
const SECOND_DEPLOY_PATH = 'deploy_prod';

console.log("Arguments:");
process.argv.forEach(function (val, index, array) {
    console.log(index + ': ' + val);
  });

execute(process.argv[2], process.argv[3], process.argv[4])
return;


// Function definition
function execute(sf_username, sf_client_id, executionScript){
    let authObj = {
        username: sf_username,
        clientid: sf_client_id,
        jwtkeyfile: 'server.key',
        setalias: ORG_NAME
    };

    let firstDeployObj = {
        SOURCEPATH: FIRST_DEPLOY_PATH,
        targetusername: ORG_NAME
    };

    let secondDeployObj = {
        SOURCEPATH: SECOND_DEPLOY_PATH,
        targetusername: ORG_NAME
    };

    let prodDeployment = false;

    if(executionScript === "default" || executionScript === "validateUat" || executionScript === "validateProd"){
        firstDeployObj.checkonly = true;
        secondDeployObj.checkonly = true;
    }

    if(executionScript === "validateProd" || executionScript === "deployProd"){
        prodDeployment = true;
    }

    executeDeployment(authObj, firstDeployObj, secondDeployObj, prodDeployment);
}

function executeDeployment(authObj, firstDeployObj, secondDeployObj, prodDeployment) { 
    sfdx.auth.jwt.grant(authObj).then((result) => {
        console.log('Authorization result: ' + JSON.stringify(result));
        sfdx.force.source.deploy(firstDeployObj)
        .then((deploymentResult) => {
            if(!deploymentResult){
                throw new Error('First deployment step failed');
            }
            console.log('First deployment step result: ' + getInfoString(deploymentResult));

            if(prodDeployment){
                sfdx.force.source.deploy(secondDeployObj)
                .then((deploymentResult2) => {
                    if(!validationResult){
                        throw new Error('Validation failed');
                    }
                    console.log('Second deployment step result: ' + getInfoString(deploymentResult2));
                })
                .catch(err => {
                    console.log(err);
                    throw new Error('Second deployment step failed')
                });
            }
        })
        .catch(err => {
            console.log("Something went wrong:");
            console.log(err);
            //Make job fail
            throw new Error('Deployment failed')
        });
      })
    .catch(err => {
        console.log(err);
        assert.equal(1,2);
        throw new Error('Authorization failed');
    });
} 

function getInfoString(responseObject){
    //construct response info
    return JSON.stringify(responseObject);
}   
  • Please provide enough code so others can better understand or reproduce the problem. – Nikolai Kiselev Sep 21 '22 at 09:58
  • Check the exit code of the script / command used to build Node project. Use trap command to know what exit code is & where it is produced. For reference check this: https://stackoverflow.com/questions/5312266/how-to-trap-exit-code-in-bash-script – Hussain Mansoor Sep 21 '22 at 10:22

0 Answers0