I would like to configure the Office365 options to run based on the branch, since the URL of each branch is different
options { office365ConnectorWebhooks([[notifyBackToNormal: true, notifyFailure: true, notifySuccess: true, notifyUnstable: true, url: '']]) }
I'd suggest to create simple variable before pipeline {} block and set its value conditionally.
def webHookUrl
if ("${env.BRANCH_NAME}".startsWith("PR-")) {
webHookUrl = "https://your-first-url"
} else {
webHookUrl = "https://your-second-url"
}
pipeline {
agent {
label 'my_node'
}
options {
office365ConnectorWebhooks([[startNotification: true,
notifySuccess: true,
notifyAborted: true,
notifyNotBuilt: false,
notifyUnstable: true,
notifyFailure: true,
notifyBackToNormal: true,
notifyRepeatedFailure: true,
url: "$webHookUrl",
name: "Webhook Name"
]])
}
stages {.....}
Moreover, if you want to get full control it could be achieved by some wrapper class like this (in declarative style):
class WebhooksOptions {
// default values
Boolean startNotification = true
Boolean notifySuccess = true
Boolean notifyAborted = true
Boolean notifyNotBuilt = true
Boolean notifyUnstable = true
Boolean notifyFailure = true
Boolean notifyBackToNormal = true
Boolean notifyRepeatedFailure = true
String url
String name = "Some name"
WebhooksOptions(String url) {
this.url = url
}
}
def webHookOptions
if ("${env.BRANCH_NAME}".startsWith("PR-")) {
webHookOptions = new WebhooksOptions("https://your-first-url")
} else {
webHookOptions = new WebhooksOptions("https://your-second-url")
// you can customize notification rules as well
webHookOptions.startNotification = false
}
pipeline {
agent {
label 'my_node'
}
options {
office365ConnectorWebhooks([[startNotification: webHookOptions.startNotification,
notifySuccess: webHookOptions.notifySuccess,
notifyAborted: webHookOptions.notifyAborted,
notifyNotBuilt: webHookOptions.notifyNotBuilt,
notifyUnstable: webHookOptions.notifyUnstable,
notifyFailure: webHookOptions.notifyFailure,
notifyBackToNormal: webHookOptions.notifyBackToNormal,
notifyRepeatedFailure: webHookOptions.notifyRepeatedFailure,
url: webHookOptions.url,
name: webHookOptions.name
]])
}
stages {.....}
Related
def flag_prod1 = true
def flag_prod2 = false
def prods = [
'prod1',
'prod2'
]
pipeline
{
agent { label "j" }
stages
{
stage('Trigger')
{
steps
{
script
{
prods.each
{ prd ->
stage("stage $prd")
{
if (flag_${prd})
{
echo "Do something with $prd..."
}
}
}
}
}
}
}
}
The if statement does not work: if (flag_${prd}).
What should be the syntax of the if statement where part of the variable name needs to be taken from the other variable name?
why not to declare known variable with all possible flags like this:
def prods = [
'prod1',
'prod2'
]
def flags = [
prod1: true,
prod1: false,
]
prods.each{prd->
if( flags[prd] ){
//do something
}
}
or you could try to define all in one:
def prods = [
prod1: [
flag: true
],
prod2: [
flag: false
]
]
for(prd in prods){
if( prd.flag ){
//do smth
}
}
This works (using iterator key/value, and toBoolean() conversion):
def jobs = [
'prod1': true,
'prod2': false
]
pipeline
{
agent { label "j" }
stages
{
stage('Trigger')
{
steps
{
script
{
jobs.each
{
if ("$it.value".toBoolean())
{
stage("Stage $it.key")
{
echo "Do something..."
}
}
}
}
}
}
}
}
I have this output.txt file:
cg-123456
cg-456789
cg-987654
cg-087431
Is it possible to get these values into a jenkins dropdown, like by using choice-parameter or active-choice-reactive parameter?
You can do something like the below.
pipeline {
agent any
stages {
stage ("Get Inputs") {
steps {
script{
script {
def input = input message: 'Please select the choice', ok: 'Ok',
parameters: [
choice(name: 'CHOICE', choices: getChoices(), description: 'Please select')]
}
}
}
}
}
}
def getChoices() {
filePath = "/path/to/file/output.txt"
def choices = []
def content = readFile(file: filePath)
for(def line : content.split('\n')) {
if(!line.allWhitespace){
choices.add(line.trim())
}
}
return choices
}
properties([gitLabConnection(gitLabConnection: 'GitLab Connection', jobCredentialId: ''), [$class: 'GitlabLogoProperty', repositoryName: ''], parameters([extendedChoice(multiSelectDelimiter: ',', name: 'choice', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'mongo, mysql', visibleItemCount: 10)])])
pipeline {
agent anyenter image description here
stages {
stage('mongo') {
when {
expression { choice == 'mongo' }
}
steps {
echo "${params.choice}"
}
}
stage('mysql') {
when {
expression { choice == 'mysql' }
}
steps {[enter image description here][1]
echo "${params.choice}"
}
}
}
}
When I select both mongo and mysql checkbox then both stage should work but both stage mongo and mysql are skipped
I couldn't get the extendedChoice Parameters setup in the declarative pipeline. Instead, I used Boolean Parameters. Please refer to the following for your use case. Note I have declared a global variable named choice.
def choice = ""
pipeline {
agent any
stages {
stage("Get details") {
steps{
timeout(time: 300, unit: 'SECONDS') {
script {
// Select the product image
choice = input message: 'Please select the product', ok: 'Build',
parameters: [
booleanParam(defaultValue: false, name: 'mongo'),
booleanParam(defaultValue: false, name: 'mysql')]
}
}
}
}
stage('Echo') {
steps {
script {
echo "::: Product : ${choice}"
}
}
}
stage('mongo') {
when {
expression { return choice.get("mongo") }
}
steps {
echo "MONGO"
}
}
stage('mysql') {
when {
expression { return choice.get("mysql") }
}
steps {
echo "MYSQL"
}
}
}
post {
success {
echo 'The process is successfully Completed....'
}
}
}
I want to run jenkins job using jenkinsfile with list of all parameters value or with individual value from parameter list.
def deploy(env) {
step([$class: 'UCDeployPublisher',
siteName: siteName,
deploy: [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
deployApp: appName,
deployEnv: 'DEV',
deployVersions: "${compName}:${version}",
deployProc: simpleDeploy,
deployOnlyChanged: false,
deployReqProps: "ID=${params.ID}"
]])
CHOICES = [ 'id1', 'id2', 'id3', 'id4', 'id5' ]
PARAMETERS = CHOICES + "all"
parameters {
choice(
name: 'ID',
choices: PARAMETERS,
)
stage (DEV') {
steps {
script {
if (params.ID == "all"){
CHOICES.each {
echo "$it"
}
deploy('devl') ===> this will call my deploy function
}
else {
echo "$params.ID"
deploy('devl') ===> this will call my deploy function
}
}
}
}
I was able to run job using bye selecting each value from droplist. But I also want to the run the job with all values from ID list. I tried with all but it is not taking all the the values 'id1', 'id2', 'id3', 'id4', 'id5'
You would need to define the choices in a var outside of the parameter and then use that as choices e.g.
CHOICES = [ 'id1', 'id2', 'id3', 'id4', 'id5' ]
PARAMETERS = CHOICES + "all"
pipeline {
agent any
parameters {
choice(name: "ID", choices: PARAMETERS )
}
stages {
stage('Test') {
steps {
script {
if (params.ID == "all"){
CHOICES.each {
echo "$it"
}
}
else {
echo "$params.ID"
}
}
}
}
}
}
I have a dead-simple routing case that does not involve mapping views to routes. Instead, I want to parse the route/uri and set the value of some variable in my controller. So, for instance, if the route is #/foo I want to set one value, and if it is #/bar I want to set another value.
I don't have a simple pattern for doing this. Currently, I am doing something like this in my controller:
TodoController(this.http, this.scope, this.router) {
router.onRouteStart.listen((RouteStartEvent event) {
event.completed.then((result) {
if (result) {
if (event.uri == '/foo') {
myVar = ...
} else if (event.uri == '/bar') {
myVar = ...
} else {
// ...
}
}
});
});
}
This strikes me as a clunky approach, but I'm not coming up with anything much better. Anyone have a better idea how I can monitor the route changes in my controller?
You can do something like this:
bool isFoo = false;
bool isBar = false;
router.root
..addRoute(
name: 'foo',
path: '/foo',
enter: (_) => isFoo = true,
leave: (_) => isFoo = false)
..addRoute(
name: 'bar',
path: '/bar',
enter: (_) => isBar = true,
leave: (_) => isBar = false);
If you want to listen to route changes at a later stage, for example inside a component/controller, then you do it like this:
class MyController implements NgDetachAware {
bool isFoo = false;
bool isBar = false;
RouteHandle fooHandle;
RouteHandle barHandle;
MyController(Router router) {
fooHandle = router.root.getRouter('foo').newHandle();
fooHandle.onEnter.listen((_) => isFoo = true);
fooHandle.onLeave.listen((_) => isFoo = false);
barHandle = router.root.getRouter('bar').newHandle();
barHandle.onEnter.listen((_) => isBar = true);
barHandle.onLeave.listen((_) => isBar = false);
}
detach() {
fooHandle.discard();
barHandle.discard();
}
}
Note that in this example I'm using RouteHandle that helps us cleanup all listeners when controller is destroyed.