您好,登錄后才能下訂單哦!
小編給大家分享一下jenkins中怎么集成commander應用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
前言
Jenkins 是一款流行的開源持續集成(Continuous Integration)工具,廣泛用于項目開發,具有自動化構建、測試和部署等功能。
最近參加公司的集成測試平臺的開發,在開發中遇到了不少問題,兩個星期的迭代也即將完成,在這也用這篇博客記錄下開發中的問題,供讀者參考
公司的應用較多,所以需要了解這幾種應用在jenkins中如何做構建,我自己參與的有兩種commander的應用,一種是大數據類的,一個是我們服務端架構組的scala應用
1、大數據應用BigData
配置如下:
配置文件對應的xml文件:通過crul獲取xml配置文件:http://host/job/tar_py_dwx_dev/config.xml
<project> <actions/> <description/> <keepDependencies>false</keepDependencies> <properties> <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.5.5"> <gitLabConnection/> </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty> <hudson.plugins.promoted__builds.JobPropertyImpl plugin="promoted-builds@3.1"> <activeProcessNames> <string>Deploy DEV</string> </activeProcessNames> </hudson.plugins.promoted__builds.JobPropertyImpl> </properties> <scm class="hudson.plugins.git.GitSCM" plugin="git@3.8.0"> <configVersion>2</configVersion> <userRemoteConfigs> <hudson.plugins.git.UserRemoteConfig> <url>ssh://git@172.0.10.182:10022/bigdata/dwx.git</url> <credentialsId>84f4be19-ea8d-4271-8cfb-42af8f507285</credentialsId> </hudson.plugins.git.UserRemoteConfig> </userRemoteConfigs> <branches> <hudson.plugins.git.BranchSpec> <name>*/develop</name> </hudson.plugins.git.BranchSpec> </branches> <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations> <submoduleCfg class="list"/> <extensions/> </scm> <assignedNode>!macmini</assignedNode> <canRoam>false</canRoam> <disabled>false</disabled> <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding> <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding> <triggers> <hudson.triggers.SCMTrigger> <spec>H/5 * * * *</spec> <ignorePostCommitHooks>false</ignorePostCommitHooks> </hudson.triggers.SCMTrigger> </triggers> <concurrentBuild>false</concurrentBuild> <builders> <hudson.tasks.Shell> <command> project=dwx1 cd ${WORKSPACE} tar zcvf ${project}.tar.gz * aws s3 cp ${project}.tar.gz s3://lattebank-jenkins-build-dev/${JOB_BASE_NAME}/${BUILD_NUMBER}/ --region cn-north-1 rm -rf ${project}.tar.gz </command> </hudson.tasks.Shell> </builders> <publishers/> <buildWrappers/> </project>
從xml中獲取的信息有點和圖中的配置文件有點對應不上
對于promotion的腳本在xml配置文件中是無法獲取的,這時候就有一個問題,這種api是無法獲取到promotion的的腳本,同時這也給我們的工作帶來了極大的挑戰,那也意味著單純的通過這種方法是無法實現commander應用的部署,和通過平臺的方式去直接操作jenkins的配置
但經過查詢相關的api并不能找到相關的內容,經過不懈的努力,終于找到了和promote build 插件相關的api
查詢:http://host/job/jobName/promotion/process/promotionName/config.xml
這個接口能獲取到它的xml文件,但是并不能對該配置文件進行增加和修改
對此我自己封裝了一些方法:
/** * @author chenlang * date 2018/5/7 */ @Slf4j public class JenkinsPromotionUtils { private static final String SUB_PATH_PROMOTION_COOMMAND = "/hudson.plugins.promoted__builds.PromotionProcess"; private static final String SUB_PATH_BUILD = "/buildSteps"; private static final String SUB_PATH_BUILDER_SHELL_COMMAND = "/hudson.tasks.Shell/command"; private static final String PATH_PROMOTION_COMMAND = SUB_PATH_PROMOTION_COOMMAND + SUB_PATH_BUILD + SUB_PATH_BUILDER_SHELL_COMMAND; private static String CREATE_PROMOTION_JSON = "{'properties':{'stapler-class-bag':'true','hudson-plugins-promoted_builds-JobPropertyImpl':{'promotions':{'activeItems':{'name':'%s','isVisible':'','icon':'star-gold','hasAssignedLabel':false,'assignedLabelString':'','conditions':{'stapler-class-bag':'true'}}}}}}"; private static final String CONTENT_TYPE = "application/x-www-form-urlencoded"; public static void updatePromotionShell(Document jobConfigDocument, String jobName, JenkinsPromotionClient jenkinsPromotionClient, String promotionShell, String path) throws IOException, DocumentException { if (StringUtils.isBlank(promotionShell)) { return; } String promotionName = getPromotionName(jobConfigDocument, path); Document document = jenkinsPromotionClient.getJobPromotionXml(jobName, promotionName); document.selectSingleNode(PATH_PROMOTION_COMMAND).setText(promotionShell); jenkinsPromotionClient.updateJob(jobName, promotionName, document.asXML()); } public static void createPromotionShell(Document jobConfigDocument, String tmpJobName, String jobName, String promotionShell, String path, JenkinsPromotionClient jenkinsPromotionClient) throws IOException, DocumentException { if (StringUtils.isBlank(promotionShell)) { return; } String promotionName = getPromotionName(jobConfigDocument, path); Document document = jenkinsPromotionClient.getJobPromotionXml(tmpJobName, promotionName); document.selectSingleNode(PATH_PROMOTION_COMMAND).setText(promotionShell); Map<String, String> map = Maps.newHashMap(); map.put("Content-Type", CONTENT_TYPE); map.put("json", String.format(CREATE_PROMOTION_JSON, promotionName)); try { jenkinsPromotionClient.createJob(jobName, map); } catch (Exception e) { log.error("初創promotion時失敗" + e); } jenkinsPromotionClient.createJob(jobName, promotionName, document.asXML()); } public static String getPromotionName(Document jobConfigDocument, String path) { return jobConfigDocument.selectSingleNode(path).getText(); } }
package cn.caijiajia.phoenix.service.jenkins; import com.offbytwo.jenkins.client.JenkinsHttpClient; import com.offbytwo.jenkins.client.util.EncodingUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; import java.util.Map; /** * @author chenlang * date 2018/5/4 */ @Component public class JenkinsPromotionClient { @Autowired private JenkinsHttpClient jenkinsHttpClient; public JenkinsPromotionClient() { } /** * 獲取job的promotion配置文件 * * @param jobName job名稱 * @param promotionName promotion名稱 * @return * @throws IOException */ public Document getJobPromotionXml(String jobName, String promotionName) throws IOException, DocumentException { return DocumentHelper.parseText(this.getJobXml(jobName, promotionName)); } /** * 更新job * * @param jobName * @param promotionName * @param jobXml * @throws IOException */ public void updateJob(String jobName, String promotionName, String jobXml) throws IOException { this.jenkinsHttpClient.post_xml(this.toJobBaseUrl(jobName, promotionName) + "/config.xml", jobXml, true); } /** * 添加job腳本 * * @param jobName * @param jobXml * @throws IOException */ public void createJob(String jobName, String promotionName, String jobXml) throws IOException { this.jenkinsHttpClient.post_xml(this.toJobBaseUrl(jobName, promotionName) + "/config.xml", jobXml, true); } /** * 添加promotion的job * * @param jobName * @param map * @throws IOException */ public void createJob(String jobName, Map map) throws IOException { this.jenkinsHttpClient.post_form("/job/" + EncodingUtils.encode(jobName) + "/configSubmit?", map, false); } private String getJobXml(String jobName, String promotionName) throws IOException { return this.jenkinsHttpClient.get(this.toJobBaseUrl(jobName, promotionName) + "/config.xml"); } private String toJobBaseUrl(String jobName, String promotionName) { return "/job/" + EncodingUtils.encode(jobName) + "/promotion/process/" + promotionName; } /** * promotion腳本的構建 * @param jobName * @param promotionName * @param version * @param isFirstBuild * @throws IOException */ public void build(String jobName,String promotionName,Integer version,boolean isFirstBuild) throws IOException{ if (isFirstBuild) { this.jenkinsHttpClient.post("/job/"+ EncodingUtils.encode(jobName) + "/"+version+"/promotion/forcePromotion?name="+promotionName+"&json=%7B%7D&Submit=Force promotion"); } else { this.jenkinsHttpClient.post("/job/"+ EncodingUtils.encode(jobName) + "/"+version+"/promotion/"+promotionName+"/build?json=%7B%7D&Submit=Re-execute promotion"); } } }
其中的方法封裝了對promote build插件中關于配置的增刪改查,以及promotion腳本的構建
以上是“jenkins中怎么集成commander應用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。