How to promote a specific build number from another job in Jenkins? - build

I installed the Promoted Build Plugin from Jenkins and now I'm facing some troubles to promote a build from an existing job. Here is the scenario:
There is an existing Nightly Build job that runs every night running all the tests and metrics needed;
There is an existing Deploy Build that accepts a parameter ${BUILD_NUMBER} and deploys the build that has the corresponding ${BUILD_NUMBER} from the Nightly Build
Say the [Nightly Build] ran and successfully built the artifact #39
Now I can just run the [Deploy Build] passing in #39 as a parameter
The artifacts from [Nightly Build] #39 are going to be deployed
So far so good. Now is the part where I want to add the Build Promotions...
Is there a way to promote the Nightly Build #39 (notice that it was already built before) from the Deploy Build? Or maybe even from somewhere else, quite frankly I`m kind of lost here :(
I don`t see them with a clear Upstream/Downstream relationship, because they don't have a: always runs this build and then the other during the execution - the [Deploy Build] is executed sometimes only and not always after the [Nightly Build].

Update as of version 2.23 of Parameterized Trigger Plugin:
With version 2.23+ behavior changed (thanks AbhijeetKamble for pointing out). Any parameter that is being passed by Predefined Parameters section of calling (build) job has to exist in the called (deploy) job. Furthermore, the restrictions of called job's parameters apply, so if the called job's parameter is a choice, it has to have all possible values (from promotions) pre-populated. Or just use Text parameter type.
Solution
Yes, I have the exact same setup: a build job (based on SVN commits) and manually executed deploy job. When the user selects any build from the build job (including older builds), they can then go to Promotion Status link and execute various deploy promotions, for example Deploy to DEV, Deploy to QA, etc
Here is how to setup the promotion on build job:
You will need these plugins: Parameterized Trigger Plugin, Promoted Builds Plugin
You will also need to setup default Archive the Artifacts post-build action on this build job.
Check mark Promote builds when
Define Name "Deploy to DEV"
Under Criteria check mark Only when manually approved
Under Actions use Trigger/call builds on other projects
In Projects to build enter the name to your deploy job here
Check mark Block until the triggered projects finish their builds
Mark this build as failure if the triggered build is worse or equal to: FAILURE (adjust according to statuses of your deploy job)
Predefined parameters (Code A)
Code A:
Server=IP_of_my_dev_server`
Job=$PROMOTED_JOB_NAME`
BuildSelection=<SpecificBuildSelector><buildNumber>$PROMOTED_NUMBER</buildNumber></SpecificBuildSelector>
Above, in the Predefined parameters section, the name to the left of = are the parameters that are defined in your deploy job. And to the right of = are the values that will be assigned to those parameters when this promotion executes. Defines three parameters Server, Job and BuildSelection.
The parameter Server= is my own, as my deploy job can deploy to multiple servers. However if your deploy job is hardcoded to always deploy to a specific location, you won't need that.
The Job= parameter is required, but the name of the param depends on what you've setup in your deploy job (I will explain configuration there). The value $PROMOTED_JOB_NAME has to remain as is. This is an environment variable that the promotion process is aware of and refers back to the name of your build job (the one where promotion process is configured)
The BuildSelection= parameter is required. This whole line has to remain as is. The value passed is $PROMOTED_NUMBER, which once again the promotion is aware of. In your example, it would be #39.
The Block until the triggered projects finish their builds check mark will make the promotion process wait until the deploy job finished. If not, the promotion process will trigger the deployment job and quit with success. Waiting for the deploy job to finish has the benefit that if the deploy job fails, the promotion star will be marked with failure too.
(One little note here: the promotion star will appear successful while the deploy job is running. If there is a deploy failure, it will only change to failure after the deploy job finished. Logical... but can be a bit confusing if you look at the promotion star before the deployment completed)
Here is how to setup deploy job
You will need Copy Artifacts plugin
Under This build is parameterized
Configure a parameter of type Choice (or Text) with name Server (this name has to match with configuration in promotion's Predefined Parameters in previous section)
Choices: Enter list of possible server IPs that would be used by the promotion's Predefined Parameters in previous section (see update note below)
Configure a parameter of type Choice (or Text) with name Job (this name has to match with configuration in promotion's Predefined Parameters in previous section)
Choices: Enter the name of your build job as default. This is only needed if you trigger the deploy job manually. When the deploy job is triggered from promotion, the promotion will supply the value (the Job= from Predefined parameters that we configured). Also, if there is no value passed from promotion's Predefined parameters, the first choice value will be used. If you have a 1-to-1 relationship between the build and deploy jobs, you can omit the Job= parameter in promotion's configuration.
Update: since version 2.23 of Parameterized Trigger, the available choices in the deploy job configuration have to have all possible values coming from the promotion's predefined parameters. If you don't want that limit, use "Text" instead of "Choice"
Configure a parameter of type Build selector for Copy Artifact with name: BuildSelection
Default Selector: Latest successful build
Under Build steps
Configure Copy artifacts from another project
In Project name enter ${Job}
At Which build choose Specified by a build parameter
In Parameter Name enter BuildSelection (without ${...}!)
Configure the rest accordingly for your artifacts that will be copied from build job to deploy job's workspace
Use the copied artifacts inside the deploy job as you need in order to deploy
So now, with the above deploy job, you can run it manually and select which build number from build job you want to deploy (last build, last successful, by build number, etc). You probably already have it configured very similarly. The promotion on the build job will basically execute the same thing, and supply the build number, based on what promotion was executed.
Let me know if you got any issues with the instructions.

Marked answer is great explanation for the question. But I would like to suggest a solution for those people looking for "how-to-promote-a-specific-build-number-from-another-job-in-jenkins"
We can use a generalized solution for doing force promotion using CURL and REST API. You can execute curl from Shell or Groovy scripts.
Shell Solution using CURL:
user_name="jenkins_user"
user_token="token"
promotion_name="Test_Promote"
jenkins_url="http://build-server.com"
JOB_NAME="job_name"
JOB_NO="job-no"
url="--silent -u $user_name:$user_token $jenkins_url/job/$JOB_NAME/$JOB_NO/promotion/forcePromotion?name=$promotion_name"
curl $url
Groovy Soultion:
user_name="jenkins_user"
user_token="token"
promotion_name="Test_Promote"
jenkins_url="http://build-server.com"
JOB_NAME="job_name"
JOB_NO="job-no"
def response = "curl -u $user_name:$user_token \" $jenkins_url/job/$JOB_NAME/$JOB_NO/promotion/forcePromotion?name=$promotion_name".execute().text
How to generate jenkins user token: https://jenkins.io/blog/2018/07/02/new-api-token-system/

Related

How to have a simple manual ECS deployment in CodePipeline / CodeDeploy?

Basically I would like to have a simple manual deploy step that's not directly linked to a build. For use cases, when using containers, I wouldn't like to perform a build separately per environment (eg: once my build puts an image tag in ECR, I would like to deploy that to any number of environments).
Now, I know in CodePipeline I can have a number of actions and I can precede them with manual approval.
The problem with that is that should I not want to perform the last manually approved deploy, subsequent executions will pile on - the pipeline execution doesn't complete and what comes next will just have to wait. I can set a timeout, for sure, but there are moments when 20 builds come in fast and I don't know which one of them I may want to deploy to which environment (they generally all go to some QA/staging, but some need to manually deployed to a particular dev-related environment or even to production).
Manually updating task definitions all around in ECS is tedious.
I have a solution where I can manually patch a task definition using awscli and yq but is there a way to have a simple pipeline with one step that takes a manual input (aka image tag) and either uses an ECS deploy step (the only place where you can provide a clean straight patch json to patch the task definition) or uses my yq script to deploy?

How to set a TeamCity build trigger to use an environment parameter

I have a TeamCity environment parameter (env.Environment) that can be set to the particular environment you can run the build against i.e. UAT, DEV
During TeamCity Build triggers setup, there are no options to select the parameters you want the triggered build to run against. When I run the build manually, I get the option to select the parameters to run against but not when setting a trigger, any ideas where/how this can be set?
There are multiple workarounds to achieve it but you cannot implement this directly in the TeamCity triggers.
The environment that you have created just change the display value to "Prompt" in the specs. But I believe it will pause the build. So whenever the build gets triggered on the scheduled time then you need to select value in the prompt.
The most efficient way that I could think of is trigger the build with the help of TC rest API. In the request payload, you can send the desired parameter value as well. So whatever the condition is just handle those things in the custom script written in any programming language.
one more (not really beautiful) way - when build started by trigger, teamcity parameter teamcity.build.triggeredBy.username is equals "Schedule Trigger"
so you can check its value in first step of build in command line and then redefine needed params
if [ %teamcity.build.triggeredBy.username% == "Schedule Trigger" ]; then
echo "##teamcity[setParameter name='env.PARAM' value='VALUE']"
fi

Is there a Jenkins pipeline equivalent of "Only show latest build status"?

We're in the process of migrating to Jenkins pipeline. We use Jenkins to update the build status of our branches/PRs in Bitbucket.
One of our merge checks in Bitbucket is "Check the last commit for at least 1 successful build and no failed builds". This means that we can't merge unless there is 1 successful build of the latest commit on Jenkins, and no failed builds of the latest commit.
Sometimes, Jenkins builds fail for reasons unrelated to the code and the build is fine next time you run it. However, this means that the branch can't be merged as there has been at least one failed build.
To get around this, we were previously able to tick the box in the build status notifier plugin configuration that said "Only show latest build status". As of yet, we're unable to find any equivalent way to configure this in a Jenkinsfile. Does anyone know if it's possible, and if so how?
This question is a duplicate of Multi branch pipeline with bitbucket build status notifier plugin in jenkins, but that question was never really answered.
It seems that your use case it's covered by the Build REST API of Bitbucket, and there is even a step-by-step example of your exact case, being last build failed for external reasons, and a new build succeeds
In order to overwrite the status of an already reported build, you will need to use the same commit sha and the same key
Regarding the Bitbucket Build Status Notifier, it allows you to specify a buildKey as an optional parameter. You should make sure to specify this parameter, and that the parameter is always the same for all builds of the same job.
That way, a new build of the same last-commit will overwrite the last one, as it will be using the same sha and the same key.
See the API section of the plugin documentation for additional parameters.

How to test using a variety of build options with AWS CodeBuild

I am using AWS CodeBuild as a replacement for travis-ci.org. (Travis-CI was just too slow for our needs.) I would like to have CodeBuild test a variety of different configurations:
static vs shared libraries
gnu vs llvm compilers
debug vs release optimizations
I have a very simple buildspec.yml file that works well, but it doesn't seem to be able to generate a matrix of options like I can do on Travis-CI.
I could create a variety of buildspec.yml files with each one doing something a little differently, but that quickly gets out of hand with the different options that need to be considered.
What is the right/best way to do this with AWS CodeBuild?
I am from the AWS CodeBuild team. Thanks for your feedback and feature request. We'll pass this to our product management team to consider for future roadmap.
As you said, one option would be creating multiple buildspec.yml files and using overriding per build based on the desired build config.
Alternatively, you could have a single buildspec.yml that executes different build scripts based on an environment variable. For example you could have an environment variable called BUILD_CONFIG whose value will be read by your build script to determine which build configuration to use.
Environment variables can be overriden per build. Use the environmentVariablesOverride field in StartBuild:
http://docs.aws.amazon.com/codebuild/latest/APIReference/API_StartBuild.html#CodeBuild-StartBuild-request-environmentVariablesOverride

Teamcity chain multiple build promotions

Im trying to figure out how to chain multiple 'promotions' (by a user clicking) whilst ensuring that ever build in the chain is not queued. By current setup is as follows, NOTE as my application is a white label the configuration described below is repeated for every site.
Build & Test - Creates zipped artifact
Deploy to Testing - Has artifact and snapshot dependency
Deploy to Staging - Has artifact and snapshot dependency
Deploy to Production Has artifact dependency
When promoting to production i want to do this across all websites (without having to manually click promote on each build).
I am currently trying the following strategy, to set the 'deploy to production' build to have a Artifact dependency, without a snapshot dependency so it doesn't queue down the chain. I have set the artifact to depend on the 'Build & Test' configuration to gain access to the zipped project and i have set it to build with a specific build number referencing a parameter in the production build.
After doing some googleing i found out that i am able to get the stagings build number using the rest api as follows:
http://teamcity_url/httpAuth/app/rest/builds/buildType:build_configuration_id/resulting-properties/build.number
And this works great, however i don't understand how i can get this value into the parameter?
Also i dont know if my approach is correct? is there a better way?
Set up the artifact dependencies chronologically (Build -> Test -> Staging -> Production) and all your snapshot dependencies to Build & Test. Depending on exact needs you might have a snapshot dependency on both Build and the one your artifact dependency is on.
Also make sure you enable "Do not run new build if there is a suitable one. This should keep it from queuing down the chain without intention.
Using the build chain tab will be important because the main project page only shows the last build ran. So clicking run from there will que the chain because you are asking for a new build, even though to you it might feel like your asking for the next step to be ran. The build chain tab helps keep things clear.