Skip to content

Commit

Permalink
Inject deployment env variables in the build
Browse files Browse the repository at this point in the history
[FIXED JENKINS-33988] Inject deployment env variables in the build
  • Loading branch information
fbelzunc committed Apr 2, 2016
1 parent 8d04f58 commit 63a9cd8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
Expand Up @@ -20,8 +20,12 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.CheckForNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
Expand Down Expand Up @@ -55,7 +59,12 @@ public Condition getCond() {
return cond;
}

@Deprecated
public void checkAndFire(DeploymentFacet facet) {
checkAndFire(facet, null);
}

public void checkAndFire(DeploymentFacet facet, @CheckForNull HostRecord hostRecord) {
try {
if (upstream==null)
upstream = Jenkins.getInstance().getItem(upstreamJob, job, Job.class);
Expand All @@ -75,7 +84,15 @@ public void checkAndFire(DeploymentFacet facet) {
if (b!=null) {
// pass all the current parameters if we can
ParametersAction action = b.getAction(ParametersAction.class);
parameterizedJobMixIn.scheduleBuild2(5, new CauseAction(new UpstreamDeploymentCause(b)), action);
if (hostRecord != null) {
List<HostRecord> listHostRecord = new ArrayList();
Iterator iterator = facet.records.iterator();
while (iterator.hasNext()) {
listHostRecord.add((HostRecord) iterator.next());
}
HostRecords hostrecords = new HostRecords(listHostRecord);
parameterizedJobMixIn.scheduleBuild2(5, new CauseAction(new UpstreamDeploymentCause(b)), action);
}
return;
}
}
Expand Down Expand Up @@ -106,15 +123,15 @@ private Triggered findTriggeredRecord(Fingerprint f) {
@Extension
public static class ListenerImpl extends DeploymentFacetListener {
@Override
public void onChange(final DeploymentFacet facet, HostRecord newRecord) {
public void onChange(final DeploymentFacet facet, final HostRecord newRecord) {
POOL.submit(new Runnable() {
public void run() {
//TODO - 1.621: use getTrigger(Job<?,?> job, Class<T> clazz)
for (ParameterizedJobMixIn.ParameterizedJob parameterizedJob : Jenkins.getInstance().getAllItems(ParameterizedJobMixIn.ParameterizedJob.class)) {
for (Trigger trigger : parameterizedJob.getTriggers().values()) {
if (trigger instanceof DeploymentTrigger) {
DeploymentTrigger deploymentTrigger = (DeploymentTrigger) trigger;
deploymentTrigger.checkAndFire(facet);
deploymentTrigger.checkAndFire(facet, newRecord);
}
}
}
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/org/jenkinsci/plugins/deployment/HostRecords.java
@@ -0,0 +1,73 @@
/*
* The MIT License
*
* Copyright 2015 Felix Belzunce Arcos.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.deployment;

import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.EnvironmentContributingAction;
import hudson.model.InvisibleAction;
import org.apache.commons.lang.StringUtils;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Injects the deployment environment variables into the build
* @since May 8, 2015
* @version TODO
*/
public class HostRecords extends InvisibleAction implements EnvironmentContributingAction {
private final @Nonnull Collection<HostRecord> hostrecords;

public HostRecords(@Nonnull Collection<HostRecord> hostRecords) {
this.hostrecords = hostRecords;
}

public @Nonnull Collection<HostRecord> getHostrecords() {
return hostrecords;
}

public void buildEnvVars(AbstractBuild<?, ?> abstractBuild, EnvVars envVars) {
@Nonnull final List<String> deploymentEnv = new ArrayList();
@Nonnull final List<String> deploymentHost = new ArrayList();
@Nonnull final List<String> deploymentPath = new ArrayList();
@Nonnull final List<String> deploymentTimeStamp = new ArrayList();

Collection<HostRecord> hostRecords = getHostrecords();

for (HostRecord hostRecord : hostRecords) {
deploymentEnv.add(hostRecord.getEnv());
deploymentHost.add(hostRecord.getHost());
deploymentPath.add(hostRecord.getPath());
deploymentTimeStamp.add(hostRecord.getTimestampString());
}
envVars.put("DEPLOYMENT_ENV", StringUtils.join(deploymentEnv, ","));
envVars.put("DEPLOYMENT_HOST", StringUtils.join(deploymentHost, ","));
envVars.put("DEPLOYMENT_PATH", StringUtils.join(deploymentPath, ","));
envVars.put("DEPLOYMENT_TIMESTAMP", StringUtils.join(deploymentTimeStamp, ","));
}
}
Expand Up @@ -7,6 +7,15 @@
For example, you can use this to run post-deployment smoke tests, or you can tickle your load balancer to shift
traffic to newer servers.

<p>
This plugin injects the following variables:

<ul>
<li><b>DEPLOYMENT_HOST</b> - Host to which the artifact was deployed</li>
<li><b>DEPLOYMENT_PATH</b> - Path to which the artifact was deployed</li>
<li><b>DEPLOYMENT_TIMESTAMP</b> - Timestamp of the deployment</li>
</ul>

<p>
This feature critically depends on <a href="https://wiki.jenkins-ci.org/display/JENKINS/Fingerprint">fingerprints</a>
to function properly.
Expand Down

1 comment on commit 63a9cd8

@pratannaimjoi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

จากการนำเข้า linepy *
จาก liff.ttypes นำเข้า LiffChatContext, LiffContext, LiffSquareChatContext, LiffNoneContext, LiffViewRequest
จาก akad.ttypes นำเข้าข้อความ
จาก akad.ttypes อิมพอร์ต ContentType เป็น Type
จาก akad.ttypes นำเข้า TalkException
จาก datetime นำเข้า datetime, timedelta
จากเวลานำเข้านอนหลับ
จาก bs4 นำเข้า BeautifulSoup เป็น bSoup
จาก bs4 นำเข้า BeautifulSoup
จาก humanfriendly นำเข้า format_timespan, format_size, format_number, format_length
จาก gtts นำเข้า gTTS
จากเธรดนำเข้าเธรด
จาก io นำเข้า StringIO
จากพูลการนำเข้าแบบหลายกระบวนการ
จากนักแปลนำเข้า googletrans
จาก urllib.parse นำเข้า urlencode
จากการนำเข้า tmp.MySplit *
จากการนำเข้าสุ่มrandint
จาก copyfile นำเข้า shutil
จาก youtube_dl นำเข้า YoutubeDL
นำเข้ากระบวนการย่อย, youtube_dl, เป็นมนุษย์, ติดตามย้อนกลับ
นำเข้ากระบวนการย่อยเป็น cmd
แพลตฟอร์มนำเข้า
คำขอนำเข้า json
เวลานำเข้า , สุ่ม, sys, json, null, pafy, codecs, html5lib, shutil, เกลียว, glob, re, base64, สตริง, ระบบปฏิบัติการ, การร้องขอ, หก, ast, pytz, วิกิพีเดีย, urllib, arllib, asyncio , ตรวจสอบย้อนกลับ
_session =ร้องขอ. session ()
ลอง :
นำเข้า urllib.request เป็น urllib2
ยกเว้น ImportError :
นำเข้า urllib2

Please sign in to comment.