├── vars
├── unstashParam.txt
└── unstashParam.groovy
├── README.md
└── license.txt
/vars/unstashParam.txt:
--------------------------------------------------------------------------------
1 | unstashParam(paramname[, filename])
2 |
3 |
4 | Save the file passed as build parameter named paramname
5 | to a current workspace. Returns the path to that file relative to the
6 | current workspace.
7 |
8 |
9 |
10 | If filename is specified, the file is saved under that
11 | name (again, relative to the workspace). Otherwise original filename
12 | is used.
13 |
14 |
15 |
16 | Throws an error (by means of error pipeline step) if:
17 |
18 |
19 | - no build parameter named
paramname exists,
20 | - parameter is not a file parameter,
or
21 | unstathParam is not called within a workspace on a node .
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Jenkins `unstashParam` Library
2 |
3 | This [pipeline library][2] contains a workaround for Jenkins issue [ JENKINS-27413][1]. It provides a new step `unstashParam` that saves file parameter to a workspace.
4 |
5 | ## Configuration
6 |
7 | See [Jenkins User Handbook, chapter Extending with Shared Libraries][2] on how to configure pipeline libraries.
8 |
9 | ## Usage
10 |
11 | In a pipeline script:
12 |
13 | library "jenkinsci-unstashParam-library"
14 | node {
15 | def file_in_workspace = unstashParam "file"
16 | sh "cat ${file_in_workspace}"
17 | }
18 |
19 | The above assumes the Jenkins job has a file build parameter named `file`.
20 |
21 | ## License
22 |
23 | This library is MIT licensed. See `license.txt` for details.
24 |
25 | [1]: https://issues.jenkins-ci.org/browse/JENKINS-27413
26 | [2]: https://jenkins.io/doc/book/pipeline/shared-libraries/
27 |
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 Palantir Solutions
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/vars/unstashParam.groovy:
--------------------------------------------------------------------------------
1 | import hudson.FilePath
2 | import hudson.model.ParametersAction
3 | import hudson.model.FileParameterValue
4 | import hudson.model.Executor
5 |
6 | def call(String name, String fname = null) {
7 | def paramsAction = currentBuild.rawBuild.getAction(ParametersAction.class);
8 | if (paramsAction != null) {
9 | for (param in paramsAction.getParameters()) {
10 | if (param.getName().equals(name)) {
11 | if (! (param instanceof FileParameterValue)) {
12 | error "unstashParam: not a file parameter: ${name}"
13 | }
14 | if (!param.getOriginalFileName()) {
15 | error "unstashParam: file was not uploaded"
16 | }
17 | if (env['NODE_NAME'] == null) {
18 | error "unstashParam: no node in current context"
19 | }
20 | if (env['WORKSPACE'] == null) {
21 | error "unstashParam: no workspace in current context"
22 | }
23 |
24 | if (env['NODE_NAME'].equals("master") || env['NODE_NAME'].equals("built-in")) {
25 | workspace = new FilePath(null, env['WORKSPACE'])
26 | } else {
27 | workspace = new FilePath(Jenkins.getInstance().getComputer(env['NODE_NAME']).getChannel(), env['WORKSPACE'])
28 | }
29 |
30 | filename = fname == null ? param.getOriginalFileName() : fname
31 | file = workspace.child(filename)
32 |
33 | destFolder = file.getParent()
34 | destFolder.mkdirs()
35 |
36 | file.copyFrom(param.getFile())
37 | return filename;
38 | }
39 | }
40 | }
41 | error "unstashParam: No file parameter named '${name}'"
42 | }
43 |
44 |
--------------------------------------------------------------------------------