Skip to content

Commit

Permalink
jenkins-pipeline-example
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalbe4 committed May 21, 2016
1 parent aa7fc9a commit fa5201a
Showing 1 changed file with 37 additions and 38 deletions.
75 changes: 37 additions & 38 deletions jenkins-pipeline-examples/README.md
@@ -1,38 +1,38 @@
# Jenkins Pipeline - Working with Artifactory
# Jenkins Pipeline - Working With Artifactory

## Introduction
The Pipeline Jenkins Plugin allows you to create a script that defines your build steps.
For those of you who are not familiar with Jenkins Pipeline, please check out the [Pipeline tutorial](https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md) or the [Getting started with Pipeline](https://jenkins.io/doc/pipeline/) documentation.
We have recently added support for Artifactory operations as part of the Pipeline script DSL.
You can now download dependencies, upload artifacts and publish build-info to Artifactory from a Pipeline script.
The Pipeline support for Artifactory is provided by the [Jenkins Artifactory Plugin](https://github.com/JFrogDev/jenkins-artifactory-plugin).
The official release of the Jenkins Artifactory Plugin with Pipeline support hasn't been published yet,
but it should become available soon.
We encourage you however to try out the plugin snapshot.
For those not familiar with Jenkins Pipeline, check out the [Pipeline Tutorial](https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md) or the [Getting Started With Pipeline](https://jenkins.io/doc/pipeline/) documentation.
We recently added support for Artifactory operations as part of the Pipeline script DSL.
You have the added option of downloading dependencies, uploading artifacts, and publishing build-info to Artifactory from a Pipeline script.
Pipeline support for Artifactory is provided by the [Jenkins Artifactory Plugin](https://github.com/JFrogDev/jenkins-artifactory-plugin).
The official release of the Jenkins Artifactory Plugin with Pipeline support has not been published yet,
but will become available within the coming weeks.
In the meantime, we encourage you to try out the plugin snapshot.

## Installing the Plugin Snapshot
1. Make sure that the Pipeline Plugin is installed on your Jenkins instance.
2. Download the the Artifactory Plugin snapshot from [here](https://bintray.com/jfrog/jfrog-jars/download_file?file_path=artifactory.hpi).
3. Install the Artifactory Plugin you downloaded by following these steps:
1. Ensure that the Pipeline Plugin is installed on your Jenkins instance.
2. Download the the Artifactory Plugin snapshot from [this link](https://bintray.com/jfrog/jfrog-jars/download_file?file_path=artifactory.hpi).
3. To install the Artifactory Plugin you downloaded, follow these steps:
* Go to ‘Manage Jenkins’ --> ‘Manage Plugins’ --> ‘Advanced’ tab.
* In the ‘Upload Plugin’ section, select the path to the artifactory.hpi file and click ‘Upload’
* Restart Jenkins
* In the ‘Upload Plugin’ section, select the path to the artifactory.hpi file and click ‘Upload’.
* Restart Jenkins.

## Using the Artifactory DSL
### Creating an Artifactory Server Instance
In order to download or upload files from and to your Artifactory server, you first
need to create an Artifactory server instance in your PipeLine script.
If your Artifactory server is already defined in Jenkins, all you need is the server ID.
To get or set your Artifactory server ID, go to Manage --> Configure System.
To upload or download files to and from your Artifactory server, first you need to
create an Artifactory server instance in your PipeLine script.
If your Artifactory server is pre-defined in Jenkins, all you need is the server ID.
To obtain or set your Artifactory server ID, go to Manage --> Configure System.
Then, you can create your Artifactory server instance by adding the following line to your script:
```
def server = Artifactory.server('my-server-id')
```
### Downloading and Uploading Files From and To Artifactory
In order to download files from an Artifactory server, we first need to create a spec,
### Uploading and Downloading Files To and From Artifactory
Once you have created an Artifactory server instance, you are ready to download files from an Artifactory server. First, you need to create a spec,
which is a json string.
The spec defines the files that you'd like to download and the target path for the files.
Here's an example:
The spec defines the files that you want to download and the target path for the files.
See the example below:
```
def downloadSpec = '{
"files": [
Expand All @@ -46,14 +46,14 @@ def downloadSpec = '{
The above spec defines the following:
Downloads all zip files in the *bazinga-repo* Artifactory repository into the
*bazinga* directory on your Jenkins agent file-system.
Note that *files* is a list, so your spec can include a list of patterns and targets.
The next step is to use the spec to download the files. You do that by adding the following line:
Notice that *files* is a list, therefore, your spec can include a list of patterns and targets.
The following step is to use the spec to download the files. To download the files, add the following line:
```
server.download(downloadSpec)
```
That's it. Easy, isn't it?
Piece of cake, right?

The concept of uploading files is similar to downloading. First create a spec and then use it upload files to the Arttifactory server:
The process of uploading files is similar to downloading files. First, create a spec and then use this spec to upload files to the Artifactory server:
```
def uploadSpec = '{
"files": [
Expand All @@ -65,18 +65,18 @@ def uploadSpec = '{
}'
server.upload(uploadSpec)
```
The above code uploaads all zip files that include *froggy* in their names into the *froggy-files* foldder in the *bazinga-repo* Artifactory repository.
The code shown above uploads all zip files that include *froggy* in their names into the *froggy-files* foldder in the *bazinga-repo* Artifactory repository.

### Publishing Build-Info to Artifactory
Both the *download* and *upload* methods return a build-info object, that you can then
publish to Artifactory. Here's how you do it:
Both the *download* and *upload* methods return a build-info object, which can be published
to Artifactory. Below is an example code sample:
```
def buildInfo1 = server.download(downloadSpec)
def buildInfo2 = server.upload(uploadSpec)
buildInfo1.append(buildInfo2)
server.publishBuildInfo(buildInfo1)
```
You can also do the following:
Below is a second example:
```
def buildInfo = Artifactory.newBuildInfo()
server.download(artifactoryDownloadDsl, buildInfo)
Expand All @@ -86,11 +86,11 @@ server.publishBuildInfo(buildInfo1)

## Advanced Upload Options
### Using Placeholders When Uploading
The upload spec enormous flexibility in how you define the target path, through the use of wildcard or regular expressions with placeholders.
The upload spec allows for flexibility in how you define the target path by using wildcard or regular expressions with placeholders.
Any wildcard enclosed in parenthesis in the source path can be matched with a corresponding placeholder in the target path to determine the name of the artifact once uploaded.
In the following example, for each .tgz file in the source directory, a corresponding directory with the same name
is created in the target repository and the file is uploaded into it.
For example, a file called froggy.tgz should be uploaded to my-local-rep/froggy/froggy.tgz.
is created in the target repository and the file is created in the corresponding directory.
For example, a file called froggy.tgz is uploaded to my-local-rep/froggy/froggy.tgz.
```
def uploadSpec = '{
"files": [
Expand All @@ -103,9 +103,8 @@ def uploadSpec = '{
}'
```
### Using Regular Expressions
You can use a regular expression in your upload spec to define which artifacts should be uploaded.
You need to add *"regexp": "true"* to the spec.
Here's an example:
You have the option of using a regular expression rather than wildcard patterns to define the upload pattern. Add *"regexp": "true"* to the spec.
See the example below:
```
def uploadSpec = '{
"files": [
Expand All @@ -120,7 +119,7 @@ def uploadSpec = '{
```

## The Download Spec Schema
You can also use [AQL](https://www.jfrog.com/confluence/display/RTF/Artifactory+Query+Language) instead of the wildcards pattern:
You can also use [AQL](https://www.jfrog.com/confluence/display/RTF/Artifactory+Query+Language) instead of wildcard patterns:
```
{
"files": [
Expand Down Expand Up @@ -150,7 +149,7 @@ You can also use [AQL](https://www.jfrog.com/confluence/display/RTF/Artifactory+
}
```
## Examples
We included a few examples to help you get started using the Artifactory DSL in your Pipeline scripts.
The example below are meant to help you get started using the Artifactory DSL in your Pipeline scripts.

* The [props-example](https://github.com/jfrogdev/project-examples/tree/master/jenkins-pipeline-examples/props-example]) downloads and uploads files to Artifactory with properties. It also uses a placeholder when uploading.
* The [props-example](https://github.com/jfrogdev/project-examples/tree/master/jenkins-pipeline-examples/props-example]) download and upload files to Artifactory with properties. The props-example also uses a placeholder when uploading.
* The [aql-example](https://github.com/jfrogdev/project-examples/tree/master/jenkins-pipeline-examples/aql-example]) uses [AQL](https://www.jfrog.com/confluence/display/RTF/Artifactory+Query+Language) in its download spec.

0 comments on commit fa5201a

Please sign in to comment.