Skip to content

Commit 7453507

Browse files
committed
Merge pull request #21 from LD250/build-statuses
Pre-release version
2 parents 82b4893 + 0228766 commit 7453507

6 files changed

Lines changed: 318 additions & 108 deletions

File tree

README.md

Lines changed: 127 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,146 @@
44
[![Code Health](https://landscape.io/github/LD250/jenkins-cli-python/master/landscape.svg?style=flat)](https://landscape.io/github/LD250/jenkins-cli-python/master)
55
[![Requirements Status](https://requires.io/github/LD250/jenkins-cli-python/requirements.svg?branch=master)](https://requires.io/github/LD250/jenkins-cli-python/requirements/?branch=master)
66

7-
# Install:
7+
**Based on**
8+
[python-jenkins](https://github.com/openstack/python-jenkins)
9+
10+
# Tested on
11+
Jenkins ver: 1.565, 1.655
12+
13+
Python ver: 2.7, 3.4, 3.5
14+
15+
# Table of contents
16+
* [Installation](#installation)
17+
* [Commands overview](#commands-overview)
18+
* [Usage example](#usage-example)
19+
* [Tests](#tests)
20+
21+
# Installation:
22+
23+
```bash
24+
pip isntall
25+
```
26+
27+
or
28+
829
```bash
930
git clone https://github.com/LD250/jenkins-cli-python.git
1031
cd jenkins-cli-python
1132
python setup.py install
1233
```
1334

14-
# Usage:
15-
16-
```bash
17-
jenkins [-h] [--host jenkins-url] [--username username]
18-
[--password password]
19-
{jobs,queue,building,start,info,set_branch,stop,console} ...
20-
```
35+
## Configuration file (.jenkins-cli)
2136

22-
Server URL, Username and password may be specified either by the command line arguments or in configuration file **(.jenkins-cli)**. Command line arguments has the highest priority, after that the **.jenkins-cli** file from current folder is taking into account. If there is no.jenkins-cli file in current folder, settings will be read from **.jenkins-cli** from the home folder
37+
Host, username and password may be specified either by the command line arguments or in the configuration file **(.jenkins-cli)**. Command line arguments have the highest priority, after that the **.jenkins-cli** file from current folder is used. If there is no.jenkins-cli file in the current folder, settings will be read from **.jenkins-cli** located in the home folder
2338

24-
# Optional arguments:
25-
```bash
26-
-h, --help show this help message and exit
27-
--host jenkins-url Jenkins Server Url
28-
--username username Jenkins Username
29-
--password password Jenkins Password
39+
**.jenkins-cli** example
40+
```txt
41+
host=http://localhost:8082/
42+
username=username
43+
password=******
3044
```
3145

32-
# Available commands:
33-
```bash
34-
{jobs,queue,building,start,info,set_branch,stop,console}
46+
# Commands overview:
3547
jobs Show all jobs and their statuses
36-
queue Shows builds queue
48+
queue Show builds queue
3749
building Build executor status
3850
start Start job
3951
info Job info
40-
set_branch Set SCM branch
52+
setbranch Set VCS branch (Mercurial or Git)
4153
stop Stop job
42-
console Show console for last build
54+
console Show console for the build
55+
builds Show builds for the job
56+
changes Show build's changes
57+
Run `jenkins --help` for detailed help. To view optional parameters, run `--help` for the specific command. For example `jenkins jobs --help` will show job status description and optional arguments.
58+
59+
60+
# Usage example:
61+
62+
Show status descriptions
63+
```bash
64+
$ jenkins jobs --help
65+
usage: jenkins jobs [-h] [-a] [-p]
66+
67+
Status description:
68+
69+
... -> Unknown
70+
F.. -> Failed
71+
D.. -> Disabled
72+
U.. -> Unstable
73+
N.. -> Not built
74+
S.. -> Stable
75+
A.. -> Aborted
76+
.>> -> Build in progress
77+
78+
optional arguments:
79+
-h, --help show this help message and exit
80+
-a show only active jobs
81+
-p show only jobs in build progress
82+
```
83+
Show jobs
84+
```bash
85+
$ jenkins jobs
86+
D.. hudson
87+
S.. jenkins-cli
88+
U>> new-project
4389
```
90+
Show job info
91+
```bash
92+
$ jenkins info jenkins-cli
93+
Last build name: jenkins-cli #18 (result: SUCCESS)
94+
Last success build name: jenkins-cli #18
95+
Build started: 2016-03-31 00:22:38.326999
96+
Building now: No
97+
Git branch set to: master
98+
```
99+
Update VCS branch
100+
```bash
101+
$ jenkins setbranch new-feature
102+
Done
103+
```
104+
Run job
105+
```bash
106+
$ jenkins start jenkins-cli
107+
jenkins-cli: started
108+
```
109+
View job builds
110+
```bash
111+
$ jenkins builds jenkins-cli
112+
S.. #18 0:00:07 (2 commits)
113+
S.. #17 0:00:08 (2 commits)
114+
F.. #16 0:00:00 (4 commits)
115+
S.. #15 0:00:08 (2 commits)
116+
```
117+
Show previous build changes
118+
```bash
119+
$ jenkins changes jenkins-cli -b 17
120+
1. add .travis.yml from add-travis branch by Denys Levchenko affected 1 files
121+
2. scaffolding for tests by Denys Levchenko affected 5 files
122+
```
123+
Show current job console output (last 13 lines)
124+
```bash
125+
$ jenkins console jenkins-cli -n 13
126+
test_jobs (tests.test_cli.TestCliCommands) ... ok
127+
test_queue (tests.test_cli.TestCliCommands) ... ok
128+
test_set_branch (tests.test_cli.TestCliCommands) ... ok
129+
test_start (tests.test_cli.TestCliCommands) ... ok
130+
test_stop (tests.test_cli.TestCliCommands) ... ok
131+
test_read_settings_from_file (tests.test_cli.TestCliFileUsing) ... ok
132+
133+
----------------------------------------------------------------------
134+
Ran 12 tests in 0.015s
135+
136+
OK
137+
Finished: SUCCESS
138+
```
139+
140+
# Tests
141+
142+
To perform flake8 checks and run tests similar to Travis, do the following
143+
144+
```bash
145+
pip install -r requirements.txt
146+
tox
147+
```
148+
149+

jenkins_cli/__init__.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,71 @@
1+
from __future__ import print_function
2+
13
import argparse
24
from jenkins import JenkinsException
35

46
from jenkins_cli.cli import JenkinsCli, CliException, get_jobs_legend
7+
from jenkins_cli.version import version
58

69

710
def main():
811
parser = argparse.ArgumentParser(prog='jenkins',
9-
description='Server URL, Username and password may be specified either by the command line arguments '
10-
'or in configuration file (.jenkins-cli). Command line arguments has the highest priority, '
11-
'after that the .jenkins-cli file from current folder is taking into account. If there is no'
12-
'.jenkins-cli file in current folder, setiings will be read from .jenkins-cli from the home'
12+
description='Host, username and password may be specified either by the command line arguments '
13+
'or in the configuration file (.jenkins-cli). Command line arguments have the highest priority, '
14+
'after that the .jenkins-cli file from current folder is used. If there is no'
15+
'.jenkins-cli file in the current folder, settings will be read from .jenkins-cli located in the home'
1316
'folder')
14-
parser.add_argument('--host', metavar='jenkins-url', help='Jenkins Server Url', default=None)
17+
parser.add_argument('--host', metavar='jenkins-url', help='Jenkins Host', default=None)
1518
parser.add_argument('--username', metavar='username', help='Jenkins Username', default=None)
1619
parser.add_argument('--password', metavar='password', help='Jenkins Password', default=None)
20+
parser.add_argument('--version', '-v', action='version', version='jenkins-cli %s' % version)
1721

1822
subparsers = parser.add_subparsers(title='Available commands', dest='jenkins_command')
1923

2024
jobs_parser = subparsers.add_parser('jobs',
2125
help='Show all jobs and their statuses',
2226
formatter_class=argparse.RawTextHelpFormatter,
2327
description="Status description:\n\n" + "\n".join(get_jobs_legend()))
24-
jobs_parser.add_argument('-a', help='Show only active jobs', default=False, action='store_true')
28+
jobs_parser.add_argument('-a', help='show only active jobs', default=False, action='store_true')
29+
jobs_parser.add_argument('-p', help='show only jobs in build progress', default=False, action='store_true')
2530

26-
subparsers.add_parser('queue', help='Shows builds queue')
31+
subparsers.add_parser('queue', help='Show builds queue')
2732

2833
subparsers.add_parser('building', help='Build executor status')
2934

35+
builds_parser = subparsers.add_parser('builds', help='Show builds for the job')
36+
builds_parser.add_argument('job_name', help='Job name of the builds')
37+
3038
start_parser = subparsers.add_parser('start', help='Start job')
3139
start_parser.add_argument('job_name', help='Job to start', nargs='*')
3240

3341
start_parser = subparsers.add_parser('info', help='Job info')
34-
start_parser.add_argument('job_name', help='Job to to get info for')
42+
start_parser.add_argument('job_name', help='Job to get info for')
3543

36-
set_branch = subparsers.add_parser('setbranch', help='Set SCM branch')
37-
set_branch.add_argument('job_name', help='Job to to set branch')
38-
set_branch.add_argument('branch_name', help='Name of the SCM branch')
44+
set_branch = subparsers.add_parser('setbranch', help='Set VCS branch (Mercurial or Git)')
45+
set_branch.add_argument('job_name', help='Job to set branch for')
46+
set_branch.add_argument('branch_name', help='Name of the VCS branch')
3947

4048
stop_parser = subparsers.add_parser('stop', help='Stop job')
4149
stop_parser.add_argument('job_name', help='Job to stop')
4250

43-
console_parser = subparsers.add_parser('console', help='Show console for last build')
51+
console_parser = subparsers.add_parser('console', help='Show console for the build')
4452
console_parser.add_argument('job_name', help='Job to show console for')
45-
console_parser.add_argument('-n', help='Show first n num of the lines only(if n is negative, shows last n lines)', type=int)
46-
console_parser.add_argument('-i', help='Interactive console', default=False, action='store_true')
53+
console_parser.add_argument('-b', '--build', help='job build number to show console for (if omitted, last build number is used)', default='')
54+
console_parser.add_argument('-n', help='show first n lines only(if n is negative, show last n lines)', type=int)
55+
console_parser.add_argument('-i', help='interactive console', default=False, action='store_true')
56+
57+
console_parser = subparsers.add_parser('changes', help="Show build's changes")
58+
console_parser.add_argument('job_name', help='Job to show changes for')
59+
console_parser.add_argument('-b', '--build', help='job build number to show changes for (if omitted, last build number is used)', default='')
4760

4861
args = parser.parse_args()
4962
try:
50-
JenkinsCli(args).run_command(args)
63+
if args.jenkins_command is None:
64+
parser.print_help()
65+
else:
66+
JenkinsCli(args).run_command(args)
5167
except JenkinsException as e:
52-
print(e)
68+
print("Jenkins server response: %s:" % e)
5369
except KeyboardInterrupt:
5470
print("Aborted")
5571
except CliException as e:

0 commit comments

Comments
 (0)