I am using following step in my pipeline jenkins job:
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'my#xyz.com', sendToIndividuals: true])
But no email was sent when the build failed (i.e. error). Any pointers why?
P.S. Emails can be sent from this server, I have tested that.
Use Declarative Pipelines using new syntax, for example:
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'echo "Fail!"; exit 1'
}
}
}
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
failure {
mail bcc: '', body: "<b>Example</b><br>\n\<br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "foo#foomail.com";
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
}
}
You can find more information in the Official Jenkins Site:
https://jenkins.io/doc/pipeline/tour/running-multiple-steps/
Note that this new syntax make your pipelines more readable, logic and maintainable.
You need to manually set the build result to failure, and also make sure it runs in a workspace. For example:
try {
throw new Exception('fail!')
} catch (all) {
currentBuild.result = "FAILURE"
} finally {
node('master') {
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'my#xyz.com', sendToIndividuals: true])
}
}
The plugin is checking currentBuild.result for the status, and this isn't normally changed until after the script completes.
Related
I would love to use Jenkins job-dsl pipelineJob for creating a build job for a GitHub repository with a static (and centrally maintained) pipeline.
But looking at the documentation (https://jenkinsci.github.io/job-dsl-plugin/#path/javaposse.jobdsl.dsl.DslFactory.pipelineJob-definition) I can either create a cps with a static script or cpsScm with a SCM and a reference to the Jenkinsfile in the repository.
The requirement for having SCM defined comes from the gitParameter plugin, which I want to use for picking a git revision.
Is there a way how I can use a static script for the pipeline together with the SCM?
Update:
This is concretely what I would like to do:
defining a pipeline job
using a git Parameter to select the revision
declare the particular script inline
pipelineJob("test") {
parameters {
gitParameter {
name('revision')
type('PT_BRANCH_TAG')
defaultValue('origin/master')
selectedValue('DEFAULT')
description('')
branch('')
branchFilter('')
tagFilter('')
useRepository('')
quickFilterEnabled(true)
}
}
logRotator {
numToKeep(50)
}
definition {
cpsScm {
scm {
git {
remote {
github("<my-repo>")
credentials('github')
}
branch('$revision')
}
}
script("""
#Library(value='pipeline-lib#master', changelog=false) _
myPipeline projectName: 'test-name'
""")
}
}
}
Pipeline as code has incorporated many functions of jobs-dsl, which is probably why some of the fine tuning capabilities are missing from jobs-dsl's pipelineJob. The examples given on the the Git Parameter plugin page are actually in the pipelines, which could be be the script portion of the cps portion of the jobs-dsl definition. The code quoted in the question could be converted to:
pipelineJob("test") {
logRotator {
numToKeep(50)
}
definition {
cps {
script('''
pipeline {
agent any
libraries {
lib("pipeline-lib#master")
}
parameters {
gitParameter name: 'revision',
type: 'PT_BRANCH_TAG',
defaultValue: 'origin/master',
selectedValue: 'DEFAULT',
description: '',
branch: '',
branchFilter: '',
tagFilter: '',
useRepository: '',
quickFilterEnabled: true
}
stages {
stage('Build') {
steps {
git branch: "${revision}",
url: <myrepo>,
credentialsId: 'github'
// not familiar with what the next line does, assuming it's a pipeline step
myPipeline projectName: 'test-name'
}
}
}
''')
}
}
}
This is for a declarative pipeline. There is also an example of scripted pipeline on the git parameter plugin page.
I have configured my jenkins pipeline to send mail like this
post {
success {
mail bcc: '', body: 'this is the body', cc: '', from: '', replyTo: '', subject: 'This is a test', to: 'xyz#gmail.com'
}
}
Now, I would like to send an attachment along with this, but, could not able to find out any option for that. Does anyone knows how to send attachment with the above syntax. I will be thankful.
I'm getting the error
Invalid exit definition ("success"). Must be a dictionary-- i.e. plain JavaScript object like `{}`.
Invalid exit definition ("error"). Must be a dictionary-- i.e. plain JavaScript object like `{}`.
when doing sails lift. The error is on getRole.js
module.exports = {
friendlyName: 'Get Role',
description: '',
inputs: {
user_id: {
friendlyName: 'User Id',
description: 'The ID of the user to check role',
type: 'string',
required: true
}
},
exits: {
success: function (role){
return role;
},
error: function (message) {
return message;
}
},
fn: function (inputs, exits) {
User.findOne({ id: inputs.user_id } , function (err, user) {
if (err) return exits.err(err);
return exits.success(user.role);
});
}
};
This is a new error, and looking at my git, nothing has changed in my code since it successfully compiled. I understand the Sails version (v1.0) I'm using in beta, so I'm taking that into account.
Exits cannot be defined as functions. There is a special syntax (Machine Spec) to define exits. In your example this should work:
exits: {
error: {
description: 'Unexpected error occurred.',
},
success: {
description: 'Role was succesffuly fetched'
}
},
You can read more info about helper exits here: https://next.sailsjs.com/documentation/concepts/helpers
May changes occur on the last release 1.0.0-38. I've not checked underneath yet, but the way to execute helpers changed: on .exec() I get errors. Now, use .switch();
I want to use grunt for deployment and therefore want to read in configuration of remote hosts based on the already existing ~/.ssh/config file.
To load that configuration I'm using sshconf but need to include the grunt.initConfig() call in the callback to have the configuration when defining environments.
var sshconf = require('sshconf');
module.exports = function(grunt) {
// Read in ssh configuration
sshconf.read(function(err, sshHosts) {
if (err)
console.log(err);
// SSH config loaded, now init grunt
grunt.initConfig({
sshconfig: {
staging: {
privateKey: grunt.file.read(sshHosts['project_staging'].properties.IdentityFile),
host: sshHosts['project_staging'].properties.HostName,
username: sshHosts['project_staging'].properties.User,
port: sshHosts['project_staging'].properties.Port || 22,
path: "/var/www/project"
},
production: {
// ...
}
},
// Tasks to be executed on remote server
sshexec: {
example_task: {
command: 'uptime && hostname'
}
},
sftp: {
deploy: {
files: {
"./": ["*.json", "*.js", "config/**", "controllers/**", "lib/**", "models/**", "public/**", "views/**"]
},
options: {
//srcBasePath: "test/",
createDirectories: true
}
}
}
// More tasks
// ...
});
grunt.loadNpmTasks('grunt-ssh');
// More plugins ...
});
};
When I call grunt --help it states:
> grunt --help
Grunt: The JavaScript Task Runner (v0.4.1)
…
Available tasks
(no tasks found)
If I do not wrap the grunt initiation in that callback (sshconf.read(function(err, sshHosts) {})) everything is working fine (except for the ssh config not loaded or not yet ready to be used).
Is what I am trying even possible and if so, how? Am I missing something obvious?
Grunt init cannot be used in an async fashion like this. Either read the sshconf synchronously, or use a task, as described in this answer: How can I perform an asynchronous operation before grunt.initConfig()?
I am using https://github.com/eleith/emailjs for my node.js application
After setting up the configuration properly, when I send email, I get the successful message in my log, but I do not see any mails either in the inbox or spambox :-(..
I using gmail, smtp.gmail.com, ssl:true, port:465.
email.send({...},function(err, message) {
if (err) {
console.log('Error sending email : ', err);
}
else {
console.log('Email SUCCESSFULLY sent', message);
}
^[[32m[2011-10-13 06:53:28.758] [INFO] console - ^[[39mEmail SUCCESSFULLY sent {
attachments: [],
html: null,
header:
{ 'message-id': '<1318488805460.5532#Abcd-PC>',
from: 'xxxxx#gmail.com',
to: 'yyyyy#gmail.com, ',
cc: 'zzzzz#gmail.com',
subject: 'Test mail from emailjs' },
content: 'text/plain; charset=utf-8',
text: 'Testing sending email' }
I use the following code for my emails in node.js (using emailjs)
var server = email.server.connect({
user: 'your#gmail.com',
password: 'gmailPass',
host: 'smtp.gmail.com',
ssl: true
});
server.send({
text: 'hello world',
from: 'obama#state.gov',
to: 'someone#else.com',
subject: 'just a subject here'
}, function(err, message) {
if (err) {
// err
} else {
// ok
}
});
Problem was comma(,) "yyyyy#gmail.com," in the 'to' email. I was trying to add multiple ids separated by commas(,) and that created the problem..
In the documentation, they have mentioned though
**"someone <someone#gmail.com>, another <another#gmail.com>"**
may be it expects the email format to be in the form they mentioned.