Skip to content

Commit

Permalink
add parameters to action. Add option to not wait for job completion. …
Browse files Browse the repository at this point in the history
…Fix logging.
  • Loading branch information
geokats7 committed Dec 24, 2021
1 parent 2a9bb27 commit 05874e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@
jenkins_password:
description: 'Jenkins password'
required: true
jenkins_job_parameters:
description: 'Jenkins job parameters'
required: false
wait_for_result:
description: 'Wait for result'
required: false
default: 'True'

runs:
image: "Dockerfile"
using: "docker"
args:
- ${{ inputs.jenkins_job_name }}
- ${{ inputs.jenkins_job_parameters }}
- ${{ inputs.wait_for_result }}
- --jenkins_base_url=${{ inputs.jenkins_base_url }}
- --jenkins_user=${{ inputs.jenkins_user }}
- --jenkins_password=${{ inputs.jenkins_password }}
16 changes: 7 additions & 9 deletions jenkins_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@ def list_jobs(self) -> list:
job_names_list = [item[0] for item in self._jenkins.items()]
return job_names_list

def start_job(self, job_name: str, params: dict = None):
def start_job(self, job_name: str, params: dict = None, wait_for_result: bool = True):
"""Start a job and poll it until it's over or timed out."""
if params is not None:
if type(params) is not dict:
print(type(params))
raise AttributeError("The parameters should be entered in a dictionary")
if params is not None and type(params) is not dict:
print(type(params))
raise TypeError(f"The parameters should be entered as a dictionary.\nParameters given: {params}.\nHint: Check for missing quotation.")
job = self._jenkins[job_name]
queue_item = job.invoke(build_params=params)
logging.info("Job entered queue")
# build = queue_item.block_until_building()
build = self._poll_job_queue(queue_item)
logging.info(f"Job started building [Build no. {queue_item.get_build_number()}]")
logging.info(f"Estimated duration -> {str(datetime.timedelta(seconds=build.get_estimated_duration())).split('.')[0]}")
self._poll_build_for_status(build)
logging.info(build.get_status())
if wait_for_result:
self._poll_build_for_status(build)

def _poll_job_queue(self, queue_item: QueueItem):
elapsed_time = 0
Expand Down Expand Up @@ -82,7 +80,7 @@ def _poll_build_for_status(self, build: Build):
elif result == 'FAILURE':
# Do failure steps
logging.info(f"{time.ctime()} | Job: {build.job.name} | Status: {result}")
logging.info(f"View the job logs here: {build.get_build_url()}")
logging.info(f"View more details here: {build.get_build_url()}")
sys.exit(1)
elif result == 'ABORTED':
# Do aborted steps
Expand Down

0 comments on commit 05874e3

Please sign in to comment.