{"id":18548444,"url":"https://github.com/danielfbm/docker-build-library","last_synced_at":"2025-05-15T08:13:04.413Z","repository":{"id":96090518,"uuid":"175922105","full_name":"danielfbm/docker-build-library","owner":"danielfbm","description":"Jenkins shared library docker build implementation","archived":false,"fork":false,"pushed_at":"2019-03-16T05:16:19.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-26T07:09:27.476Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Groovy","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielfbm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-16T03:35:37.000Z","updated_at":"2019-03-16T05:16:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"cf2ff2cb-c1fa-4b19-a0b0-dead0d960870","html_url":"https://github.com/danielfbm/docker-build-library","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfbm%2Fdocker-build-library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfbm%2Fdocker-build-library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfbm%2Fdocker-build-library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielfbm%2Fdocker-build-library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielfbm","download_url":"https://codeload.github.com/danielfbm/docker-build-library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239269808,"owners_count":19610865,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T20:34:37.499Z","updated_at":"2025-02-17T09:43:25.182Z","avatar_url":"https://github.com/danielfbm.png","language":"Groovy","funding_links":[],"categories":[],"sub_categories":[],"readme":"# docker-build-library\nJenkins shared library docker build implementation\n\n## Setup and requirements\n\n### 1. Setup as shared library\n\nSetup the jenkins shared library using Jenkins configuration interfaces. [Reference](https://jenkins.io/doc/book/pipeline/shared-libraries/)\n\n### 2. Agent with docker client CLI\n\nThe specified agent should have a `docker client` as a `docker` command. If running inside a container, it should attach the `docker.sock ` so that the host's `dockerd` is reachable.\n\n### 3. sh DSL\n\nUnder the hood, the `Build` uses Jenkins `sh` DSL to execute all the build commands. For this reason, this library is only compatible with Linux/Unix systems, but it should be easily fixed to support other operating systems.\n\n## TL; DR\n\nUsing the [Kubernetes plugin](\u003chttps://github.com/jenkinsci/kubernetes-plugin\u003e) a `docker` label agent can be created with the docker client library\n\n```\n@Library('dockerbuild') _\n\nnode('docker') {\n    git url: 'https://github.com/alauda/tasks-gateway'\n    container('docker') {\n        def image = dockerBuild.setup(\n            \"index.alauda.cn/alauda/tasks-gateway\",\n            \"latest\",\n            \"Dockerfile\",\n            \".\",\n            \"alauda-credentials\",\n            1,\n        )\n        image.build().push()\n        def imageUrl = image.getImage()\n        echo \"${imageUrl}\"\n    }\n}\n\n```\n\n\n\n\n## DSL\n\n### dockerBuild.setup(String *address* = \"index.alauda.cn\", String *tag* = \"latest\", String *dockerfilePath* = \"Dockerfile\", String *context* = \".\", String *credentialsId* = \"\", int *retryTimes* = 2)\n\n`dockerBuild.setup` method is the entrypoint to initiate a `Build` instance.\n\nThe `setup` method has the following parameters:\n\n| parameter      | description                                                  | Type   | default value     |\n| -------------- | ------------------------------------------------------------ | ------ | ----------------- |\n| address        | Image repository address used to push the image repository. Should be the final repository address: i.e `index.alauda.cn/alauda/hello-world` | String | `index.alauda.cn` |\n| tag            | Tag given to the docker image during build. Other tags can be used in specific `push` commands in from `Build` instance. | String | `latest`          |\n| dockerFilePath | Dockerfile path and filename                                 | String | `Dockerfile`      |\n| context        | Docker build context (directory) used for dockerbuild. The Dockerfile must be inside the `context` folder | String | `.`               |\n| credentialsId  | If docker login is required for pushing/pull the images, a credential should be added to Jenkins with the registry's username and password, and its ID should be given as a String | String | ``                |\n| retryTimes     | Number of times the commands will retry during the process if fails | Int    | `2`               |\n\nExample with only basic parameters:\n\n```groovy\nnode('docker') {\n    def imageBuild = dockerBuild.setup(\n      \"index.alauda.cn/alauda/hello-world\", // docker repository address\n      \"latest\", // tag \n    )\n    //  will build the docker image, push as \"latest\" tag, give a \"v1.0\" and push again\n    imageBuild.build().push().push(\"v1.0\")\n  \n    def imageAddress = imageBuild.getImage()\n\t  echo \"${imageAddress}\" // will print \"index.alauda.cn/alauda/hello-world:latest\" \n}\n```\n\n\n\nExample with all parameters:\n\n```groovy\nnode('docker') {\n    def imageBuild = dockerBuild.setup(\n      \"index.alauda.cn/alauda/hello-world\", // docker repository address\n      \"latest\", // tag\n      \"Dockerfile\", // Dockerfile path to file\n      \".\", // Docker build context\n      \"docker-credentials\", // user/password credentials stored in Jenkins\n      2, // number of retries if a specific command fails\n    )\n    //  will build the docker image, push as \"latest\" tag, give a \"v1.0\" and push again\n    imageBuild.setArg(\"commit_id\", env.COMMIT_ID).build().push().push(\"v1.0\")\n  \n    def imageAddress = imageBuild.getImage(\"v1.0\")\n\t  echo \"${imageAddress}\" // will print \"index.alauda.cn/alauda/hello-world:v1.0\" \n}\n```\n\n\n\n## Build object\n\n`dockerBuild.setup` only initiates the `Build` object. Most methods will `return this`  which makes it convenient to chain calls of different methods. The full list of methods is described bellow:\n\n### setArg(String *name*, String *value*)\n\nThis command prepares a key/value argument in memory to be used once the `build` method executes. The list of parameters follows:\n\n| parameter | description                                | Type   |\n| --------- | ------------------------------------------ | ------ |\n| name      | argument name as defined in the Dockerfile | String |\n| value     | value given to the argument                | String |\n\nReturns: `Build` object\n\n### setFullAddress(String *address*)\n\nOverwrites the image repository address given in the `setup` method.\n\nReturns: `Build` object\n\n\n\n### setRetries(int *retryTimes* = 2)\n\nOverwrites the retry number of commands. All docker commands are wrapped in a `retry(n)` , and this method can be used to set `n` values.\n\nReturns: `Build` object\n\n\n\n### getRegistryAddress()\n\nReturns the registry address of the repository. i.e if the docker repository address is `index.alauda.cn/alauda/hello-world` it returns `index.alauda.cn`\n\nReturns: `String`\n\n\n\n### build()\n\nStarts the build process. Will execute `login` method if a credential was given and if yet not logged in.\n\nReturns: `Build` object\n\n\n\n### push(String *tag* = \"\")\n\nPushes the image. If a tag value is given will also automatically tag the built image and push to the docker registry.\n\nReturns: `Build` object\n\n\n\n### pull(String *tag*=\"\")\n\nPulls the docker repository used. Will also login if a credential ID is provided and not-yet logged int. If a tag value is given will automatically pull the specified tag using the docker repository address.\n\nReturns: `Build` object\n\n\n\n### login()\n\nExecutes docker login if a credential ID is given and not-yet logged in. Although this method is accessible, it is not necessary to explicitly call it as all commands that require login will execute this method implicitly.\n\nReturns: `Build` object\n\n\n\n### getImage(String *tag* = \"\")\n\nReturns the full address of the docker repository. By default returns the tag given during `setup`, but if a tag value is given will use it instead.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielfbm%2Fdocker-build-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielfbm%2Fdocker-build-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielfbm%2Fdocker-build-library/lists"}