def __enter__(self): try: start_go_server(self.gocd_version, self.gocd_download_version_string) configurator = GoCdConfigurator(HostRestClient('localhost:8153')) pipeline = configurator \ .ensure_pipeline_group("P.Group") \ .ensure_replacement_of_pipeline("more-options") \ .set_timer("0 15 22 * * ?") \ .set_git_material(GitMaterial("https://github.com/SpringerSBM/gomatic.git", material_name="some-material-name", polling=False)) \ .ensure_environment_variables({'JAVA_HOME': '/opt/java/jdk-1.7'}) \ .ensure_parameters({'environment': 'qa'}) stage = pipeline.ensure_stage("earlyStage") job = stage.ensure_job("earlyWorm").ensure_artifacts({ Artifact.get_build_artifact("scripts/*", "files"), Artifact.get_build_artifact("target/universal/myapp*.zip", "artifacts"), Artifact.get_test_artifact("from", "to") }).set_runs_on_all_agents() job.add_task(ExecTask(['ls'])) configurator.save_updated_config(save_config_locally=True) return GoCdConfigurator(HostRestClient('localhost:8153')) except: # Swallow exception if __exit__ returns a True value if self.__exit__(*sys.exc_info()): pass else: raise
def test_all_versions(self): for gocd_version, gocd_download_version_string in self.gocd_versions: print 'test_all_versions', "*" * 60, gocd_version with populated_go_server( gocd_version, gocd_download_version_string) as configurator: self.assertEquals( ["P.Group"], [p.name for p in configurator.pipeline_groups]) self.assertEquals(["more-options"], [ p.name for p in configurator.pipeline_groups[0].pipelines ]) pipeline = configurator.pipeline_groups[0].pipelines[0] self.assertEquals("0 15 22 * * ?", pipeline.timer) self.assertEquals( GitMaterial("https://github.com/SpringerSBM/gomatic.git", material_name="some-material-name", polling=False), pipeline.git_material) self.assertEquals({'JAVA_HOME': '/opt/java/jdk-1.7'}, pipeline.environment_variables) self.assertEquals({'environment': 'qa'}, pipeline.parameters) self.assertEquals(['earlyStage'], [s.name for s in pipeline.stages]) self.assertEquals(['earlyWorm'], [j.name for j in pipeline.stages[0].jobs]) job = pipeline.stages[0].jobs[0] self.assertEquals( { Artifact.get_build_artifact("scripts/*", "files"), Artifact.get_build_artifact( "target/universal/myapp*.zip", "artifacts"), Artifact.get_test_artifact("from", "to") }, job.artifacts) self.assertEquals(True, job.runs_on_all_agents) self.assertEquals([ExecTask(['ls'])], job.tasks)
def __enter__(self): try: start_go_server(self.gocd_version) configurator = GoCdConfigurator(HostRestClient('localhost:8153')) pipeline = configurator \ .ensure_pipeline_group("P.Group") \ .ensure_replacement_of_pipeline("more-options") \ .set_timer("0 15 22 * * ?") \ .set_git_material(GitMaterial("https://github.com/SpringerSBM/gomatic.git", material_name="some-material-name", polling=False)) \ .ensure_environment_variables({'JAVA_HOME': '/opt/java/jdk-1.7'}) \ .ensure_parameters({'environment': 'qa'}) stage = pipeline.ensure_stage("earlyStage") job = stage.ensure_job("earlyWorm").ensure_artifacts( {Artifact.get_build_artifact("scripts/*", "files"), Artifact.get_build_artifact("target/universal/myapp*.zip", "artifacts"), Artifact.get_test_artifact("from", "to")}).set_runs_on_all_agents() job.add_task(ExecTask(['ls'])) configurator.save_updated_config(save_config_locally=True) return GoCdConfigurator(HostRestClient('localhost:8153')) except: # Swallow exception if __exit__ returns a True value if self.__exit__(*sys.exc_info()): pass else: raise
def create_go_pipeline(self, app_name, pipeline_name, git_repo, team_name): pipeline = self.go_configurator \ .ensure_pipeline_group(team_name) \ .ensure_replacement_of_pipeline(app_name) \ .set_git_url(git_repo) build_stage = pipeline.ensure_stage("build") build_stage.ensure_job("init") build_job = build_stage.ensure_job("build") build_job.add_task(ExecTask(['./gradlew', 'assemble'])) build_job.ensure_artifacts( {Artifact.get_build_artifact(f"build/libs/{app_name}*.jar")}) build_image_stage = pipeline.ensure_stage("build_image") build_docker_image_job = build_image_stage.ensure_job( "build_docker_image") build_docker_image_job.add_task(ExecTask(['make', 'push'])) approle = self.setup_app_role(pipeline_name) build_docker_image_job.ensure_encrypted_environment_variables( {"VAULT_SECRET_ID": self.encrypt(approle['secret_id'])}) build_docker_image_job.ensure_environment_variables( {"VAULT_ROLE_ID": approle['role_id']}) # This should actually be committed to a git repo, rather than go directly to go self.go_configurator.save_updated_config()
def build_pipeline(gocd_host, pipeline_name, vault_server, vault_token): configurator = GoCdConfigurator(HostRestClient(gocd_host)) pipeline = configurator \ .ensure_pipeline_group("hello_world_group") \ .ensure_replacement_of_pipeline("hello_world_app") \ .set_git_url("https://github.com/ThoughtWorks-AELab/hello-secret-world.git") build_stage = pipeline.ensure_stage("build") build_stage.ensure_job("init") build_job = build_stage.ensure_job("build") build_job.add_task(ExecTask(['./gradlew', 'assemble'])) build_job.ensure_artifacts( {Artifact.get_build_artifact("build/libs/hello-secret-world*.jar")}) build_image_stage = pipeline.ensure_stage("build_image") build_docker_image_job = build_image_stage.ensure_job("build_docker_image") build_docker_image_job.add_task(ExecTask(['make', 'push'])) approle = setup_app_role(pipeline_name, vault_server, vault_token) build_docker_image_job.ensure_encrypted_environment_variables( {"VAULT_SECRET_ID": encrypt(approle['secret_id'], gocd_host)}) build_docker_image_job.ensure_environment_variables( {"VAULT_ROLE_ID": approle['role_id']}) configurator.save_updated_config()
def work(gocd_version, gocd_download_version_string, self): print('test_all_versions', "*" * 60, gocd_version) with populated_go_server(gocd_version, gocd_download_version_string) as configurator: self.assertEquals(["P.Group"], [p.name for p in configurator.pipeline_groups]) self.assertEquals(["more-options"], [p.name for p in configurator.pipeline_groups[0].pipelines]) pipeline = configurator.pipeline_groups[0].pipelines[0] self.assertEquals("0 15 22 * * ?", pipeline.timer) self.assertEquals(GitMaterial("https://github.com/SpringerSBM/gomatic.git", material_name="some-material-name", polling=False), pipeline.git_material) self.assertEquals({'JAVA_HOME': '/opt/java/jdk-1.7'}, pipeline.environment_variables) self.assertEquals({'environment': 'qa'}, pipeline.parameters) self.assertEquals(['earlyStage'], [s.name for s in pipeline.stages]) self.assertEquals(['earlyWorm'], [j.name for j in pipeline.stages[0].jobs]) job = pipeline.stages[0].jobs[0] self.assertEquals({Artifact.get_build_artifact("scripts/*", "files"), Artifact.get_build_artifact("target/universal/myapp*.zip", "artifacts"), Artifact.get_test_artifact("from", "to")}, job.artifacts) self.assertEquals(True, job.runs_on_all_agents) self.assertEquals([ExecTask(['ls'])], job.tasks)