I am trying to run a bash script after a successful build in jenkins.
stages {
stage("test") {
steps {
...
}
post {
success {
steps {
sh "./myscript"
}
}
}
}
}
I am getting an error saying that method "steps" does not exist. How can I run a script after a successful build?
You need to remove the ”steps” inside the ”success” block. call the script directly inside the ”success” block.
according to the docs which is quite confusing, the ”success” is a container for steps (so no need to add another nested ”steps” ):
https://jenkins.io/doc/book/pipeline/syntax/#post
stages {
stage("test") {
steps {
...
}
post {
success {
sh "./myscript"
}
}
}
}
Related
I am trying to do something that seems pretty basic but is giving me a lot of trouble. I am trying to copy the jar file that is built into a different directory but the build keep failing on that step. Here is my Jenkinsfile and the "Copy" step at the bottom is causing the failure.
pipeline {
agent any
stages {
stage("Clean Up"){
steps{
deleteDir()
}
}
stage("Clone Repo"){
steps {
bat "git clone https://github.com/CBoton/spring-boot-hello-world.git"
}
}
stage("Build"){
steps {
dir("spring-boot-hello-world") {
bat "mvn clean install"
}
}
}
stage("Test") {
steps {
dir("spring-boot-hello-world") {
bat "mvn test"
}
}
}
stage("Package") {
steps {
dir("spring-boot-hello-world") {
bat "mvn package"
}
}
post {
success {
dir("spring-boot-hello-world") {
archiveArtifacts 'target/*.jar'
}
}
}
}
stage("Copy"){
steps {
dir("spring-boot-hello-world"){
bat ("copy target/*.jar C:\\Users\\Curti\\Downloads")
}
}
}
}
}
How I have it now produces the following error
C:\ProgramData\Jenkins\.jenkins\workspace\spring\spring-boot-hello-world>copy target/*.jar C:\Users\Curti\Downloads
The syntax of the command is incorrect.
I have tried many variations for the path but can't get it to work. This is starting to drive me crazy because it seems like it would be so simple. Any help would be greatly appreciated.
#ycr, I was trying to do it without plugins but ultimately the File Operations plugin worked, thank you. My new copy step looks like
stage("Copy"){
steps {
fileOperations([fileCopyOperation(
excludes: '',
flattenFiles: false,
includes: '**/*.jar',
targetLocation: "C:\\output"
)])
}
}
I'm trying to create a Jenkinsfile that runs a release build step only if a boolean parameter isRelease is true, otherwise skip this step.
My Jenkinsfile looks like this (extract):
pipeline {
agent { label 'build' }
tools {
jdk 'OpenJDK 11'
}
parameters {
booleanParam( name: 'isRelease', description: 'Run a release build?', defaultValue: false)
}
stages {
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Release') {
when {
beforeAgent true
expression { return isRelease }
}
steps {
sh 'echo "######### Seems to a release!"'
}
}
}
}
However, I don't seem to understand how to use the parameters variable properly. What happens is that the release step is always executed.
I changed expression { return isRelease } to expression { return "${params.isRelease}" } which did not make a difference. Changing it to expression { return ${params.isRelease} } causes the step to fail with java.lang.NoSuchMethodError: No such DSL method '$' found among steps.
What's the right way to use a parameter to skip a step?
You were closest on your first attempt. The latter two failed because:
Converting a Boolean to a String would always return truthiness because a non-empty String is truthy.
That is invalid Jenkins Pipeline or Groovy syntax.
The issue here with the first attempt is that you need to access isRelease from within the params object.
when {
beforeAgent true
expression { params.isRelease }
}
The when directive documentation actually has a very similar example in the expression subsection for reference.
I've linked up to the 'github webhook' now.
Now all we have to do is build and deploy to 'ec2'.
Is it a place to create and test files to be uploaded to ec2 in the build phase? If yes, I wonder what the code would look like.
And I wonder how to utilize dir in build .
I'm curious about the code how to write it in the deployment stage
Below is the code I have written so far
pipeline {
agent any
environment {
SLACK_CHANNEL = '#'
}
stages {
stage('Start') {
steps {
slackSend (channel: SLACK_CHANNEL, color: '#FFFFOO', message: "STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
stage('Checkout') {
steps {
git branch: 'feature/signin.up',
credentialsId: 'github_access_token',
url: 'https://github.com/###/westudy.git'
}
}
stage('Build') {
steps {
{
}
}
}
stage('Deploy') {
steps {
}
}
I want to execute nested declarative scripts that pre exist. Say I have this Declarative script in my workspace and its called test.DS
pipeline {
agent any
stages {
parallel {
stage('stage-1') {
steps {
sh "echo this is stage-1"
}
}
stage('stage-2') {
steps {
sh "echo this is stage-2"
}
}
}
}
}
What would a declarative script look like that will run this script test.DS?
Below is one of the possible solutions
node {
load './test.DS'
}
I'm setting up Jenkins pipeline which is mentioned below. My build gets aborted if the 1st stage is got failed but I want to execute 1st all stage and steps which are mentioned in stages.
pipeline {
agent none
stages {
stage("build and test the project") {
agent {
docker "coolhub/vault:jenkins"
}
stages {
stage("build") {
steps {
sh 'echo "build.sh"'
}
}
stage("test") {
steps {
sh 'echo "test.sh" '
}
}
}
}
}
}
I'd like to execute 1st all stage and steps which are mentioned in stages.
after all, stage gets executed then finally need to get abort Jenkins job and show stage and steps which are failed.
Yeah, well there's no way currently to do that apart from try catch blocks in a script.
More here: Ignore failure in pipeline build step.
stage('someStage') {
steps {
script {
try {
build job: 'system-check-flow'
} catch (err) {
echo err
}
}
echo currentBuild.result
}
}
In hakamairi's answer, the stage is not marked as failed. It is now possible to fail a stage, continue the execution of the pipeline and choose the result of the build:
pipeline {
agent any
stages {
stage('1') {
steps {
sh 'exit 0'
}
}
stage('2') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "exit 1"
}
}
}
stage('3') {
steps {
sh 'exit 0'
}
}
}
}
In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:
As you might have guessed, you can freely choose the buildResult and stageResult, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.
Just make sure your Jenkins is up to date, since this is a fairly new feature.