private SchedulingContext schedulingContext(String username, PipelineConfig pipelineConfig, StageConfig stageConfig) { Agents availableAgents = environmentConfigService.agentsForPipeline(pipelineConfig.name()); SchedulingContext context = new DefaultSchedulingContext(username, availableAgents, elasticProfileService.listAll()); context = context.overrideEnvironmentVariables(pipelineConfig.getVariables()); context = context.overrideEnvironmentVariables(stageConfig.getVariables()); return context; }
public static void toJSON(OutputWriter jsonWriter, StageConfig stageConfig) { if (!stageConfig.errors().isEmpty()) { jsonWriter.addChild("errors", errorWriter -> { new ErrorGetter(new HashMap<>()).toJSON(errorWriter, stageConfig); }); } jsonWriter.addIfNotNull("name", stageConfig.name()); jsonWriter.add("fetch_materials", stageConfig.isFetchMaterials()); jsonWriter.add("clean_working_directory", stageConfig.isCleanWorkingDir()); jsonWriter.add("never_cleanup_artifacts", stageConfig.isArtifactCleanupProhibited()); jsonWriter.addChild("approval", approvalWriter -> ApprovalRepresenter.toJSON(approvalWriter, stageConfig.getApproval())); jsonWriter.addChildList("environment_variables", envVarsWriter -> EnvironmentVariableRepresenter.toJSON(envVarsWriter, stageConfig.getVariables())); jsonWriter.addChildList("jobs", getJobs(stageConfig)); }
public static void toJSON(OutputWriter jsonWriter, StageConfig stageConfig) { if (!stageConfig.errors().isEmpty()) { jsonWriter.addChild("errors", errorWriter -> { new ErrorGetter(new HashMap<>()).toJSON(errorWriter, stageConfig); }); } jsonWriter.addIfNotNull("name", stageConfig.name()); jsonWriter.add("fetch_materials", stageConfig.isFetchMaterials()); jsonWriter.add("clean_working_directory", stageConfig.isCleanWorkingDir()); jsonWriter.add("never_cleanup_artifacts", stageConfig.isArtifactCleanupProhibited()); jsonWriter.addChild("approval", approvalWriter -> ApprovalRepresenter.toJSON(approvalWriter, stageConfig.getApproval())); jsonWriter.addChildList("environment_variables", envVarsWriter -> EnvironmentVariableRepresenter.toJSON(envVarsWriter, stageConfig.getVariables())); jsonWriter.addChildList("jobs", getJobs(stageConfig)); }
public StageConfig toStage(CRStage crStage) { Approval approval = toApproval(crStage.getApproval()); StageConfig stageConfig = new StageConfig(new CaseInsensitiveString(crStage.getName()), crStage.isFetchMaterials(), crStage.isCleanWorkingDir(), approval, crStage.isArtifactCleanupProhibited(), toJobConfigs(crStage.getJobs())); EnvironmentVariablesConfig environmentVariableConfigs = stageConfig.getVariables(); for (CREnvironmentVariable crEnvironmentVariable : crStage.getEnvironmentVariables()) { environmentVariableConfigs.add(toEnvironmentVariableConfig(crEnvironmentVariable)); } return stageConfig; }
@Test public void shouldSupportEnvironmentVariablesInAStage() throws Exception { String content = configWithPipeline( "<pipeline name='pipeline1'>" + " <materials>" + " <svn url ='svnurl'/>" + " </materials>" + " <stage name='mingle'>" + " <environmentvariables>" + " <variable name='STAGE_VARIABLE'><value>stage variable</value></variable>" + " </environmentvariables>" + " <jobs>" + " <job name='do-something'>" + " </job>" + " </jobs>" + " </stage>" + "</pipeline>", CONFIG_SCHEMA_VERSION); CruiseConfig cruiseConfig = ConfigMigrator.loadWithMigration(content).config; assertThat(cruiseConfig.pipelineConfigByName(new CaseInsensitiveString("pipeline1")).getFirstStageConfig().getVariables(), hasItem(new EnvironmentVariableConfig("STAGE_VARIABLE", "stage variable"))); }
CRStage stageToCRStage(StageConfig stageConfig) { CRStage crStage = new CRStage(stageConfig.name().toString()); for(JobConfig job: stageConfig.getJobs()) { crStage.addJob(jobToCRJob(job)); } for (EnvironmentVariableConfig var: stageConfig.getVariables()) { crStage.addEnvironmentVariable(environmentVariableConfigToCREnvironmentVariable(var)); } crStage.setApproval(approvalToCRApproval(stageConfig.getApproval())); crStage.setFetchMaterials(stageConfig.isFetchMaterials()); crStage.setArtifactCleanupProhibited(stageConfig.isArtifactCleanupProhibited()); crStage.setCleanWorkingDir(stageConfig.isCleanWorkingDir()); return crStage; }
@Test public void shouldSetEnvironmentVariablesOnSchedulingContext() throws Exception { EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig(); existing.add("firstVar", "firstVal"); existing.add("overriddenVar", "originalVal"); SchedulingContext schedulingContext = new DefaultSchedulingContext(); schedulingContext = schedulingContext.overrideEnvironmentVariables(existing); EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig(); stageLevel.add("stageVar", "stageVal"); stageLevel.add("overriddenVar", "overriddenVal"); StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval()); config.setVariables(stageLevel); ReflectionUtil.setField(schedulingContext, "rerun", true); SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables()); assertThat(context.isRerun(), is(true)); EnvironmentVariablesConfig environmentVariablesUsed = context.getEnvironmentVariablesConfig(); assertThat(environmentVariablesUsed.size(), is(3)); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal"))); }
@Test public void shouldCreateRerunSchedulingContextCorrectly() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); windows.disable(); Agents matchingAgents = new Agents(linux, windows); EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig(); existing.add("firstVar", "firstVal"); existing.add("overriddenVar", "originalVal"); SchedulingContext schedulingContext = new DefaultSchedulingContext("approver", matchingAgents); schedulingContext = schedulingContext.overrideEnvironmentVariables(existing); EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig(); stageLevel.add("stageVar", "stageVal"); stageLevel.add("overriddenVar", "overriddenVal"); StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval()); config.setVariables(stageLevel); SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables()); SchedulingContext rerunContext = context.rerunContext(); assertThat(rerunContext.isRerun(), is(true)); assertThat(rerunContext.getApprovedBy(), is("approver")); Agents agents = (Agents) ReflectionUtil.getField(rerunContext, "agents"); assertThat(agents, is(matchingAgents)); EnvironmentVariablesConfig environmentVariablesUsed = rerunContext.getEnvironmentVariablesConfig(); assertThat(environmentVariablesUsed.size(), is(3)); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal"))); }
@Test public void shouldCreatePermittedAgentContextCorrectly() throws Exception { AgentConfig linux = agent("uuid1", "linux"); AgentConfig windows = agent("uuid2", "windows"); windows.disable(); Agents matchingAgents = new Agents(linux, windows); EnvironmentVariablesConfig existing = new EnvironmentVariablesConfig(); existing.add("firstVar", "firstVal"); existing.add("overriddenVar", "originalVal"); SchedulingContext schedulingContext = new DefaultSchedulingContext("approver", matchingAgents); schedulingContext = schedulingContext.overrideEnvironmentVariables(existing); EnvironmentVariablesConfig stageLevel = new EnvironmentVariablesConfig(); stageLevel.add("stageVar", "stageVal"); stageLevel.add("overriddenVar", "overriddenVal"); StageConfig config = StageConfigMother.custom("test", Approval.automaticApproval()); config.setVariables(stageLevel); SchedulingContext context = schedulingContext.overrideEnvironmentVariables(config.getVariables()); ReflectionUtil.setField(context, "rerun", true); SchedulingContext permittedAgentContext = context.permittedAgent("uuid1"); Agents agents = (Agents) ReflectionUtil.getField(permittedAgentContext, "agents"); assertThat(agents.size(), is(1)); assertThat(agents.get(0).getAgentIdentifier().getUuid(), is("uuid1")); assertThat(permittedAgentContext.isRerun(), is(true)); assertThat(permittedAgentContext.getApprovedBy(), is("approver")); EnvironmentVariablesConfig environmentVariablesUsed = permittedAgentContext.getEnvironmentVariablesConfig(); assertThat(environmentVariablesUsed.size(), is(3)); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("firstVar", "firstVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("overriddenVar", "overriddenVal"))); assertThat(environmentVariablesUsed, hasItem(new EnvironmentVariableConfig("stageVar", "stageVal"))); }
@Test public void shouldCloneStagesSoThatMutationDoesnotAffectTemplate() throws Exception { PipelineConfig pipelineConfig = new PipelineConfig(new CaseInsensitiveString("pipelineName"), new MaterialConfigs(MaterialConfigsMother.hgMaterialConfig("http://google.com"))); pipelineConfig.setTemplateName(new CaseInsensitiveString("templateName")); PipelineTemplateConfig template = new PipelineTemplateConfig(); JobConfig jobConfigFromTemplate = new JobConfig("job-1"); StageConfig stageConfigFromTemplate = new StageConfig(new CaseInsensitiveString("stage-1"), new JobConfigs(jobConfigFromTemplate)); template.add(stageConfigFromTemplate); pipelineConfig.usingTemplate(template); StageConfig stageConfigFromPipeline = pipelineConfig.get(0); EnvironmentVariablesConfig variablesConfig = new EnvironmentVariablesConfig(); variablesConfig.add("FOO", "BAR"); stageConfigFromPipeline.setVariables(variablesConfig); JobConfig jobConfigFromPipeline = stageConfigFromPipeline.jobConfigByConfigName(new CaseInsensitiveString("job-1")); EnvironmentVariablesConfig jobVariablesConfigFromPipeline = new EnvironmentVariablesConfig(); jobVariablesConfigFromPipeline.add("BAZ", "QUUX"); jobConfigFromPipeline.setVariables(jobVariablesConfigFromPipeline); assertThat(stageConfigFromPipeline.getVariables().isEmpty(), is(false)); assertThat(jobConfigFromPipeline.getVariables().isEmpty(), is(false)); assertThat(stageConfigFromTemplate.getVariables().isEmpty(), is(true)); assertThat(jobConfigFromTemplate.getVariables().isEmpty(), is(true)); }
StageConfig fromStage = from.findBy(toStage.name()); copy(fromStage, toStage); copyCollectionErrors(fromStage.getVariables(), toStage.getVariables()); copy(fromStage.getApproval(), toStage.getApproval()); copyCollectionErrors(fromStage.getApproval().getAuthConfig(), toStage.getApproval().getAuthConfig());