Skip to content

Commit cc8f03f

Browse files
Merge pull request #28 from Nike-Inc/devel
Add support for downloading artifacts, deleting jobs, and installing bartlett from homebrew
2 parents 35a2b0a + aed2998 commit cc8f03f

File tree

12 files changed

+175
-27
lines changed

12 files changed

+175
-27
lines changed

README.md

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ A simple Jenkins command line client to serve your needs.
1010
- [Supported Platforms](#supported-platforms)
1111
- [Installation](#installation)
1212
- [from Homebrew](#from-homebrew)
13+
- [Updating Bartlett with Homebrew](#updating-bartlett-with-homebrew)
1314
- [from Source](#from-source)
1415
- [Getting Help](#getting-help)
1516
- [Usage](#usage)
1617
- [Getting Help at the Command Line](#getting-help-at-the-command-line)
1718
- [Querying Existing Jobs](#querying-existing-jobs)
1819
- [Triggering Job Builds](#triggering-job-builds)
1920
- [Managing Job Configurations](#managing-job-configurations)
21+
- [Deleting Existing Jobs](#deleting-existing-jobs)
22+
- [Downloading Artifacts for a Given Job](#downloading-artifacts-for-a-given-job)
2023
- [Configuring Profiles](#configuring-profiles)
2124
- [Supported Configuration Values](#supported-configuration-values)
2225
- [A note on password storage](#a-note-on-password-storage)
@@ -76,8 +79,38 @@ please check
7679

7780
### from Homebrew
7881

79-
Please track the following issue for Homebrew support:
80-
https://github.com/Nike-Inc/bartlett/issues/4
82+
Homebrew is an OSX specific application that allows users to install
83+
applications that didn't come with Apple's operating system.
84+
85+
For help installing Homebrew [see the installation instructions here.](http://brew.sh/)
86+
87+
If you haven't already, be sure to enable Nike's tap:
88+
89+
```
90+
brew tap nike-inc/nike && brew update
91+
```
92+
93+
Then install bartlett with the following command:
94+
95+
```
96+
brew install bartlett
97+
```
98+
99+
#### Updating Bartlett with Homebrew
100+
101+
Recent versions of Homebrew periodically refresh package indexes, but if you do
102+
not see the latest version of Bartlett then running the following command
103+
will force a refresh:
104+
105+
```
106+
brew update
107+
```
108+
109+
Then, upgrade to the latest version
110+
111+
```
112+
brew upgrade bartlett
113+
```
81114

82115
### from Source
83116

@@ -130,8 +163,9 @@ Available commands:
130163
info Get information on the given job
131164
build Trigger a build for the given job
132165
config Manage XML configurations for jobs
166+
artifact Download artifacts from jobs
133167
134-
Copyright (c) Nike, Inc. 2016
168+
Copyright (c) Nike, Inc. 2016-present
135169
```
136170

137171
### Querying Existing Jobs
@@ -251,6 +285,42 @@ Enter password:
251285
}
252286
```
253287

288+
#### Deleting Existing Jobs
289+
290+
You can delete an existing job by passing the `-d` flag to a `config` command:
291+
292+
```
293+
bartlett --username user --jenkins https://my-jenkins.com \
294+
config -d /path/to/job/to/delete
295+
Enter password:
296+
{
297+
"statusMessage": "OK",
298+
"statusCode": 200
299+
}
300+
```
301+
302+
### Downloading Artifacts for a Given Job
303+
304+
Artifacts can be downloaded for a given job by using the `artifact` sub-command.
305+
At this time only one artifcat may be downloaded at a time.
306+
307+
```
308+
bartlett --username my-user --jenkins https://my-jenkins.com \
309+
artifact /path/to/job my-artifact-id
310+
Enter password:
311+
echo "foo" > foo.txt
312+
```
313+
314+
Artifacts are currently sent to STDOUT, which works for simple files, but my not be
315+
desirable for larger files or binaries. It is recommended at this time to pipe
316+
artifact output directly to a file:
317+
318+
```
319+
bartlett --username my-user --jenkins https://my-jenkins.com \
320+
artifact /path/to/job my-artifact-id > my-artifact-id.txt
321+
Enter password:
322+
```
323+
254324
### Configuring Profiles
255325

256326
You may store configuration values for many different Jenkins instances. First

app/Main.hs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import qualified Bartlett.Configuration as C
88
import qualified Bartlett.Actions.Info as AI
99
import qualified Bartlett.Actions.Build as AB
1010
import qualified Bartlett.Actions.Config as AC
11+
import qualified Bartlett.Actions.Artifact as AA
1112

1213
import Control.Exception (bracket_)
1314
import Data.ByteString.Lazy.Char8 (ByteString, pack, unpack, hPutStr)
@@ -87,12 +88,17 @@ executeCommand cmd usr jenkinsInstance =
8788
AI.getInfo usr jenkinsInstance jobPaths
8889
Build jobPath jobParameters ->
8990
AB.postBuild usr jenkinsInstance jobPath jobParameters
90-
Config jobPath configFilePath ->
91-
case configFilePath of
92-
Just cp ->
93-
AC.updateConfig usr jenkinsInstance jobPath cp
94-
Nothing ->
95-
AC.getConfig usr jenkinsInstance jobPath
91+
Artifact jobPath artifactId ->
92+
AA.getArtifact usr jenkinsInstance jobPath artifactId
93+
Config deleteFlag jobPath configFilePath ->
94+
if deleteFlag
95+
then AC.deleteConfig usr jenkinsInstance jobPath
96+
else
97+
case configFilePath of
98+
Just cp ->
99+
AC.updateConfig usr jenkinsInstance (head jobPath) cp
100+
Nothing ->
101+
AC.getConfig usr jenkinsInstance (head jobPath)
96102

97103
-- | Execute the appropriate sub-command given parsed cli options.
98104
run :: Options -> IO ()

bartlett.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: bartlett
2-
version: 1.3.1
2+
version: 1.4.0
33
synopsis: The Jenkins command-line tool to serve your needs.
44
description: Please see README.md
55
homepage: https://github.com/Nike-inc/bartlett
@@ -21,7 +21,8 @@ library
2121
Bartlett.Configuration,
2222
Bartlett.Actions.Info,
2323
Bartlett.Actions.Build,
24-
Bartlett.Actions.Config
24+
Bartlett.Actions.Config,
25+
Bartlett.Actions.Artifact
2526
ghc-options:
2627
-fwarn-tabs
2728
-fwarn-unused-imports

src/Bartlett/Actions/Artifact.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{-|
2+
Module : Info
3+
Description : Methods for downloading job artifacts from Jenkins
4+
Copyright : (c) Nike, Inc., 2016-present
5+
License : BSD3
6+
Maintainer : fernando.freire@nike.com
7+
Stability : stable
8+
9+
Methods for downloading job artifacts from Jenkins.
10+
-}
11+
module Bartlett.Actions.Artifact (
12+
getArtifact
13+
) where
14+
15+
import Bartlett.Network (execRequest)
16+
import Bartlett.Types
17+
import Bartlett.Util (mkUrl)
18+
19+
import Control.Lens (set, (^.), (&))
20+
import Data.Maybe (Maybe)
21+
import qualified Data.ByteString.Lazy.Char8 as BL
22+
import Network.Wreq (responseBody, defaults, auth)
23+
24+
-- | Download an artifact from the provided job.
25+
getArtifact ::
26+
BasicAuthUser b => Maybe b -- ^ The user to authenticate with.
27+
-> JenkinsInstance -- ^ The Jenkins instance to authenticate against.
28+
-> JobPath -- ^ The job to get the artifact from.
29+
-> ArtifactId -- ^ The artifact to get from the job.
30+
-> IO ()
31+
getArtifact user base path artifactId = do
32+
resp <- execRequest Get reqOpts reqUri Nothing
33+
BL.putStrLn $ resp ^. responseBody
34+
where reqOpts = defaults & set auth (getBasicAuth <$> user)
35+
reqUri = mkUrl base path $
36+
BL.concat ["/lastSuccessfulBuild/artifact/", artifactId]

src/Bartlett/Actions/Build.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-|
22
Module : Build
33
Description : Methods for executing build requests against Jenkins
4-
Copyright : (c) Nike, Inc., 2016
4+
Copyright : (c) Nike, Inc., 2016-present
55
License : BSD3
66
Maintainer : fernando.freire@nike.com
77
Stability : stable

src/Bartlett/Actions/Config.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-|
22
Module : Config
33
Description : Methods for executing config requests against Jenkins
4-
Copyright : (c) Nike, Inc., 2016
4+
Copyright : (c) Nike, Inc., 2016-present
55
License : BSD3
66
Maintainer : fernando.freire@nike.com
77
Stability : stable
@@ -48,3 +48,16 @@ updateConfig user base path configPath = do
4848
resp <- execRequest Post reqOpts (configUri base path) (Just configFile)
4949
BL.putStrLn . encodePretty . toResponseStatus $ resp ^. responseStatus
5050
where reqOpts = defaults & set auth (getBasicAuth <$> user)
51+
52+
-- | Delete the XML configuration for the given job.
53+
deleteConfig :: BasicAuthUser a =>
54+
Maybe a -- The user to authenticate with.
55+
-> JenkinsInstance -- The Jenkins instance to interact with.
56+
-> [JobPath] -- The job for the given Jenkins instance to delete.
57+
-> IO ()
58+
deleteConfig user base [] = return ()
59+
deleteConfig user base (path:paths) = do
60+
resp <- execRequest Post reqOpts (mkUrl base path "/doDelete") Nothing
61+
BL.putStrLn . encodePretty . toResponseStatus $ resp ^. responseStatus
62+
deleteConfig user base paths
63+
where reqOpts = defaults & set auth (getBasicAuth <$> user)

src/Bartlett/Actions/Info.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-|
22
Module : Info
33
Description : Methods for executing informational requests against Jenkins
4-
Copyright : (c) Nike, Inc., 2016
4+
Copyright : (c) Nike, Inc., 2016-present
55
License : BSD3
66
Maintainer : fernando.freire@nike.com
77
Stability : stable

src/Bartlett/Configuration.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-|
22
Module : Configuration
33
Description : Configuration management for Bartlett
4-
Copyright : (c) Nike, Inc., 2016
4+
Copyright : (c) Nike, Inc., 2016-present
55
License : BSD3
66
Maintainer : fernando.freire@nike.com
77
Stability : stable

src/Bartlett/Network.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{-|
44
Module : Network
55
Description : General network related methods used throughout Bartlett
6-
Copyright : (c) Nike, Inc., 2016
6+
Copyright : (c) Nike, Inc., 2016-present
77
License : BSD3
88
Maintainer : fernando.freire@nike.com
99
Stability : stable
@@ -20,7 +20,7 @@ module Bartlett.Network (
2020
)where
2121

2222
import qualified Bartlett.Util as BU
23-
import Bartlett.Types (RequestType(Get, Post), JenkinsInstance)
23+
import Bartlett.Types (RequestType(..), JenkinsInstance)
2424

2525
import qualified Control.Exception as E
2626
import Control.Lens ((.~), (^?), (&))
@@ -31,7 +31,7 @@ import Data.ByteString.Lazy.Char8 (ByteString, unpack, toStrict)
3131
import Data.Maybe (fromMaybe)
3232
import qualified Network.HTTP.Client as NHC
3333
import System.Exit (die)
34-
import Network.Wreq (Options, Response, param, responseBody, header, defaults)
34+
import Network.Wreq (Options, Response, param, responseBody, header)
3535
import qualified Network.Wreq.Session as S
3636

3737

@@ -48,7 +48,7 @@ requestCSRFToken sess opts jenkins = do
4848
Left _ ->
4949
return (Nothing, Nothing)
5050
Right r ->
51-
return $
51+
return
5252
(BU.toByteString <$> (r ^? responseBody . key (BU.toText "crumbRequestField") . _String),
5353
BU.toByteString <$> (r ^? responseBody . key (BU.toText "crumb") . _String))
5454
where reqUri = BU.setPath jenkins "/crumbIssuer/api/json"

src/Bartlett/Parsers.hs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-|
22
Module : Parsers
33
Description : Parsers used to extract command line options at invocation
4-
Copyright : (c) Nike, Inc., 2016
4+
Copyright : (c) Nike, Inc., 2016-present
55
License : BSD3
66
Maintainer : fernando.freire@nike.com
77
Stability : stable
@@ -39,7 +39,7 @@ withInfo opts desc = info (helper <*> opts)
3939
(fullDesc
4040
<> progDesc (unpack desc)
4141
<> header "bartlett - the Jenkins command-line tool to serve your needs."
42-
<> footer "Copyright (c) Nike, Inc. 2016")
42+
<> footer "Copyright (c) Nike, Inc. 2016-present")
4343

4444
-- | Parse a credentials flag.
4545
parseRefreshCredentials :: Parser Bool
@@ -77,6 +77,12 @@ parseConfigFilePath = option str $
7777
short 'f' <> long "filepath" <> metavar "CONFIG_FILE_PATH" <>
7878
help "Path to the job configuration to upload"
7979

80+
-- | Parse whether we should delete the given resource.
81+
parseDeleteFlag :: Parser DeleteFlag
82+
parseDeleteFlag = switch $
83+
short 'd' <> long "delete"
84+
<> help "Delete the given job path"
85+
8086
-- | Parse an Info sub-command.
8187
parseInfo :: Parser Command
8288
parseInfo = Info <$> some (argument readerByteString (metavar "JOB_PATHS..."))
@@ -90,15 +96,23 @@ parseBuild = Build
9096
-- | Parse a Config sub-command.
9197
parseConfig :: Parser Command
9298
parseConfig = Config
93-
<$> argument readerByteString (metavar "JOB_PATH")
99+
<$> parseDeleteFlag
100+
<*> some (argument readerByteString (metavar "JOB_PATH..."))
94101
<*> optional parseConfigFilePath
95102

103+
-- | Parse an Artifact sub-command.
104+
parseArtifact :: Parser Command
105+
parseArtifact = Artifact
106+
<$> argument readerByteString (metavar "JOB_PATH")
107+
<*> argument readerByteString (metavar "ARTIFACT_ID")
108+
96109
-- | Parse a Command.
97110
parseCommand :: Parser Command
98111
parseCommand = subparser $
99112
command "info" (parseInfo `withInfo` "Get information on the given job")
100113
<> command "build" (parseBuild `withInfo` "Trigger a build for the given job")
101114
<> command "config" (parseConfig `withInfo` "Manage XML configurations for jobs")
115+
<> command "artifact" (parseArtifact `withInfo` "Download artifacts from jobs")
102116

103117
-- | Combinator for all command line options.
104118
parseOptions :: Parser Options

0 commit comments

Comments
 (0)