Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GoCD version detection does not work for templates #62

Open
lobsterdore opened this issue Jan 22, 2019 · 1 comment
Open

GoCD version detection does not work for templates #62

lobsterdore opened this issue Jan 22, 2019 · 1 comment

Comments

@lobsterdore
Copy link

lobsterdore commented Jan 22, 2019

I've encountered an issue with creating a template from code, consider this bit of config:

#!/usr/bin/env python
from gomatic import *

configurator = GoCdConfigurator(HostRestClient("localhost:8153"))
template = configurator \
    .ensure_replacement_of_template("first_pipeline")

stage = template.ensure_stage("a_stage")
job = stage.ensure_job("a_job")
job.add_task(ExecTask(['thing']))

job.ensure_artifacts({
    gocd.artifacts.Artifact.get_build_artifact('a_file')
})

configurator.save_updated_config()

If you are using a version of GoCD that is 18.3 or later then this will fail with the following error:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    configurator.save_updated_config()
  File "/Users/david.reedfrancis/workspace/jsains/tickets/smss/188/smartshop-services-gocd/.venv_Darwin_x86_64_2.7.15/lib/python2.7/site-packages/gomatic/go_cd_configurator.py", line 307, in save_updated_config
    self.__host_rest_client.post('/go/admin/restful/configuration/file/POST/xml', data, headers)
  File "/Users/david.reedfrancis/workspace/jsains/tickets/smss/188/smartshop-services-gocd/.venv_Darwin_x86_64_2.7.15/lib/python2.7/site-packages/gomatic/go_cd_configurator.py", line 346, in post
    raise RuntimeError("Could not post config to Go server (%s) [status code=%s]:\n%s" % (url, result.status_code, message))
RuntimeError: Could not post config to Go server (http://localhost:8153/go/admin/restful/configuration/file/POST/xml) [status code=409]:
"Type" is required for Artifact

Digging into the code it seems that there is a function in pipeline.py that switches how the artifact XML is created based on the GoCD version, it seems that the detection will only work for pipelines that have a group as a parent - https://github.com/gocd-contrib/gomatic/blob/master/gomatic/gocd/pipelines.py#L105

I don't think this code will ever work for templates.

@lobsterdore
Copy link
Author

A hacky workaround for this:

#!/usr/bin/env python
from gomatic import *

def gocd_version_patcher():
    return True

configurator = GoCdConfigurator(HostRestClient("localhost:8153"))
template = configurator \
    .ensure_replacement_of_template("first_pipeline")

stage = template.ensure_stage("a_stage")
job = stage.ensure_job("a_job")
job.is_gocd_18_3_and_above = gocd_version_patcher
job.add_task(ExecTask(['thing']))

job.ensure_artifacts({
    gocd.artifacts.Artifact.get_build_artifact('a_file')
})

configurator.save_updated_config()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant