Jenkinsfile to run Terraform - jenkins-pipeline

following this tutorial
https://medium.com/#devopslearning/100-days-of-devops-day-34-terraform-pipeline-using-jenkins-a3d81975730f
I want to be run a terraform file from Jenkins
I have installed Terraform plugin version 1.0.9
I go create a new pipeline project
on the pipeline tab I choose pipeline script and paste the below script
node {
env.PATH += ":/opt/terraform_0.7.13/"
stage ('Terraform Plan') {
sh 'terraform plan -no-color -out=create.tfplan'
}
// Optional wait for approval
input 'Deploy stack?'
stage ('Terraform Apply') {
sh "terraform --version"
}
This is the console output
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Terraform Plan)
[Pipeline] sh
[aws_terraform] Running shell script
+ terraform plan -no-color -out=create.tfplan
/var/lib/jenkins-slave/workspace/ow/ow_eng/aws_terraform#tmp/durable-53622951/script.sh: line 2: terraform: command not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

The terraform binary is not installed on the jenkins slave that is executing the pipeline. The binary must be installed to have the plugin work

If you would like to use a docker image for running this you can use this snippet:
pipeline {
agent {
docker {
image 'hashicorp/terraform:light'
args '--entrypoint='
}
}
stages {
stage('Terraform Plan') {
steps {
sh 'terraform plan -no-color -out=create.tfplan'
}
}
// Optional wait for approval
input 'Deploy stack?'
stage ('Terraform Apply') {
sh "terraform --version"
}
}
}
Be aware that you will need to install the docker pipeline plugin. The trick is here to redefine the entrypoint because the official terraform image already defines an entrypoint to the terraform executable.

Related

Jenkins: Catch error to skip to next stage, but still show errored stage has failed

I am currently using Jenkins catchError to catch if an error has occurred within a stage, and if so, then skip to the next stage.
What I would like is to present that if a stage had caught an error, to presented in Jenkins as Red (Failed), even though this stage was skipped to the next.
This is how Jenkins pipeline stage is coded to catch error is script fails:
stage('test_run') {
steps {
catchError {
sh """#!/bin/bash -l
***
npm run test:run:reporter
"""
}
}
}
I found this solution in StackOverflow:
Jenkins: Ignore failure in pipeline build step
This solution works, but in Jenkins' run, the stage that failed is presenting Green (aka Success).
This Jenkins run indicates that it failed:
The following stage actually the cause of the failure and was skipped on caught error, however, this stage is showing Green (Success) and prefer to present it to be Red (Failed):
The final post actions is showing as Yellow (Unstable), when normally it shows as Green (Success):
Thank you for reading, always appreciate the assistance.
You can use
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
Add your commands
}
Add whatever result you want to set for build and stage result.
#DashrathMundkar Thank you
Using catchError() with the buildResult and stageResult, however I set the values to both 'FAILURE', and that did the trick.
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE', message: 'Test Suite had a failure') { }

Getting ERROR: script returned exit code 1 while testing

Getting below build output- I need to know the reason behind FAILURE (all required packages were pre-installed)
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /opt/bitnami/jenkins/jenkins_home/workspace/testing-xx
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ npm -s run test
Test Suites: 4 passed, 4 total
Tests: 20 passed, 20 total
Snapshots: 1 obsolete, 3 passed, 3 total
Time: 6.88s
Ran all test suites.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
Please help me understand the reason and resolution (I have removed few outputs to shorten)

stageInitS4sdkPipeline running on slave node not working anymore

[My Requirements] I have setup pipeline with master node and multiple slave nodes. Since my master node does not have web access to github.com, so I need to transfer all pipeline workload to slave nodes.
[Previous configuration, failed now]
1. Configuration in Jenkinsfile
pipeline {
agent { node { label 'slave1' } }
Modification of cloud-s4-sdk-pipeline-lib/vars/stageInitS4sdkPipeline.groovy
node('slave1') {
deleteDir()
[Current Error message] When running initS4sdkPipeline, it changes back to master node
10:24:37 + `date --utc +%Y-%m-%dT%H%M%S%Z`
[Pipeline] `echo`
10:24:37 [artifactSetVersion] Sending telemetry data is disabled.
[Pipeline] `node`
10:24:38 Running on Jenkins in /var/jenkins_home/workspace/PipelineJava_master
[Pipeline] {
[Pipeline] }
[Pipeline] `// node`
[Pipeline] `echo`
10:24:38 --- Begin library step of: mavenExecute ---
[Pipeline] `echo`
10:24:38 Unstash content: piper-bin
[Pipeline] unstash
[Pipeline] `echo`
10:24:38 Unstash failed: piper-bin (No such saved stash ‘piper-bin’)
[Pipeline] httpRequest
10:24:38 HttpMethod: GET
10:24:38 URL: https://github.com/SAP/jenkins-library/releases/download/v1.39.0/piper
10:24:38 Sending request to url: https://github.com/SAP/jenkins-library/releases/download/v1.39.0/piper
10:24:38 Treating UnknownHostException(github.com: Name or service not known) as 404 Not Found
[further questions] I never understand why it is hard coded as node('master') in stageInitS4sdkPipeline. I think it is much better if it is configurable using node label names. Currently, I also do not have clues for how this will support multiple slave nodes distribution. And There is more steps are hard coded in pipeline, for example: 'checkDiskSpace.groovy'
I think this specific issue was resolved in version v36 of the SAP Cloud Sdk Pipeline by not using curl anymore and not forcing to run parts of the init stage on the master.
For more details please also see the release notes of v36
https://github.com/SAP/cloud-s4-sdk-pipeline/releases/tag/v36

How to fix stage for use with SonarQube in JenkinsPipeline

I am setting up my pipeline to do a SonarQube scan on a dotnet project. This is the stage:
stage('SonarQube analysis') {
withSonarQubeEnv('My Sonar') {
dotnet "/usr/local/sonar-scanner-msbuild/SonarScanner.MSBuild.dll" begin /k:${SONARQUBEPROJECTKEY}
dotnet build "src/hub-backend.sln"
dotnet "/usr/local/sonar-scanner-msbuild/SonarScanner.MSBuild.dll" end
}
}
However, this fails and returns this error:
Obtained Jenkinsfile from git https://<removed>/scm/<removed>/jenkins-stuff.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 30: expecting '}', found 'begin' # line 30, column 84.
ild/SonarScanner.MSBuild.dll" begin /k:$
This errors says that it is expecting '}' but I don't see where that would be. The nesting of curly braces seem to be correct.
Can somebody help me understand this error?
Thanks
Eric
I think you are missing a sh or bat in front of the dotnet (or is dotnet a jenkins pipeline step implemented by some jenkins plugin?).
Assuming that your dotnet is a command that's installed for you jenkins user on your linux slaves.
stage('SonarQube analysis') {
withSonarQubeEnv('My Sonar') {
sh "dotnet \"/usr/local/sonar-scanner-msbuild/SonarScanner.MSBuild.dll\" begin /k:${SONARQUBEPROJECTKEY}"
sh "dotnet build \"src/hub-backend.sln\""
sh "dotnet \"/usr/local/sonar-scanner-msbuild/SonarScanner.MSBuild.dll\" end"
}
}

pipline always trigered surprisely

I run 3 pipeline builds which need approval on jenkins where there are only 2 executors. So if the first and the second build are not approved, the third build will wait, meanwhile the strange things will happen that the third build will be triggered once per minute(I set scm poll "* * * * * "and these three builds all triggered by code commit). How can I fix this?
this is my pipeline script
pipeline{
agent any
stages{
stage('Build'){
steps{
echo 'Building123'
}
}
stage('Test'){
steps{
echo 'Testing12'
}
}
stage('Approve'){
steps{
script{
input message: 'Do you want to deploy?'
}
}
}
stage('Deploy-Staging1'){
steps{
echo 'Deploy-Staging1'
echo 'Deploy-Staging2'
}
}
stage('Deploy-Production'){
steps{
echo 'Deploy-Production'
}
}
}
}
build 47, 48 and 49 is triggered by commit, build50 and others are triggered accidentally
jenkins build screen
this is the polling log of build 54
Polling Log
View as plain text
This page captures the polling log that triggered this build.
Started on Feb 27, 2018 1:52:00 AM
Using strategy: Default
[poll] Last Built Revision: Revision f0a868f03cba928812b8505afbfd86034605f96e (origin/master)
> git --version # timeout=10
> git ls-remote -h https://github.com/yuzp1996/test-approve # timeout=10
Found 1 remote heads on https://github.com/yuzp1996/test-approve
[poll] Latest remote head revision on refs/heads/master is: 6a9a5f9ef27350be97978e225bef1dda7272f7ef
Done. Took 5.3 sec
Changes found

Resources