Thursday, July 12, 2018

Jenkins Pipeline Parameter

Little background : I am running an inspec test for couple of servers. I accept server ip's as a jenkins parameter. Each IP the inspec test for the mysql profile should execute.

Parameter "IP_MYSQL" holds server ips

node{
    cleanWs()
stage('Checking MySQL'){
    echo 'Inspec test for mysql'
    withCredentials([file(credentialsId: 'mysql-prod', variable: 'SSH_KEY')]) {
    sh '''
sudo git clone https://github.com/dev-sec/mysql-baseline
cd mysql-baseline
    echo "${IP_MYSQL}" | sed -n 1'p' | tr ',' '\n' | while read IP; do
            sudo cp ../inspec.yml inspec.yml
            sudo sed -i -e "s/mysql-baseline/mysql-baseline-$IP/g" inspec.yml
            cd ..
            sudo inspec exec mysql-baseline -t ssh://clouduser@$IP -i $SSH_KEY --reporter junit:Report_$IP.xml || true
        done
'''
junit '*.xml'
    }
  }
}
}