diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c22595..0286193 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.67.0](https://github.com/cloudogu/ces-build-lib/releases/tag/1.67.0) - 2023-09-04 +### Changed +- Switch to hadolint Dockerfile linter; #111 + +### Added +- Add Dockerfile class with new linting functions; #111 + - `lint()` lints with default hadolint config parameters. Only fails on errors + - `lintWithConfig()` lets you specify the hadolint configuration + +### Deprecated +- `lintDockerfile()` function should be replaced by the Dockerfile `lint()` function -## [1.66.0](https://github.com/cloudogu/ces-build-lib/releases/tag/1.66.1) - 2023-09-04 +## [1.66.1](https://github.com/cloudogu/ces-build-lib/releases/tag/1.66.1) - 2023-09-04 ### Fixed - Split helm-repo-config in separate values #113 diff --git a/README.md b/README.md index 10ee370..f8d2dc1 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Jenkins Pipeline Shared library, that contains additional features for Git, Mave - [Additional features provided by the `Docker` class](#additional-features-provided-by-the-docker-class) - [`Docker.Image` methods provided by the docker plugin](#dockerimage-methods-provided-by-the-docker-plugin) - [Additional features provided by the `Docker.Image` class](#additional-features-provided-by-the-dockerimage-class) +- [Dockerfile](#dockerfile) - [SonarQube](#sonarqube) - [Constructors](#constructors) - [A complete example](#a-complete-example) @@ -79,10 +80,10 @@ Jenkins Pipeline Shared library, that contains additional features for Git, Mave * Install [Pipeline: GitHub Groovy Libraries](https://wiki.jenkins.io/display/JENKINS/Pipeline+GitHub+Library+Plugin) * Use the Library in any Jenkinsfile like so ``` -@Library('github.com/cloudogu/ces-build-lib@6cd41e0') +@Library('github.com/cloudogu/ces-build-lib@1.67.0') import com.cloudogu.ces.cesbuildlib.* ``` -* Best practice: Use a defined version (e.g. a git commit hash or a git tag, such as `6cd41e0` or `1.49.0` in the example above) and not a branch such as `develop`. Otherwise, your build might change when the there is a new commit on the branch. Using branches is like using snapshots! +* Best practice: Use a defined version (e.g. a git commit hash or a git tag, such as `6cd41e0` or `1.67.0` in the example above) and not a branch such as `develop`. Otherwise, your build might change when the there is a new commit on the branch. Using branches is like using snapshots! * When build executors are docker containers and you intend to use their Docker host in the Pipeline: Please see [#8](https://github.com/cloudogu/ces-build-lib/issues/8#issuecomment-353584252). # Syntax completion @@ -712,6 +713,22 @@ new Docker(this).image('kkarczmarczyk/node-yarn:8.0-wheezy') } ``` +# Dockerfile + +The `Dockerfile` class provides functions to lint Dockerfiles. For example: + +```groovy +stage('Lint') { + Dockerfile dockerfile = new Dockerfile(this) + dockerfile.lint() // Lint with default configuration + dockerfile.lintWithConfig() // Use your own hadolint configuration with a .hadolint.yaml configuration file +} +``` + +The tool [hadolint](https://github.com/hadolint/hadolint) is used for linting. It has a lot of configuration parameters +which can be set by creating a `.hadolint.yaml` file in your working directory. +See https://github.com/hadolint/hadolint#configure + # SonarQube When analyzing code with SonarQube there are a couple of challenges that are solved using ces-build-lib's @@ -1159,7 +1176,10 @@ Additionally, the markdown link checker can be used with a specific version (def markdown.check() ``` -### DockerLint +### DockerLint (Deprecated) + +Use Dockerfile.lint() instead of lintDockerfile()! +See [Dockerfile](#dockerfile) ```groovy lintDockerfile() // uses Dockerfile as default; optional parameter diff --git a/pom.xml b/pom.xml index ae2e935..be79365 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ com.cloudogu.ces ces-build-lib ces-build-lib - 1.66.1 + 1.67.0 diff --git a/src/com/cloudogu/ces/cesbuildlib/Dockerfile.groovy b/src/com/cloudogu/ces/cesbuildlib/Dockerfile.groovy new file mode 100644 index 0000000..bf820c0 --- /dev/null +++ b/src/com/cloudogu/ces/cesbuildlib/Dockerfile.groovy @@ -0,0 +1,39 @@ +package com.cloudogu.ces.cesbuildlib + +class Dockerfile { + private script + + Dockerfile(script) { + this.script = script + } + + /** + * Lints the Dockerfile with hadolint using a configuration file + * + * To configure hadelint, add a ".hadolint.yaml" file to your working directory + * See https://github.com/hadolint/hadolint#configure + * + * @param dockerfile Path to the Dockerfile that should be linted + * @param configuration Path to the hadolint configuration file + * @param hadolintVersion Version of the hadolint/hadolint container image + */ + void lintWithConfig(String dockerfile = "Dockerfile", String configuration = ".hadolint.yaml", hadolintVersion = "latest-debian"){ + script.docker.image("hadolint/hadolint:${hadolintVersion}").inside(){ + script.sh "hadolint --no-color -c ${configuration} ${dockerfile}" + } + } + + /** + * Lints the Dockerfile with the latest version of hadolint + * Only fails on errors, ignores warnings etc. + * Trusts registries docker.io, gcr.io and registry.cloudogu.com + * + * @param dockerfile Path to the Dockerfile that should be linted + * @param hadolintVersion Version of the hadolint/hadolint container image + */ + void lint(String dockerfile = "Dockerfile", hadolintVersion = "latest-debian"){ + script.docker.image("hadolint/hadolint:${hadolintVersion}").inside(){ + script.sh "hadolint -t error --no-color --trusted-registry docker.io --trusted-registry gcr.io --trusted-registry registry.cloudogu.com ${dockerfile}" + } + } +} diff --git a/vars/lintDockerfile.groovy b/vars/lintDockerfile.groovy index 23b9bc5..9f52143 100644 --- a/vars/lintDockerfile.groovy +++ b/vars/lintDockerfile.groovy @@ -1,8 +1,10 @@ package com.cloudogu.ces.cesbuildlib +@Deprecated def call(String dockerfile = "Dockerfile") { - // only latest version available - docker.image('projectatomic/dockerfile-lint:latest').inside({ - sh "dockerfile_lint -p -f ${dockerfile}" - }) + docker.image('hadolint/hadolint:latest-debian').inside(){ + sh "hadolint --no-color -t error " + + "--trusted-registry docker.io --trusted-registry gcr.io --trusted-registry registry.cloudogu.com " + + "${WORKSPACE}/${dockerfile}" + } }