Vorwort
Jenkins ist ein beliebtes Open -Source -Integrationstool, das in der Projektentwicklung häufig verwendet wird und Funktionen wie automatisierter Konstruktion, Test und Bereitstellung hat.
Kürzlich habe ich an der Entwicklung der integrierten Testplattform des Unternehmens teilgenommen und auf viele Probleme in der Entwicklung gestoßen. Die zweiwöchige Iteration steht kurz vor dem Abschluss. Ich werde diesen Blog verwenden, um die Probleme in der Entwicklung für die Referenz der Leser aufzuzeichnen.
Das Unternehmen hat viele Anwendungen, daher müssen wir verstehen, wie diese Anwendungen in Jenkins erstellt werden. Ich habe an zwei Arten von Commander -Anwendungen teilgenommen, eine ist die Big -Data -Klasse und die andere ist die Skala -Anwendung unserer Serverarchitekturgruppe.
1. Anwendung Big Data
Die Konfiguration ist wie folgt:
Die XML -Datei, die der Konfigurationsdatei entspricht: Rufen Sie die XML -Konfigurationsdatei über cRUL: http: //host/job/tar_py_dwx_dev/config.xml ab
<Project> <Aktionen/> <beschreibung/> <weitdependencies> false </keepDependenzen> <properties> <com.dabSquared.gitlabjenkins.Connection.gitlabConnectionProperty plugin = "[email protected]"> <gitlabConnection/</com.dabSquared.gitlabjenkins.Connection.gitlabConnectionProperty> <hudson.plugins.promoted__builds.jobpropertyimpl Plugin = "[email protected]"> < Dev </string> </activeProcessnames> </hudson.plugins.Promoted__builds.jobpropertyImpl> </properties> <scm plugin = "[email protected]"> <configVersion> 2 </configVersion> <userremoteConfigs> <hudson.plugins.git.USerrem OTECONFIG> <URL> ssh: //[email protected]: 10022/bigdata/dwx.git </url> <emmermentialsid> 84f4be19-ea8d-4271 -8cfb-42af8f507285</credentialsId></hudson.plugins.git.UserRemoteConfig></userRemoteConfigs><branches><hudson.plugins.git.BranchSpec><name>*/develop</name></hudson.plugins.git.BranchSpec></branch ES> <dodenerateSubModuleConfigurations> Falsch </dodenerateSubModuleConfigurations> <submodulecfg/> <extensions/</scm> <suredNode>! MacMini </beauftragtesNode> <canroam> false </canroam> <Behinderte> false </disa bled><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 -rfname $ {project} .tar.gz </command> </hudson.tasks.shell> </builders> <Publishers/> <BuildWapper/> <projekt>
Die von der XML erhaltenen Informationen entsprechen der Konfigurationsdatei in der Abbildung nicht.
Promotionskripte können in der XML -Konfigurationsdatei nicht erhalten werden. Zu diesem Zeitpunkt gibt es ein Problem. Diese API kann die Promotionskripte nicht erhalten. Gleichzeitig bringt dies auch große Herausforderungen für unsere Arbeit. Dies bedeutet auch, dass die Bereitstellung der Commander -Anwendung nicht mit dieser Methode realisiert werden kann und die Konfiguration von Jenkins über die Plattformmethode direkt betrieben werden kann.
Nach der Abfrage der relevanten APIs können wir jedoch nicht den relevanten Inhalt finden. Nach unablässigen Bemühungen fanden wir schließlich die API im Zusammenhang mit dem Förderungs-Build-Plug-In
Abfrage: http: //host/job/jobname/promotion/process/promotionname/config.xml
Diese Schnittstelle kann ihre XML -Datei abrufen, kann jedoch die Konfigurationsdatei nicht hinzufügen oder ändern.
Ich habe einige Methoden dafür integriert:
/** * @Author Chenlang * Datum 2018/5/7 */ @Slf4JPublic -Klasse JenkinSpromotionUtils {private statische endgültige String sub_path_promotion_coommand = "/hudson.plugins.promoted__builds.PromotionProcess"; private statische endgültige String sub_path_build = "/buildsteps"; private statische endgültige String sub_path_builder_shell_command = "/hudson.tasks.shell/command"; private statische endgültige Zeichenfolge path_promotion_command = sub_path_promotion_coommand + sub_path_build + sub_path_builder_shell_command; private statische String create_promotion_json = "{'Eigenschaften': {'Stapler-Klasse-Bag': 'True', 'Hudson-Plugins-Promoting_Builds-JohnPropertyImpl': {'Promotions': {'ActiveItems': {'Name': '%s ',' is tosible ':' ',' icon ':' star-gold ',' HassignedLabel ': false,' beauftragtLabelstring ':' ',' Bedingungen ': {' Stapler-Klasse-Bag ':' True '}}}} "; private statische endgültige String content_type = "Anwendung/x-www-form-rencoded"; public static void updatePromotionShell (Dokument JobconfigDocument, String JobName, JenkinSpromotionClient JenkinSpromotionClient, String PromotionShell, String Path) löst 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 -Pfad, JenkinSpromotionClient JenkinSpromotionClient) löscht 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); Karte <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 (Ausnahme e) {log.Error ("fehlgeschlagen, wenn eine Promotion gestartet wird" + e); } JenkinSpromotionClient.createjob (JobName, PromotionName, document.asxml ()); } public static String getPromotionName (Dokument JobConfigDocument, String Path) {return JobConfigDocument }} Paket cn.caijiajia.phoenix.service.jenkins; import com.offbytwo.jenkins.client.jenkinShttpclient; import com.offbytwo.jenkins.client org.dom4j.documentHelper; import org.springframework.bean.factory.annotation /** * @Author Chenlang * Datum 2018/5/4 */ @componentpublic Klasse JenkinSpromotionClient {@autowired Private JenkinShtpclient JenkinShttpclient; public JenkinSpromotionClient () {} / *** die Auftragskonfigurationsdatei des Jobs erhalten* @param JobName Jobname* @param PromotionName Promotion Name* @return* @throws ioException* / public document GetJobPromotionxml (String Jobname, String PromotionName) Throws ioException, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, Dokument, documentce, {{{{{{{{{oder DocumentHelper.ParSetExt (this.getJobXML (JobName, PromotionName)); } /** * Update 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); }/** * Jobskript hinzufügen * * @param JobName * @param jobxml * @throws ioException */public void createjob (String JobName, String ProprootionName, String JobXML) löst ioException {this.jenkinShttpclient.post_xml (this.tojobbase, (this. Jobxml, wahr); }/** * Fügen Sie einen Auftrag von Promotion hinzu * * @param JobName * @param map * @throws ioException */public void createjob (String JobName, Map Map) löst IoException {this.jenkinKinShttpclient.post_form ("/" + + comodingils. } private String getJobXMl (String JobName, String ProproctionName) löst IOException {return this.jenkinShttpclient.get (this.jobbaseurl (JobName, PromotionName) + "/config.xml") aus; } private String tojobbaseUrl (String JobName, String ProprootionName) {return "/job/" + codingutils.encode (JobName) + "/Promotion/Process/" + ProprootionName; } /** * Construction of promotion scripts* @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/"+codingutils.code (jobname)+"/"+Version+"/Promotion/ForcePromotion? name ="+ProprootionName+"& json =%7b%7d & subschritt = Force Promotion"); } else {this.jenkinShttpclient.post ("/job/"+codingutils.code (JobName)+"/"+Version+"/Promotion/"+ProprootionName+"/Build? }}}Die Methode verkauft die Konfiguration Addition, Löschung, Änderung und Abfrage des Promote-Build-Plug-Ins sowie die Konstruktion des Promotion-Skripts.
Zusammenfassen
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, dass der Inhalt dieses Artikels einen gewissen Referenzwert für das Studium oder die Arbeit eines jeden hat. Wenn Sie Fragen haben, können Sie eine Nachricht zur Kommunikation überlassen. Vielen Dank für Ihre Unterstützung bei Wulin.com.