How to setup concurrent build in azure devops service - tfs - tfs

I need to build the code one after another in TFS. When the first build pipeline is completed, the second build pipeline should be triggered automatically

If you are using Azure DevOps service:
You could simply chain related builds together using build completion triggers.
Add a build completion trigger to run your build upon the successful
completion of the triggering build. You can select any other build in
the same project.
After you add a build completion trigger, select the triggering build.
If the triggering build is sourced from a Git repo, you can also
specify branch filters. If you want to use wildcard characters, then
type the branch specification (for example, features/modules/*) and
then press Enter.
Source Link
If you are using on-premise and your TFS version do not support build completion triggers:
There are two ways to run another build in your current build.
Option 1: add PowerShell task in your current build definition to queue another build by REST API
Assume another build id is 5, so you can add PowerShell task with the script:
$body = #{
definition = #{
id = 5
}
}
$Uri = "http://account.visualstudio.com/DefaultCollection/project/_apis/build/builds?api-version=2.0"
$buildresponse = Invoke-RestMethod -Method Post -UseDefaultCredentials -ContentType application/json -Uri $Uri -Body (ConvertTo-Json $body)
Option 2: install related extension in Market place
There are some extensions you can install for your VSTS account, then you can add the task to queue another build. such as Queue Build(s) Task, Trigger New Build, Queue New Build etc.

Related

trigger the jenkins job from octopus

I want to call the jenkins job(automated smoke suite) automatically from the octopus(deployment tool), once the deployment is successful.
The simplest way of doing this I found is, to create a script that uses the Jenkins REST API to trigger the jenkins job. And then add that script as a step at the end of deployment in octopus.
Created a powershell script,
to call the REST API
Invoke-WebRequest -Uri "http://jenkinsserver/jenkins/job/Run%20a%20script/build" -Headers $BuildHeaders -Method Postenter code here
I am getting error " Invoke-WebRequest : HTTP ERROR 403 No valid crumb was included in the request".
In jenkins due to security issue I can not change configuration.
Please guide me to proceed.
There is a community step, Jenkins - Queue Job, for triggering a Jenkins job.
More information about Octopus Community Step Templates - https://octopus.com/docs/projects/community-step-templates

TFS rest API - queue build on same machine

I am trying to see if it is possible to queue a build on the same VM as another build. Note, I am in the vNext system. I know it is possible to configuration information about the agent queues. but I want to get a little more specific than that. I'm not sure if what I want falls outside of build best practices, but I'd like to ask anyway.
Consider the following setup for builds A & B:
There are 4 agents: 2 on server 1, 2 on server 2 Any agent can pick up build A or build B .... I know how to set up the demands so only agents on those VMs can pick up this builds.
Build A uses the rest API to queue build B, and wait for it to complete. Right now, under current configurations, the spawned build B can be picked up by any of the 3 remaining agents on (so the free agent on the same machine as build A, or any of the agents on the other server).
I want to try to run build B on the same server as build A when build A launches build B. In other words, if an agent on server 1 picks up build A, I want the build B it launches to use the same server (using the other agent on the machine). Conversely, if the server 2 picks up build A, I want the build B it launches to be picked up by server 2 as well. Assuming I know things like the Agent ID, Agemt Machine Name, etc, I wanted to use the rest API in c# to simulating launching build B to try to control which machine picks it up.
I could configure agents so that both builds only run on 1 machine, but I am trying to avoid running too many agents on one machine. I could configure the above 4 agents to run on 1 machine, but I want to distribute the agents as much as possible. I know it is possible to specify information about agents queues / pools etc, bot not the machine. I am trying to avoid having to restrict the number of machines that run a set of agents.
I know this may seem a bit unusual ... but I am dealing with a scenario where I want to share information between the builds that will be simplified if they run on the same machine. If build B is launched by itself, it doesn't matter what machine picks it up. If this isn't possible, I will try other ways to share information between the builds.
Is it possible to do this?
Yes, you can achieve that by set up the demands with REST API when queue the build.
For example, you can use below PowerShell script to queue a build with the specific agent:
Param(
[string]$collectionurl = "http://server:8080/tfs/DefaultCollection/",
[string]$projectName = "ProjectName",
[string]$keepForever = "true",
[string]$BuildDefinitionId = "1",
[string]$user = "username",
[string]$token = "password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = #"
{
"definition": {
"id": $BuildDefinitionId
},
"sourceBranch": "$/xxxx",
"demands":["Agent.Name -equals Agent1"]
}
}
"#
return $value
}
$json = CreateJsonBody
$uri = "$($collectionurl)/$($projectName)/_apis/build/builds?api-version=2.0"
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}
In your scenario: (Assume that Agent1 is the name of the agent, you can get it from Agent Capabilities)
Server1 : Agent1, Agent2
Server2 : Agent3, Agent4
If Build A queued with Agent1, then you an set "demands":["Agent.Name -equals Agent2"] to queue Build B.
Same thing, if Build A queued with Agent3, then you an set "demands":["Agent.Name -equals Agent4"] to queue Build B.
You can also create a simple PowerShell script and add a PowerShell step as the end of the build step in definition A, then run the PS script to queue build B once Build A completed.
If you want to queue build with the REST API in C#, then you can reference below thread:
Queue vNext build from c# using REST API (Tfs on premise)

Establish relationship between two Jenkins Jobs available on different Jenkins server

I am building Jenkins for Test / QA automation scripts, lets name it TEST_JOB. For application, I have application source code Jenkins build, name it DEV_JOB.
My scenario is when DEV_JOB completes execution (successfully), execute TEST_JOB immediately. I am aware about setting up project upstream / downstream [ Build after other projects are built ] to accomplish this task. But here, Problem is DEV_JOB is on different server than TEST_JOB. Due to which, TEST_JOB fails to recognize DEV_JOB.
Now, how would I achieve this scenario?
You can use Jenkins API for remote trigger of Job.
Say you have job on DEV_JOB on JENKINS_1, add a penultimate step(or upstream/downstream project having only this step) which invokes TEST_JOB using remote API call of JENKINS_2 server.
Example command would be
$(curl --user "username:password" "http://JENKINS_2/job/TEST_JOB/buildWithParameters?SOMEPARAMETER=$SOMEPARAMETER")
username:password is a valid user on JENKINS_2.
Avoid using your own account here but rather a 'build trigger' account that only has permissions to start those jobs.

Jenkins - Upstream project dependency issue

Here is something I want to achieve:
I have a jenkins project which has 4 upstream projects. But I don't want to trigger this project when the upstream jobs are done building, but I want the trigger the project via remote API, which then waits on upstream projects until they are done building, if these projects are building.
Lets say all the 4 upstream projects can build the source code from any branch passed via API, but I want the downstream project to start only when a specific branch is passed to these upstream projects.
Scenario:
Lets say I have two clusters A and B, for the sake of this question, I want to deploy my code to cluster A, i.e front end and backend code. Now I have a project to build front end and 1 project to build backend (these two projects can build code for cluster A and B, based on the branch passed). Now, I have two deploy projects for cluster A which will deploy front end and backend. So, when I pass a branch to build code for cluster A, it will trigger the build projects. But now I only want these two deploy projects to start when this specific branch was passed.
If you want to control the builds remotely then use the Jerkins cli - I have found it very useful http://jenkinshost:8080/cli
You need to get the ssh key config right, add the public key of the user running the cli to the user you want to run the job in Jenkins using the Jenkins user configuration (not on the command line
Test key setup with
java -jar jenkins=cli.jar -s http://jenkinshost:8080 who-am-i
This should then report which user will be used to run the build in Jenkins
But I think you can use the Conditional Build Step plugin for your problem
https://wiki.jenkins-ci.org/display/JENKINS/Conditional+BuildStep+Plugin
This will allow you to put a conditional wrapper around a build step i.e.
if branch==branchA then
trigger step - deploy to clusterA
if branch==branchB then
trigger step - deploy to clusterB
Personally I find this plugin a bit clunky and it makes the job config page a little messy
Another solution I came up with was to always call the child job and then let it decide if it runs.
So I have a script step at the start of the child job to see if it should run
if [${branch}="Not the right branch name" ] ; then
echo "EXIT_GREEN"
exit 1
fi
You have now failed this job which would cause the parent job to go red but by using the Groovy Postbuild plugin https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin you can add a post build step like this
if (manager.logContains(".*EXIT_GREEN.*")) {
manager.addBadge("info.gif","This job had nothing to do")
manager.build.#result = hudson.model.Result.SUCCESS
}
Child job has run green (with an info icon against the build) but has actually not done anything. Obviously if the branch is one you want deploy then the first script step does not run the exit 1 and the job continues as normal

Let Jenkins build project from a Mercurial commit

Is there a way to specify a hook in the single repository?
Now we have specified the hook in the "/etc/mercurial/hgrc" file, but every time it builds twice, and it builds for each commit in each repository.
So we want to specify a build per repository.
This is how we implemented the hook:
[hooks]
changegroup = curl --silent http://jenkins:8080/job/ourProject/build
It's on a Ubuntu server.
Select the Poll SCM option under Build Triggers.
Make sure that schedule form is empty.
You should be creating in the .hg directory, /home/user/mercurial/.hg/hgrc and add hooks as:
[hooks]
commit.jenkins = wget -q http://localhost:8080/mercurial/notifyCommit?url=file:///home/user/mercurial > /dev/null
incoming.jenkins = wget -q http://localhost:8080/mercurial/notifyCommit?url=file:///home/user/mercurial > /dev/null
You should make sure that
Your Jenkins project doesn't poll
You use the proper notifyCommit URLs for your Mercurial hooks: https://wiki.jenkins-ci.org/display/JENKINS/Mercurial+Plugin
Ok, I found what I looked for (I'm the bounty; my case is Mercurial with a specific branch).
In the main/origin repository, place a hook with your desired build script. Pregroupchange is to maintain the incoming changes. I have a rhodecode installed on the main repository and itself has its own hooks.
In this way, I still trigger Jenkins and still have the changes afther the trigger for rhodecode push notifications and others.
[hooks]
pregroupchange = /path/to/script.extention
In the script, place your desired actions, also a trigger for Jenkins. Don't forget to enable in Jenkins:Job:Configure:Build Triggers:checkbox Trigger builds remotely + put here your desired_token (for my case: Mercurial).
Because you can't trigger only to a specific branch in Mercurial, I found the branch name in this way. Also, to trigger from a remote script, you need to give in Jenkins read permission for anonymous overall, or create a specific user with credentials and put them into the trigger URL.
Bash example:
#!/bin/bash
BRANCH_NAME=`hg tip --template "{branch}"`
if [ $BRANCH_NAME = "branch_name" ]; then
curl --silent http://jenkins_domain:port/path/to/job?token=desired_token
fi
For the original question:
In this way you only execute one build, for a desired branch. Hooks are meant only for the main repository in case you work with multiple clones and multiple developers. You may have your local hooks, but don't trigger Jenkins from you local, for every developer. Trigger Jenkins only from the main repository when a push came (commit, incoming, and groupchange). Your local hooks are for other things, like email, logs, configuration, etc.

Resources