序文
Jenkinsは、プロジェクト開発で広く使用されており、自動化された建設、テスト、展開などの機能を備えた人気のあるオープンソースの連続統合ツールです。
最近、私は会社の統合テストプラットフォームの開発に参加し、開発で多くの問題に遭遇しました。 2週間の反復が完了しようとしています。このブログを使用して、読者のリファレンスのために開発の問題を記録します。
同社には多くのアプリケーションがあるため、これらのアプリケーションをJenkinsで構築する方法を理解する必要があります。私は2種類の司令官アプリケーションに参加しました。1つはビッグデータクラス、もう1つはサーバーアーキテクチャグループのSCALAアプリケーションです。
1。ビッグデータアプリケーション
構成は次のとおりです。
構成ファイルに対応するXMLファイル:crul:http://host/job/tar_py_dwx_dev/config.xmlを介してXML構成ファイルを取得します
<project> <action/> <説明/> <keepdependencies> false </keepdependencies> <properties> <com.dabsquared.gitlabjenkins.connection.gitlabconnectionpropertyプラグイン= "[email protected]"> <gitlabconnection/</com.dabsquared.gitlabjenkins.connection.gitlabconnectionproperty> <hudson.plugins.promoted__builds.jobpropertyimplプラグインdev </string> </ActiveProcessNames> </hudson.plugins.promoted__builds.jobpropertyimpl> </properties> <scmプラグイン= "[email protected]"> <configversion> 2 </configversion> <userremoteconfigs> <hudson.plugins.git.userrem oteconfig> <url> ssh://[email protected]:10022/bigdata/dwx.git </url> <credentiencesid> 84f4be19-ea8d-4271 -8CFB-42AF8F507285 </credentienceSid> </hudson.plugins.git.userremoteconfig> </userremoteconfigs> <branches> <hudson> <hudson.plugins.git.branchspec> <name>*/develop </name> </hudson.plugins.git.branch.branchspec> </name es> <dogenerateubmoduleconfigurations> false </dogeneratesubmoduleconfigurations> <submodulecfg/> <extensions/</scm> <assignednode>!macmini </assidednode> <canroam> false </canroam> <disa> disa bled> <blockbuildwhendownstreambuilding> false </blockbuildwhendownstreambuilding> <blockbuildwhenupstreambuilding> false </blockbuildwhenupstreambuilding> <triggers> <hudson.triggers.scmtrigger> <pescgger> h/5 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *</spec> <ignorepostcommithooks> false </ingorepostcommithooks> </hudson.triggers.scmtrigger> </triggers> <concurrentBuild> false </concurrentBuild> <builders> <hudson.tasks.shell> <command> project = dwdx1 cd $ $ $ $ $ $ $ $ $ $ $ {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から取得した情報は、図の構成ファイルに対応していません。
プロモーションスクリプトは、XML構成ファイルで取得できません。現時点では、問題があります。このAPIはプロモーションスクリプトを取得できません。同時に、これは私たちの仕事に大きな挑戦をもたらします。また、司令官アプリケーションの展開をこの方法では実現できず、プラットフォームメソッドを介したジェンキンスの構成を直接操作できることも意味します。
ただし、関連するAPIを照会した後、関連するコンテンツを見つけることができません。免除の努力の後、最終的にAPIがプロモーションビルドプラグインに関連していることがわかりました
クエリ:http://host/job/jobname/promotion/process/promotionname/config.xml
このインターフェイスはXMLファイルを取得できますが、構成ファイルを追加または変更することはできません。
これのためのいくつかの方法をカプセル化しました:
/** * @author chenlang *日付2018/5/7 */ @slf4jpublic class jenkinspromotionutils {private static final string sub_path_path_ppath_path_path_path_promotion_coommand = "/hudson.plugins.promoted__builds.promotionprocess"; private static final string sub_path_build = "/buildsteps";プライベート静的な最終文字列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-plomoted_builds-jobpropertyimpl':{'protions':{'ActiveItems':{'name': '%s '、' isvisible ':' ''、 'icon': 'star-gold'、 'hasassignedlabel':fals、 'assignlabelstring': ''、 '条件':{'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(protionshell)){return; } string promotionname = getPromotionName(JobConfigDocument、Path); document document = jenkinspromotionclient.getjobpromotionxml(jobname、promotionname); document.selectsinglenode(path_promotion_command).settext(protionshell); jenkinspromotionclient.updatejob(jobname、promotionname、document.asxml()); } public static void createpromotionshell(document jobconfigdocument、string tmpjobname、string jobname、string promotionshell、string path、jenkinspromotionclient jenkinspromotionclient)Ioexception、documentexception {if(stringutils.isblank(promosionshell){return; } string promotionname = getPromotionName(JobConfigDocument、Path);ドキュメントdocument = jenkinspromotionclient.getjobpromotionxml(tmpjobname、promotionname); document.selectsinglenode(path_promotion_command).settext(protionshell); 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( "プロモーションの開始時に失敗しました" + e); } jenkinspromotionclient.createjob(jobname、promotionname、document.asxml()); } public static string getPromotionName(document jobconfigdocument、string path){return jobconfigdocument.selectsinglenode(path).getText(); }}パッケージ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.documentExcument; 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 *日付2018/5/4 */ @componentpublic class jenkinspromotionclient {@autowired private jenkinshttpclient jenkinshttpclient; public jenkinspromotionclient(){} / ***ジョブのプロモーション構成ファイルを取得** @paramジョブ名ジョブ名* @return* @throws ioexception* / public document getjobpromotionxmlプロモーション名)); } / ** * update job * * @param jobname * @param promoricname * @param jobxml * @throws ioexception * / public void updatejob(string jobname、string promotionname、string jobxml)throws ioexception {this.jenkinshttpclient.post_xml "/config.xml"、jobxml、true); }/** *ジョブスクリプトの追加 * * @param jobname * @param jobxml * @throws ioexception */public void createjob(string jobname、string promotionname、string jobxml)throws ioexception {this.jenkinshttpclient.post_xml(this gobname、jobname、promitionname) jobxml、true); }/** *プロモーションのジョブを追加 * * @param jobname * @param map * @throws ioexception */public void createjob(string jobname、map map)throws ioexception.post_form( "/job/" + compodingutils.encode) + "/configsubitmit? } 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; } / ** *プロモーションスクリプトの構築 * @param jobname * @paramプロモーション名 * @paramバージョン * @param isfirstbuild * @throws ioexception * / public void build(string jobname、string promotionname、integerバージョン、boolean isfirstbuild)throws ioexcepti this.jenkinshttpclient.post( "/job/"+encodingutils.encode(jobname)+"/"+version+"/promotion/forcepromotion?name ="+promotionname+"&json =%7d&submit = force promotion"); } else {this.jenkinshttpclient.post( "/job/"+encodingutils.encode(jobname)+"/"+version+"/"/"+promotionname+"/build?json = 7b%7d&submit = rexecute promotion "); }}}このメソッドは、プロモーションスクリプトの構築だけでなく、プロモーションプラグインの構成の追加、削除、変更、クエリをカプセル化します。
要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。