Skip to content

Commit

Permalink
Merge pull request #2 from BramDriesen/develop
Browse files Browse the repository at this point in the history
Added support for subfolders in Jenkins.
  • Loading branch information
BramDriesen authored Dec 12, 2020
2 parents 1d97521 + 0c0767b commit 5cadece
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sudo bash -c "$(curl -sL https://raw.githubusercontent.com/BramDriesen/rpi-jenki

Install the [Jenkinsapi][1] Python library
```sh
sudo pip install jenkinsapi --upgrade
sudo pip3 install jenkinsapi --upgrade
```

Install the [Automation HAT/pHAT][4] library
Expand All @@ -43,7 +43,9 @@ cp default-config.py config.py
Edit the configuration file with your information
- Jenkins URL
- Username and Password.
- Jobs (needs to be an array, for 1 item the structure looks like this `jobs = ['job-name-1']` )
- Jobs
- Needs to be an array even if you have 1 item
- Supports sub-folders by using a `/` in the name e.g. `folder/job/master`
- GPIO outputs (only needed fot the DIY HAT)

#### Configuration file for the **Automation HAT/pHAT**
Expand All @@ -70,20 +72,20 @@ gpios = {

Make sure to enable the setting "Wait for network on boot" in the Raspberry Pi config screen. Use `sudo raspi-config` to go to the settings. Also set the configuration to boot into the terminal.

Edit your `rc.local` file to make the script run at boot. Edit it using the command:
Edit your `rc.local` (or use any other method as [described here][6]) file to make the script run at boot. Edit it using the command:
```sh
sudo nano /etc/rc.local
```
Using your cursor keys scroll to the bottom and add the following line :

For the Automation HAT/pHAT:
```sh
python home/pi/rpi-jenkins-tower-light/jenkins_tower_light_hat.py &
python3 home/pi/rpi-jenkins-tower-light/jenkins_tower_light_hat.py &
```

For the DIY HAT
```sh
python home/pi/rpi-jenkins-tower-light/jenkins_tower_light_gpio.py &
python3 home/pi/rpi-jenkins-tower-light/jenkins_tower_light_gpio.py &
```

Note: If you cloned the directory in a different location be sure to change this path to correspond with your location.
Expand All @@ -105,11 +107,13 @@ At startup of the scripts all light's will be toggled once.
- Red: An error occurred (connection or authentication)
- Yellow: One or more jobs are building

### Features to add / Todo list
- [ ] Improve code (Mainly blinking functions)
- [x] Installation script
- [ ] Web interface to configure the settings (probably not going to do this)
- [ ] Find a use case for the buzzer.
### Features to add / Todo list / Contributing
If you want to help contributing to this library have a look at the following list:

- [ ] Improve code (Mainly blinking functions).
- [ ] Find a use case for the buzzer (if any).
- [ ] Check if it's possible to use other Jenkins authentication methods
- [ ] Some form of automated test/code coverage.

### Extra information
The tower light I am using can be bought from [Adafruit][2] or other resellers that handle Adafruit products like [Pimoroni][3] where I got mine. You can probably also use other types of tower lights but be careful with operating voltages since most of the tower lights are meant for industrial applications.
Expand All @@ -129,3 +133,4 @@ The original version of this project was featured in [The MagPi Issue 46][5]!
[3]: https://shop.pimoroni.com/products/tower-light-red-yellow-green-alert-light-with-buzzer-12vdc
[4]: https://github.com/pimoroni/automation-hat
[5]: https://www.raspberrypi.org/magpi/issues/46/
[6]: https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/
20 changes: 11 additions & 9 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ is_root(){
}

if_pip(){
if command -v pip --version >/dev/null; then
if command -v pip3 --version >/dev/null; then
:
else
$red
echo "Pip not installed"
echo "Pip3 not installed"
$yellow
echo "Installing pip ..."
echo "Installing pip3 ..."
echo
$reset
apt-get update && apt-get install python-pip
apt-get update && apt-get install python3-pip
fi
}

install_pip_libs(){
$green
echo "Installing jenkinsapi python library..."
echo "Installing 'jenkinsapi' Python library..."
$reset
if pip install jenkinsapi --upgrade; then
if pip3 install jenkinsapi --upgrade; then
:
else
$red
Expand All @@ -65,9 +65,9 @@ clone_repo(){
:
else
$red
echo "Git not installed"
echo "GIT not installed"
$yellow
echo "Installing pip ..."
echo "Installing GIT ..."
echo
$reset
apt-get update && apt-get install git
Expand Down Expand Up @@ -174,7 +174,9 @@ enable_service(){
:
else
echo "Enabling Jenkins Tower Light to start at boot..."
echo "python /home/pi/${repo_dir}/jenkins_tower_light_hat.py &" >> /etc/rc.local
sed -e s/exit 0//g -i *
echo "python3 /home/pi/${repo_dir}/jenkins_tower_light_hat.py &" >> /etc/rc.local
echo "exit 0" >> /etc/rc.local
fi
$reset
}
Expand Down
23 changes: 18 additions & 5 deletions jenkins_tower_light_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import jenkinsapi
import config as cfg
import signal
import copy
from jenkinsapi.jenkins import Jenkins
from requests.exceptions import ConnectionError

# Set the GPIO mode.
GPIO.setmode(GPIO.BCM)

# Get a GPIO code for an string


# Get a GPIO code for a string
def getcode(value):
value = value.lower()
return cfg.gpios.get(value, cfg.gpios.get('red'))


# GPIO Setup
GPIO.setwarnings(False)
GPIO.setup(getcode('red'), GPIO.OUT)
Expand Down Expand Up @@ -78,6 +78,19 @@ def set_error(value):
print("[ERROR] Only supply True or False to the setError function")


# Get all the jobs by a given name.
def get_jenkins_job_by_name(name):
# Support sub-folders.
if '/' in name:
folder, name = name.rsplit('/', 1)
server = copy.deepcopy(J)
folder_path = '/'.join([f'job/{x}' for x in folder.split('/')])
server.baseurl = server.baseurl + '/' + folder_path
return server.get_job(name)
else:
return J.get_job(name)


def check_jobs_build_status():
jobs = cfg.jobs
success = 0
Expand All @@ -86,7 +99,7 @@ def check_jobs_build_status():

for item in jobs:
try:
job = J.get_job(item)
job = get_jenkins_job_by_name(item)
except jenkinsapi.custom_exceptions.UnknownJob:
set_error(True)
else:
Expand Down Expand Up @@ -116,7 +129,7 @@ def check_jobs_building():

for item in jobs:
try:
job = J.get_job(item)
job = get_jenkins_job_by_name(item)
except jenkinsapi.custom_exceptions.UnknownJob:
set_error(True)
else:
Expand Down
18 changes: 16 additions & 2 deletions jenkins_tower_light_hat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import config as cfg
import signal
import automationhat
import copy
from jenkinsapi.jenkins import Jenkins
from requests.exceptions import ConnectionError

Expand Down Expand Up @@ -91,6 +92,19 @@ def set_error(value):
print("[ERROR] Only supply True or False to the setError function")


# Get all the jobs by a given name.
def get_jenkins_job_by_name(name):
# Support sub-folders.
if '/' in name:
folder, name = name.rsplit('/', 1)
server = copy.deepcopy(J)
folder_path = '/'.join([f'job/{x}' for x in folder.split('/')])
server.baseurl = server.baseurl + '/' + folder_path
return server.get_job(name)
else:
return J.get_job(name)


def check_jobs_build_status():
if automationhat.is_automation_hat():
automationhat.light.comms.on()
Expand All @@ -101,7 +115,7 @@ def check_jobs_build_status():

for item in jobs:
try:
job = J.get_job(item)
job = get_jenkins_job_by_name(item)
except jenkinsapi.custom_exceptions.UnknownJob:
set_error(True)
else:
Expand Down Expand Up @@ -135,7 +149,7 @@ def check_jobs_building():

for item in jobs:
try:
job = J.get_job(item)
job = get_jenkins_job_by_name(item)
except jenkinsapi.custom_exceptions.UnknownJob:
set_error(True)
else:
Expand Down

0 comments on commit 5cadece

Please sign in to comment.