Skip to content

Commit

Permalink
Merge pull request #166 from jenkinsci/users/jeyou/build-vars
Browse files Browse the repository at this point in the history
Ensure TFS/Team Services build variables are added to Jenkins environment variables
  • Loading branch information
Jeff Young authored Jul 18, 2017
2 parents 33f3be3 + 69b0a77 commit 3e897f1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
19 changes: 11 additions & 8 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This is a Maven project with Java sources. The mvn package command will produce
- Clone the repo from https://github.com/jenkinsci/tfs-plugin.git
- change dir to the tfs-plugin folder
- run "mvn package"
- If you do not have Maven installed yet, here are [instructions](http://www.mkyong.com/maven/how-to-install-maven-in-windows/) on how to install it on Windows.
- Initial build will have to download lots of libraries. This could take a few minutes.
- This produces tfs-plugin\tfs\target\tfs.hpi

Expand All @@ -18,13 +19,13 @@ To use Intellij IDEA as the editor for this project simply do the following afte

You should now be able to build from within IntelliJ
- NOTE to build the hpi file you will have to
- bring up the Maven Projects tool window (View->Tool Windows->Maven Projects) and click the "run maven goal" button
- Then type in "package" in the goal text box
- bring up the Maven Projects tool window (View->Tool Windows->Maven Projects) and click the "execute maven goal" button
- Then type "package" in the "Command Line" text box (of the Execute Maven Goal dialog)

## Debugging the Plugin
See https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial for information on how to debug the plugin.
From within IntelliJ:
1) Create a new Run configuration
1) Create a new Run configuration (Run | Edit Configurations... | + )
1) Type = Maven
1) Name = run hpi
1) Working directory should be the full path to "../tfs-plugin/tfs" (NOTE this is NOT the root folder)
Expand All @@ -37,16 +38,18 @@ From within IntelliJ:
1) or use whatever port you want
1) On the Runner tab
1) Environment Variables == MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n
1) To set the environment variables, make sure you uncheck 'Use project settings' first. Then use the '...' to set the variable.

1) Run or Debug this configuration using the play and debug icons (top right)
1) Set any breakpoints you want in IntelliJ
1) Navigate to http://localhost:8090/jenkins

Note: this runs Jenkins on your local OS not in a docker image. As such, any configurations you make are preserved between runs.
Note: this runs Jenkins on your local OS not in a Docker image. As such, any configurations you make are preserved between runs. You do not need to have Jenkins previously installed locally (IntelliJ will run Jenkins from a local JAR file).

## Installing Jenkins Locally for Manual Testing
The easiest method is to run Jenkins in a docker image.
The easiest method is to run Jenkins in a Docker image.
1) Install Docker for your OS (https://www.docker.com/community-edition)
1) Install and run the jenkins image
1) Install and run the Jenkins image

`
docker run --name localJenkins -p 9191:9191 -p 50001:50001 --env "JENKINS_OPTS=--httpPort=9191" --env JENKINS_SLAVE_AGENT_PORT=50001 --env hudson.plugins.tfs.telemetry.isDeveloperMode=true jenkins
Expand All @@ -57,7 +60,7 @@ The easiest method is to run Jenkins in a docker image.
- Look in the output for the admin password
- The output sent to the console is also where you will see any logger output
- Note the environment variable "hudson.plugins.tfs.telemetry.isDeveloperMode". It is important to set this variable so that AppInsights data is sent to right key
- This installs a linux jenkins server on Docker (NOT one based on Windows or the host OS)
- This installs a Linux Jenkins server on Docker (NOT one based on Windows or the host OS)
1) Setup Jenkins
1) Go to http://localhost:9191
1) Enter the admin password
Expand All @@ -67,7 +70,7 @@ The easiest method is to run Jenkins in a docker image.
1) Go to http://localhost:9191/pluginManager/advanced
1) Browse to the tfs.hpi file and Upload it
1) To update the plugin, repeat steps 1 and 2 and then restart Jenkins by going to http://localhost:9191/restart
1) To Stop Jenkins and start from scratch
1) To stop Jenkins and start from scratch

`
docker stop localJenkins
Expand Down
15 changes: 15 additions & 0 deletions tfs/src/main/java/hudson/plugins/tfs/TeamFoundationServerScm.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,21 @@ public void setRepositoryBrowser(final TeamFoundationServerRepositoryBrowser rep
@Override
public void buildEnvVars(AbstractBuild<?,?> build, Map<String, String> env) {
super.buildEnvVars(build, env);

final TeamBuildDetailsAction buildDetailsAction = build.getAction(TeamBuildDetailsAction.class);
if (buildDetailsAction != null) {
//Add the TFS build variables as environment variables in the Jenkins environment
//https://www.visualstudio.com/en-us/docs/build/define/variables
for (Map.Entry<String, String> entry : buildDetailsAction.buildVariables.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (value != null) {
//Replace . with _ and ensure they're UPPERCASE to match Team Services pattern
env.put(key.replace('.', '_').toUpperCase(), value);
}
}
}

if (normalizedWorkspaceName != null) {
env.put(WORKSPACE_ENV_STR, normalizedWorkspaceName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,35 @@ public void assertGetModuleRootWorksForDotWorkFolder() throws Exception {
moduleRoot.exists());
assertEquals("The module root was not the same as workspace", moduleRoot.lastModified(), workspace.lastModified());
}


@Test
public void assertTeamServicesBuildVariablesAreAddedToEnvVars() throws Exception {
//Add a couple of variables that will be present during a TFS/Team Services build
Map<String, String> buildVariables = new HashMap<String, String>();
buildVariables.put("System.DefaultWorkingDirectory", "C:\\build-agent\\_work\\1\\s");
buildVariables.put("Build.Repository.Git.SubmoduleCheckout", "false");
//These three are needed to create the action's buildUrl
buildVariables.put("System.TeamFoundationCollectionUri", "https://RESOLVED.com");
buildVariables.put("System.TeamProject", "TEAM_PROJECT");
buildVariables.put("Build.BuildId", "42");
TeamBuildDetailsAction action = new TeamBuildDetailsAction(buildVariables);

AbstractBuild build = mock(AbstractBuild.class);
when(build.getAction(TeamBuildDetailsAction.class)).thenReturn(action);
TeamFoundationServerScm scm = new TeamFoundationServerScm("serverurl", "projectpath", "WORKSPACE_SAMPLE");
AbstractProject project = mock(AbstractProject.class);
when(build.getProject()).thenReturn(project);
Map<String, String> env = new HashMap<String, String>();
scm.buildEnvVars(build, env);

//Ensure . is replaced with _, keys are UPPERCASE
assertEquals("The key or value for System.DefaultWorkingDirectory was incorrect", "C:\\build-agent\\_work\\1\\s", env.get("SYSTEM_DEFAULTWORKINGDIRECTORY"));
assertEquals("The key or value for Build.Repository.Git.SubmoduleCheckout was incorrect", "false", env.get("BUILD_REPOSITORY_GIT_SUBMODULECHECKOUT"));
assertEquals("The key or value for System.TeamFoundationCollectionUri was incorrect", "https://RESOLVED.com", env.get("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI"));
assertEquals("The key or value for System.TeamProject was incorrect", "TEAM_PROJECT", env.get("SYSTEM_TEAMPROJECT"));
assertEquals("The key or value for Build.BuildId was incorrect", "42", env.get("BUILD_BUILDID"));
}

@Test
public void assertWorkspaceNameIsAddedToEnvVars() throws Exception {
TeamFoundationServerScm scm = new TeamFoundationServerScm("serverurl", "projectpath", "WORKSPACE_SAMPLE");
Expand Down

0 comments on commit 3e897f1

Please sign in to comment.