Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Add an optional config parameter for generic jobs to run cleanup task…
Browse files Browse the repository at this point in the history
…s after the job completes.

Summary:
Add teardown script to build step

Rename TEARDOWN_SCRIPT to RESET_SCRIPT since this script is run AFTER the build completes

Add a test for the RESET_SCRIPT parameter

Update documentation to include reset-generic description

Reviewers: ar

Reviewed By: ar

Subscribers: changesbot, wwu, kylec

Projects: #changes, #changes-stability

Differential Revision: https://tails.corp.dropbox.com/D96146
  • Loading branch information
aroravishal committed Mar 18, 2015
1 parent fd85f7e commit 69cac7c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
7 changes: 5 additions & 2 deletions changes/backends/jenkins/buildstep.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,22 @@ def get_builder_options(self):
class JenkinsGenericBuildStep(JenkinsBuildStep):
builder_cls = JenkinsGenericBuilder

def __init__(self, job_name, script, cluster, diff_cluster='', path='', workspace='',
**kwargs):
def __init__(self, job_name, script, cluster, diff_cluster='', path='',
workspace='', reset_script='', **kwargs):
self.script = script
self.reset_script = reset_script
self.cluster = cluster
self.diff_cluster = diff_cluster
self.path = path
self.workspace = workspace

super(JenkinsGenericBuildStep, self).__init__(job_name=job_name, **kwargs)

def get_builder_options(self):
options = super(JenkinsGenericBuildStep, self).get_builder_options()
options.update({
'script': self.script,
'reset_script': self.reset_script,
'cluster': self.cluster,
'path': self.path,
'workspace': self.workspace,
Expand Down
2 changes: 2 additions & 0 deletions changes/backends/jenkins/generic_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class JenkinsGenericBuilder(JenkinsBuilder):
def __init__(self, master_urls=None, *args, **kwargs):
self.script = kwargs.pop('script')
self.reset_script = kwargs.pop('reset_script', '')
self.cluster = kwargs.pop('cluster')
self.diff_cluster = kwargs.pop('diff_cluster', None)
self.path = kwargs.pop('path', '')
Expand Down Expand Up @@ -48,6 +49,7 @@ def get_job_parameters(self, job, script=None, target_id=None, path=None):
{'name': 'CHANGES_PID', 'value': project.slug},
{'name': 'REPO_URL', 'value': repo_url},
{'name': 'SCRIPT', 'value': script},
{'name': 'RESET_SCRIPT', 'value': self.reset_script},
{'name': 'REPO_VCS', 'value': repository.backend.name},
{'name': 'CLUSTER', 'value': cluster},
{'name': 'WORK_PATH', 'value': path},
Expand Down
15 changes: 15 additions & 0 deletions docs/examples/jenkins-generic-job.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<description></description>
<defaultValue></defaultValue>
</hudson.model.StringParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>RESET_SCRIPT</name>
<description>Cleanup tasks to run after the generic job completes. Intended for use by reset-generic jobs.
</description>
<defaultValue></defaultValue>
</hudson.model.StringParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>CHANGES_PID</name>
<description>Changes Project Slug</description>
Expand Down Expand Up @@ -99,5 +105,14 @@
<latestOnly>false</latestOnly>
<allowEmptyArchive>true</allowEmptyArchive>
</hudson.tasks.ArtifactArchiver>
<hudson.tasks.BuildTrigger>
<childProjects>reset-generic</childProjects>
<threshold>
<name>FAILURE</name>
<ordinal>2</ordinal>
<color>RED</color>
<completeBuild>true</completeBuild>
</threshold>
</hudson.tasks.BuildTrigger>
</publishers>
</project>
6 changes: 4 additions & 2 deletions docs/integrations/jenkins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ This changes rapidly and documentation is not maintained for the internals of th

.. literalinclude:: /examples/jenkins-generic-job.xml
:language: xml
:emphasize-lines: 85-88
:emphasize-lines: 96-99

Example scripts based on git are included for reference. Note that REPO_PATH is a global variables that is assumed to exist.
Example scripts based on git are included for reference. Note that REPO_PATH is a global variable that is assumed to exist.
The reset-generic job here is an optional, sample downstream job that can be run to execute cleanup tasks passed through the RESET_SCRIPT parameter.
Running cleanup tasks outside the generic job has the advantage of not delaying build results from the generic job being posted back to Changes.


Master Build Step
Expand Down
13 changes: 12 additions & 1 deletion tests/changes/backends/jenkins/test_generic_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ def test_get_job_parameters(self):
assert {'name': 'SCRIPT', 'value': self.builder_options['script']} in result
assert {'name': 'CLUSTER', 'value': self.builder_options['cluster']} in result
assert {'name': 'WORK_PATH', 'value': 'foo'} in result
assert len(result) == 9
assert len(result) == 10

# test optional values
result = builder.get_job_parameters(job)
assert {'name': 'WORK_PATH', 'value': ''} in result
assert {'name': 'C_WORKSPACE', 'value': ''} in result
assert {'name': 'RESET_SCRIPT', 'value': ''} in result

def test_get_job_parameters_with_reset_script(self):
project = self.create_project()
build = self.create_build(project)
job = self.create_job(build)

builder = self.get_builder(reset_script='reset_me.sh')

result = builder.get_job_parameters(job, path='foo')
assert {'name': 'RESET_SCRIPT', 'value': 'reset_me.sh'} in result

def test_get_job_parameters_diff(self):
project = self.create_project()
Expand Down

0 comments on commit 69cac7c

Please sign in to comment.