Préface
Jenkins est un outil d'intégration continue open source populaire qui est largement utilisé dans le développement de projets et possède des fonctions telles que la construction, les tests et le déploiement automatisés.
Récemment, j'ai participé au développement de la plate-forme de test intégrée de l'entreprise et j'ai rencontré de nombreux problèmes dans le développement. L'itération de deux semaines est sur le point d'être achevée. J'utiliserai ce blog pour enregistrer les problèmes dans le développement de la référence des lecteurs.
L'entreprise a de nombreuses applications, nous devons donc comprendre comment créer ces applications dans Jenkins. J'ai participé à deux types d'applications de commandant, l'une est la classe Big Data, et l'autre est l'application Scala de notre groupe d'architecture de serveur.
1. Application Big Data
La configuration est la suivante:
Le fichier XML correspondant au fichier de configuration: Obtenez le fichier de configuration XML via CRUL: http: //host/job/tar_py_dwx_dev/config.xml
<PROJET> <Actions /> <Description /> <KeepDependces> False </ KeepDependces> <Properties> <com.dabsquared.gitlabjenkins.connection.gitlabconnectionproperty Plugin = "[email protected]"> <gitlabconnection / </ com.dabsquared.gitlabjenkins.connection.gitlabconnectionproperty> <Hudson.plugins.promoted__builds.jobpropertyimplnde> "[email protected]"> <cactiveProcessNames> Dev </string> </ activeProcessNames> </hudson.plugins.promoted__builds.jobpropertyimpl> </properties> <Scm plugin = "[email protected]"> <configVersion> 2 </FIGIGVERSION> <UserremoteConfigs> <Hudson.plugins.git.userrem Oteconfig> <url> ssh: //[email protected]: 10022 / bigdata / dwx.git </url> <idementialsid> 84f4be19-ea8d-4271 -8cfb-42af8f507285 </identialsid> </hudson.plugins.git.usermoteconfig> </serremoteconfigs> <cranches> <hudson.plugins.git.branchspec> <name> * / Développement </name> </hudson.plugins.git.branchSpec> es> <ogeneraSubmoduleconfigurations> false </ogeneraSubmoduleconfigurations> <submodulecfg /> <extensions / </cm> <attadedNode>! macmini </sadNode> <Canroam> Faux </ canroam> <isabled> false </sadNode> Bled> <blockbuildwhendownstreambuilding> false </ blockbuildwendownstreambuilding> <blockbuildwhenupstreambuilding> false </ blockbuildwhenupstreambuilding> <criggers> <hudson.triggers.scmtrigger> <spec> h / 5 * * * * * * * * * * </ spec> <ignorepostcomithooks> false </ ignorepostcomithooks> </hudson.triggers.scmtrigger> </priggers> <handson.tasks.shell> </currentBuil * 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>
Les informations obtenues à partir du XML ne correspondent pas au fichier de configuration de la figure.
Les scripts de promotion ne peuvent pas être obtenus dans le fichier de configuration XML. Pour le moment, il y a un problème. Cette API ne peut pas obtenir les scripts de promotion. Dans le même temps, cela apporte également de grands défis à notre travail. Cela signifie également que le déploiement de l'application Commandant ne peut pas être réalisé via cette méthode et que la configuration de Jenkins via la méthode de la plate-forme peut être exploitée directement.
Cependant, après avoir interrogé les API pertinentes, nous ne pouvons pas trouver le contenu pertinent. Après des efforts inlassables, nous avons finalement trouvé l'API liée au plug-in de promotion de promotion
Query: http: //host/job/jobname/promotion/process/promotionname/config.xml
Cette interface peut obtenir son fichier XML, mais il ne peut pas ajouter ou modifier le fichier de configuration.
J'ai encapsulé certaines méthodes pour cela:
/ ** * @Author Chenlang * Date 2018/5/7 * / @ slf4jpublic class jenkinspromotionUtils {private static final String sub_path_promotion_coommand = "/hudson.plugins.promoted__builds.promotionprocess"; chaîne finale statique privée sub_path_build = "/ buildSteps"; chaîne finale statique privée sub_path_builder_shell_command = "/hudson.tasks.shell/command"; chaîne finale statique privée path_promotion_command = sub_path_promotion_coommand + sub_path_build + sub_path_builder_shell_command; chaîne statique privée create_promotion_json = "{'Properties': {'Stapler-Class-Bag': 'True', 'Hudson-Plugins-Promoted_Builds-JobpropertyImpl': {'Promotions': {'ActiveItems': {'name': '% s ',' isvisible ':' ',' icon ':' star-gold ',' HassignedLabel ': false,' attributedLabelstring ':' ',' Conditions ': {' Stapler-Class-bag ':' true '}}}}} "; chaîne finale statique privée contenu_type = "application / x-www-form-urlencoded"; public static void updatePromotionShell (document jobconfigDocument, string jobname, jenkinspromotionclient jenkinspromotionclient, string promotionshell, string path) lance 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) lance 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 ("contenu-type", contenu_type); map.put ("json", string.format (create_promotion_json, promotionName)); essayez {jenkinspromotionclient.createjob (jobName, map); } catch (exception e) {log.error ("a échoué lors du démarrage d'une 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; Importation; org.dom4j.DocumentHelper; import org.springframework.beans.factory.annotation.autowired; import org.springframework.sterreotype.component; import java.io.ioexception; import java.util.map; / ** * @Author Chenlang * Date 2018/5/4 * / @ ComponentPublic class jenkinspromotionClient {@autowired privé jenkinshttpclient jenkinshttpclient; public jenkinspromotionclient () {} / ** * Obtenez le fichier de configuration de promotion du travail * * @param jobname nom du travail * @param promotionname nom de promotion * @return * @throws ioException * / public document getjobpromotionxml (String jobname, string promotionName) lance ioexception, documentException {return DocumentHelper.ParseText (this.getJobxml (JobName, promotionName)); } / ** * Mise à jour du travail * * @param jobname * @param promotionname * @param jobxml * @throws ioException * / public void updateJob (String jobname, string promotionname, string jobXml) lance ioException {this.jenkinshttpclient.post_xml (this.tojobbaserl "/config.xml", jobxml, true); } / ** * Ajouter un script de travail * * @param jobname * @param jobxml * @throws ioException * / public void createjob (String jobname, string promotionname, string jobxml) lance ioexception {this.jenkinshttpclient.post_xml (this.tojobbaseUl (jobname, promotionname) + "/config.," jobname, promotionname) + " jobXml, true); } / ** * Ajouter un travail de promotion * * @param jobname * @param map * @throws ioException * / public void createjob (String jobname, map map) lance ioException {this.jenkinshttpclient.post_form ("/ job /" + encodingutils.encode (jobname) + "/ configSubMit?", Map, false); } private String getJobxml (String jobName, String promotionName) lève 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; } / ** * Construction de scripts de promotion * @param jobname * @param promotionname * @param version * @param isFirstBuild * @throws ioexception * / public void build (String jobname, string promotionName, integer version, boolean isFirstBuild) throw this.jenkinshttpclient.post ("/ job /" + Encodingutils.encode (jobName) + "/" + version + "/ promotion / forcepromotion? name =" + promotionname + "& json =% 7b% 7d & soumed = force promotion"); } else {this.jenkinshttpclient.post ("/ job /" + EncodingUtils.encode (jobName) + "/" + version + "/ promotion /" + promotionname + "/ build? json =% 7b% 7d & soumed = réexécuter promotion"); }}}La méthode résume l'ajout de configuration, la suppression, la modification et la requête du plug-in de construction promouvoir, ainsi que la construction du script de promotion.
Résumer
Ce qui précède est l'intégralité du contenu de cet article. J'espère que le contenu de cet article a une certaine valeur de référence pour l'étude ou le travail de chacun. Si vous avez des questions, vous pouvez laisser un message pour communiquer. Merci pour votre soutien à wulin.com.