项目作者: Invoca

项目描述 :
Jenkins Pipeline Shared Library
高级语言: Groovy
项目地址: git://github.com/Invoca/jenkins-pipeline.git
创建时间: 2017-11-08T19:15:53Z
项目社区:https://github.com/Invoca/jenkins-pipeline

开源协议:MIT License

下载


Jenkins Pipeline

Jenkins Pipeline Shared Library. Contains helper functions to be used with the Jenkins Pipeline Plugin.

Image Class

The Docker Image class expects 3 or 4 arguments in its constructor call

# Type Required Description
1 Script Y A reference to the Script object, always this when instantiated from the Jenkinsfile.
2 String Y The name of the image, including the Docker Hub organization. i.e. invocaops/ruby.
3 String[] Y An array of tags to apply to the image.
4 String N The directory that the Dockerfile is in. Useful when multiple versions of the image need to be built. Defaults to the directory the Jenkinsfile is in.

Example

Example for Ruby 2.4.2, which is in a directory named 2.4.2 and being built from the master branch with SHA 12345:

  1. image = new Image(this, "invocaops/ruby", ["2.4.2-12345", "2.4.2-master"], "2.4.2")

Usage

The Image class has 4 main methods to perform operations

Method Arguments Action
build() buildArgs (Map) Buils the Docker image.
tag() None Tags the image.
push() None Pushes the image and its tags to Docker Hub.

Each method returns a reference to the Image object, so chaining is possible.

Build Args

The Image#build method takes a Map of build arguments.

Argument Type Required Description
gitUrl String Y URL to remote Git repository. Set to env.GIT_URL.
buildArgs Map N foo=bar pairings for docker build --build-arg.
dockerFile String N Name of Dockerfile file, defaults to Dockerfile.
target String N Target stage to build to in the docker build.

Environment

In addition to the included Git environment variables, we currently assume access to credentials for DockerHub. You’ll need to explicitly set these in your environment.

Variable Available By Default Description
DOCKERHUB_USER N Username for DockerHub.
DOCKERHUB_PASSWORD N Password for DockerHub.
GIT_COMMIT Y SHA of current build.
GIT_URL Y URL of GitHub repository being built.
GIT_BRANCH Y The name of the checked out branch.

Getting Started

To use this library, start your Jenkinsfile with:

  1. @Library('github.com/invoca/jenkins-pipeline@v0.1.0')

After, parts of the library can be imported and used. Below is an example of a Jenkinsfile that builds multiple versions of the Ruby image.

  1. @Library('github.com/invoca/jenkins-pipeline@v0.1.0')
  2. import com.invoca.docker.*;
  3. pipeline {
  4. agent any
  5. stages {
  6. stage('Setup') {
  7. steps {
  8. script {
  9. def imageName = "invocaops/ruby"
  10. def directories = sh(script: 'ls **/Dockerfile | while read dir; do echo $(dirname $dir); done', returnStdout: true).split("\n")
  11. def sha = env.GIT_COMMIT
  12. def branchName = env.GIT_BRANCH
  13. images = directories.collect {
  14. String[] tags = ["${it}-${branchName}", "${it}-${sha}"]
  15. new Image(this, imageName, tags, it)
  16. }
  17. }
  18. }
  19. }
  20. stage('Build') {
  21. steps {
  22. script {
  23. for (Image image : images) {
  24. image.build(gitUrl: env.GIT_URL).tag()
  25. }
  26. }
  27. }
  28. }
  29. stage('Push') {
  30. environment {
  31. DOCKERHUB_USER = credentials('dockerhub_user')
  32. DOCKERHUB_PASSWORD = credentials('dockerhub_password')
  33. }
  34. steps {
  35. script {
  36. new Docker().hubLogin(env.DOCKERHUB_USER, env.DOCKERHUB_PASSWORD)
  37. for (Image image : images) {
  38. image.push()
  39. }
  40. }
  41. }
  42. }
  43. }
  44. post {
  45. always {
  46. notifySlack(currentBuild.result)
  47. }
  48. }
  49. }

Please read more about libraries in the Jenkins documentation.