Skip to content

Commit

Permalink
Merge pull request #1 from GoldenspearLLC/add_files
Browse files Browse the repository at this point in the history
Add files
  • Loading branch information
guillempuigcom authored Aug 19, 2020
2 parents 60a079c + ab79b6a commit f236251
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Container image that runs your code
FROM python:3.8

#install pre-requisites
RUN apt-get update

RUN pip install --upgrade pip

# Copies your code file from your action repository to the filesystem path `/` of the container
COPY entrypoint.py /entrypoint.py
COPY requirements.txt /requirements.txt

RUN chmod +x /entrypoint.py

WORKDIR /

RUN pip install -r requirements.txt

# Code file to execute when the docker container starts up (`entrypoint.py`)
ENTRYPOINT ["/entrypoint.py"]

67 changes: 65 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,65 @@
# build-jenkins-job
Action to trigger and build a jenkins job from github workflow
# Build jenkins job from Github Action :rocket:

This action builds/triggers a jenkins job, waiting it to be finished and enabling to pass job params.

## Inputs

### `jenkins-token`

**Required**

### `jenkins-url`

**Required**

### `jenkins-user`

**Required**

### `job-path`

**Required**

E.g.
```
if job inside folder:
"job/folder_name/job/job_name"
if job in jenkins root:
"job/job_name"
```

### `job-params`

**Not mandatory**

Set jenkins params as JSON string:

E.g.
```
"{'param1': 'value1', 'param2': 'value2'}"
```


## Outputs

### `status/result`

* FAILURE
* SUCCESS
* ABORTED


## Example usage
```
- name: "Trigger jenkins job"
uses: ./.github/actions/jenkins # Uses an action in the root directory
with:
jenkins-url: ${{ secrets.JENKINS_URL }}
jenkins-token: ${{ secrets.JENKINS_TOKEN }}
user: "jenkins-username"
job-path: "job/folder_name/job/job_name"
job-params: "{'param1': 'value1', 'param2': 'value2'}"
- name: Get job status
run: echo "Job status is ${{ steps.job-build.outputs.job_status }}"
```
31 changes: 31 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# action.yml
name: 'Build Jenkins job'
description: 'Trigger jenkins job'
inputs:
jenkins-url:
description: 'jenkins server url'
required: true
jenkins-token:
description: 'token to access jenkins server'
required: true
user:
description: "user name"
required: true
job-path:
description: 'job/folder_name/job/job_name'
required: true
job-params:
description: "params to run the job. JSON string. i.e '{'param1': 'value1', 'param2': 'value2'}' "
required: false
outputs:
job_status:
description: 'Build status'
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.jenkins-url}}
- ${{ inputs.jenkins-token}}
- ${{ inputs.user}}
- ${{ inputs.job-path }}
- ${{ inputs.job-params}}
59 changes: 59 additions & 0 deletions entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3

# use of https://python-jenkins.readthedocs.io/en/latest/index.html

import sys
import requests
import jenkins
import time
import json

JENKINS_URL = sys.argv[1]
JENKINS_TOKEN = sys.argv[2]
JENKINS_USER = sys.argv[3]
JOB_PATH = sys.argv[4]
JOB_PARAMS = sys.argv[5]

# create/connect jenkins server
server = jenkins.Jenkins(f"http://{JENKINS_URL}", username=JENKINS_USER, password=JENKINS_TOKEN)
user = server.get_whoami()
version = server.get_version()
print(f"Hello {user['fullName']} from Jenkins {version}")

# build job
split = JOB_PATH.split("job/")
job_name = "".join(split)
server.build_job(job_name, parameters=json.loads(JOB_PARAMS), token=JENKINS_TOKEN)
queue_info = server.get_queue_info()
queue_id = queue_info[0].get('id')

# define url to request build_number
url = f"http://{JENKINS_USER}:{JENKINS_TOKEN}@{JENKINS_URL}/queue/item/{queue_id}/api/json?pretty=true"


def get_trigger_info(url: str):
trigger_info = requests.get(url).json()
return trigger_info


while "executable" not in (info := get_trigger_info(url)):
time.sleep(3)

build_number = info["executable"]["number"]
print(f"BUILD NUMBER: {build_number}")


def get_status(name: str, number: int) -> str:
build_info = server.get_build_info(name=name, number=number)
job_status = build_info["result"]
return job_status


while not (status := get_status(job_name, build_number)):
time.sleep(1)

print(f"Job status is : {status}")
print(f"::set-output name=job_status::{status}")

if status != 'SUCCESS':
exit(1)
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
python_jenkins==1.7.0
requests==2.24.0
jenkins==1.0.2

0 comments on commit f236251

Please sign in to comment.