jekins 指定分支_是否有可能只从一个特定分支触发Jenkins?

My situation is the following:

I have three branches in a repo: master, dev and staging.

And I have one job for each one of these branches, configured in 'Branches to build' section in Jenkins. origin/master, origin/dev, origin/staging.

Bitbucket triggers the job to build whenever there are changes to the repository via a repository hook .(https://confluence.atlassian.com/display/BITBUCKET/Jenkins+hook+management).

However, when I push to master, all jobs starts to build, and the same with the other two ones.

I want Jenkins "master" job to be built only when I push to master branch. Jenkins "dev" job to dev branch. Jenkins "staging" job to dev staging.

Is there a way to control this behaviour?

解决方案

I just discovered that Bitbucket does not allow to choose specific hook on pushing to any branch. It just calls all the hooks, then starts all Jenkins jobs.

My solution was to create an specific file on my machine which Jenkins is installed and set a Bitbucket hook to this file. (e.g. http://{jenkins url}:{apache port}/check.php)

Note that this apache port is not the same of Jenkins', but Apache's. In my case, Jenkins was running at 8080 and Apache at 7777. It did this to run php script, but not in Jenkins' directory.

Since Bitbucket hook sends a json file, I was able to verify in check.php which branch has been pushed on.

Reference: POST hook management

After the verification using a simple 'if', I just called the right url to start the right job with exec_curl, like:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, http://{jenkins url}:{jenkins port}/job/{job name}/build?token={job token});

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);

curl_close($ch);

And voilà.

Jenkins中设置针对不同代码分支使用特定应用包进行构建,通常需要以下几个步骤: 1. **配置Git或源码管理**: 首先,你需要在Jenkins中添加你的项目并关联到Git仓库。确保每个分支都有对应的Git URL。 2. **创建构建触发器**: 在项目的"Source Code Management"部分,选择合适的触发器,如Poll SCM(定时自动构建),以便于根据代码提交自动触发构建。 3. **环境变量或构建脚本**: 对于不同的分支,你可能需要使用不同的包版本。可以利用Jenkins的预处理器插件(如EnvInject或Script Path Builder)来动态设置环境变量,比如`BRANCH_NAME`,然后在构建阶段通过条件判断来加载对应的包。 4. **构建脚本**: 编写你的构建脚本(通常是使用Shell、Python、Groovy等),检查当前的`BRANCH_NAME`,然后根据需要安装相应的应用包。例如,使用Dockerfile可以在构建时基于不同的分支切换镜像。 ```groovy if (env.BRANCH_NAME == 'feature') { sh 'npm install package-for-feature' } else if (env.BRANCH_NAME == 'master') { sh 'npm install package-for-master' } ``` 5. **条件构建步骤**: 可以将应用包安装作为条件步骤,只在特定分支上运行。这可以通过使用`Conditional Build Step plugin`或者在Jenkinsfile中使用`when`块来实现。 6. **保存和部署**: 完成上述设置后,记得保存你的配置,并测试一下不同分支是否能正确使用对应的包进行构建。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值