TFS Build : How to identify previous successful build number? - tfs

In TFS Build pipeline: How to identify previous successful build number?

If you want to check previous successful build from a build definition, you could check History of a build definition.
If you want to check previous successful build from a build agent, you could check Requests for this agent in Agent Queue or Agent Pool.
Also, REST API could get a list of successful build easily. The API is similar to the format below:
GET https://{instance}/DefaultCollection/{project}/_apis/build/builds?api-version=2.0&resultFilter=succeeded

Related

BitBucket merge check to allow merge PR when ONLY LAST build is successful

We have a repo in BitBucket Server with "Minimum successful builds" merge check enabled and set to 1. When PR is opened the build starts and result of that build returns back to BitBucket.
Sometimes build fails (out of memory, etc) we run build again manually (without new commits). In that case PR contains two builds: first is "Failed" and the second is "Passed", but "Minimum successful builds" merge check still can't allow us to merge because it require to all builds be successful.
It is possible to check only last build be successful to allow PR merge?
So, the only solution I've found so far is changing the build name which Jenkins sends to Bitbucket. The main idea is that the build name must always be the same for that particular Pull Request. In that case, PR build name on Bitbucket is overridden each time when a new build of the PR starts by Jenkins.
In our case, I just removed Jenkins's BUILD_NUMBER from the build name, which it sent to Bitbucket.
The only drawback of that solution I've found is that you can only see last build status of the PR without any build history.

Manual promotion of pipeline results in Jenkins

We are currently moving from our legacy build server to Jenkins.
In our previous system, we had the following system:
You don't directly build a release version, but you build a "BETA".
After you are satisfied with the result, you can promote the "BETA" to "RELEASE".
For that, you click on the build (the actual pipeline run) and click on a button "BETA to RELEASE".
This button triggers a script to do the actual promotion (the actual steps are irrelevant for this question).
For Jenkins, I haven't found something like that yet. To be precise: I would like to choose one of the successful builds and then click on some kind of button to promote the results of that build to RELEASE. So even if I have build 1.2.0-BETA, 1.2.1-BETA and 1.2.2-BETA, I can choose 1.2.1-BETA and release it to 1.2.1.
What would be a good approach for this?
I would like to suggest the following
We can create a Jenkins pipeline for an Application (Ex: API Services). In this API deployment pipeline, there are many stages,
Get source code from GIT
Restore the dependent libraries
Build the solution & generate the deployment artifacts (with suitable version , ex: Beta 1)
Deploy the artifacts to Dev environment
We have a wait time (ex: 1 day) using a timeout in Jenkins pipeline
Once the dev team validates the build and finds that it is suitable for promotion to QA
they click on Approve
Approved build gets updated with the version (ex: QA-1)
Await QA team approval
Once approved, move to production.
Reference Link: https://jenkins.io/doc/pipeline/steps/pipeline-input-step/
Example Snippet of pipeline code
stage("Validate before Apply") {
timeout(time:30, unit:'MINUTES') {
input 'Are you sure to promote this build to QA?'
}
}
I think that Promoted Builds Plugin is what you need.
Basically, after installing the plugin, you create a job that build your BETA version and in its configuration, you set up a promotion, manually approved, that run your promotion script.
Depending of your need, you maybe configure as well the number of days you want to keep the builds of the job.

Jenkins: distinguish CI build from scheduled build

All my Jenkins jobs are triggered both by a Github webhook, but also via a scheduled build one per week. The build process is heavily cached to make the webhook CI builds finish quickly.
I would like to add a line to my build script which wipes the cache during the weekly scheduled build, and make it build from scratch. Is there a variable in the build script to identify if a build was triggered by a webhook or schedule?
Maybe the envInject plugin will give you what you need?
This plugin also exposes the cause of the current build as an
environment variable. A build can be triggered by multiple causes at
the same time e.g. an SCM Change could have occurred at the same time
as a user triggers the build manually.
The build cause is exposed as a comma separated list:
BUILD_CAUSE=USERIDCAUSE, SCMTRIGGER, UPSTREAMTRIGGER, MANUALTRIGGER
In addition, each cause is exposed as a single envvariable too:
BUILD_CAUSE_USERIDCAUSE=true
BUILD_CAUSE_SCMTRIGGER=true
BUILD_CAUSE_UPSTREAMTRIGGER=true
BUILD_CAUSE_MANUALTRIGGER=true

How can I use Jenkins in MS VSTS to build Pull Requests?

I tried to connect my Jenkins server to VSTS repo by following this Link.
https://blogs.msdn.microsoft.com/devops/2017/04/25/vsts-visual-studio-team-services-integration-with-jenkins/
However, the options are different from the tutorial.
There is no way to set a rule for Pull Request, which is what I am trying to do.
I set a new build definition to make the Jenkins build work on every commit, but doesn't work for Pull Request.
To queue Jenkins job for VSTS pull request build validation, you can follow below steps:
Create a job in Jenkins
First, you need to create a job in Jenkins. If you have already create, then skip this step.
Create and configure build definition for PR build validation
Seems you already created a build definition named Jenkins, then add
Jenkins Queue Job task.
If you didn’t configure your Jenkins as an endpoint in your VSTS project, you can click New button to specify server URL, username and password to login the Jenkins.
Then specify the Jenkins job for queuing.
Note: If your Jenkins setup as a local server (the url as http://localhost:8080), then you need to queue VSTS build by private agent which also located in the same machine.
Add build policy as the target branch policy
In the build policy, you can set the Trigger as Automatic, the Policy requirement as Required, and the Build expiration is Immediately.
Now when pull request is created or updated for merging into the target branch, VSTS build will be triggered immediately, and the Jenkins job will also be queued during VSTS build.

Send command from TeamCity to run automation tests on Jenkins

Is there a way to add a build step in TeamCity which sends a request to Jenkins server, run some automation test scripts in Jenkins and sends back a response to Teamcity.
The idea basically is to automate the whole deployment process which also includes running of some automation tests created using python scripts (which will be done on Jenkins).
I am not sure if this is the best way of doing it but are there any better ways to achieve this? Also any hints on how to send command from Teamcity to Jenkins?
You can make an http request, as mentioned in the comment to start tests on Jenkins.
As for publishing the results bach to TeamCity, the possible solution might be:
after tests are done on Jenkins, publish the results that can be accessed externally (by TeamCity) and interpreted / reported by TeamCity (either in any of the supported formats), or manually, by the script, that will be run by TeamCity, using service messages
create a build configuration that will process the tests after the Jenkins build
set up a URL build trigger plugin, configure the trigger for the created build configuration. Point the trigger to the address where results are published. As soon as the content published is changed, the build will start and you will be able to download the tests results to TeamCity and process them
Got an easy to implement solution for the first part i.e. sending command from Teamcity to Jenkins
Using CURL:
Install/copy Curl to the Teamcity agent.
and then in your TC build configurations, create a new Command line build step similar to below (modify the parameters to your needs)
curl --user %jenkins_user%:%jenkins_pwd% -X POST http://%jenkins_instance_withport%/job/%jenkins_jobs_name%/buildWithParameters?token=%jenkins_token% --data "Build_Number=%build.number%"
e.g: curl --user admin:password -X POST http://jenkinssever:2123/job/test-build-image/buildWithParameters?token=rtbuild --data "Build_Number=1.2.0"
Here i could even pass the build number to Jenkins by using "-- data"
Do the below under Jenkins build configuration:
In Jenkins configuration:
In Jenkins configuration, update the below values:
"This project is parameterized"
Name: Build_Number
Default Value: 1.2.0
"Trigger builds remotely"
Authentication Token: rtbuild
Optional: for setting build number
"Set Build Name"
Build Name: #${Build_numuber}
done and you are good to go.please do let me know you if you have more questions.
the above is the implementation of the Initial comment
I think I found a way while trying to solve similar use-case, did it for batch files in Teamcity build steps. for Jenkins, we have to modify accordingly.
Also is there any specific reason for using Teamcity and Jenkins simultaneously, unless you are making use of already created Jenkins build?
Steps:
Get CLI based command for Jenkins:
https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
you can achieve in two ways
Method 1:
As build step is in current build.
Create a build step before your current step and trigger the Jenkins build using CLI
Based on the return value of the Jenkins build step, next step will execute
Method 2:
create a new build with above CLI step and add a dependency in your primary build.
so whenever the primary build is started, it will start the dependent CLI jenkins build. and once the dependent build is completed, will return success/failure, based on that the primary build will start.
i haven't tested the CLI of Jenkins but as Teamcity supports the steps and dependencies structure, expecting this will work.
will keep posted once i implement it.

Resources