Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added source code linting to Groovy code and Jenkinsfile #325

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .groovylintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "recommended",
"extends": "recommended-jenkinsfile",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between recommended and recommended-jenkinsfile?

Copy link
Author

@chawinphat chawinphat Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the linter's recommended format which lints jenkinsfiles too. When using just 'recommended' the linter catches errors that are not actually errors and breaks the program.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide some examples what failures are you encountering? I am doubtful what to use as well. On high level looks like recommended is best path for groovy. recommended-jenkinsfile seems to be applicable for jenkinsFiles specifically? Please correct me if I am wrong.

Adding @prudhvigodithi @peterzhuamazon @dblock to get some input.

Copy link
Author

@chawinphat chawinphat Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One error I saw was that the linter complained that 'variable x was not used in the file' even when x was referenced inside a string in that file.

Running the linter's automatic fixing caused tests to fail if I used the 'jenkins' setting while it was able to build and test fine with 'jenkins-file'

"rules": {
"CompileStatic": {
"enabled": false
Expand Down
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
default_stages: [commit]
repos:
- repo: local
hooks:
- id: npm-groovy-lint
stages: [commit]
name: npm-groovy-lint
entry: bash -c 'npm-groovy-lint tests/jenkins src/jenkins vars --failon error'
gaiksaya marked this conversation as resolved.
Show resolved Hide resolved
language: system
types: [groovy]
description: Groovy & Jenkinsfile Linter
23 changes: 23 additions & 0 deletions DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ This project contains [Jenkins pipelines](jenkins) and [Jenkins shared libraries

Use Java 11 for Jenkins jobs CI. This means you must have a JDK 11 installed with the environment variable `JAVA_HOME` referencing the path to Java home for your JDK installation, e.g. `JAVA_HOME=/usr/lib/jvm/jdk-11`. Download Java 11 from [here](https://adoptium.net/releases.html?variant=openjdk11).

#### NVM and Node
Use node to run Groovy and Jenskinsfiles Lint. Install [nvm](https://github.com/nvm-sh/nvm/blob/master/README.md) to use the Node version defined in [OpenSearch-Dashboards](https://github.com/opensearch-project/OpenSearch-Dashboards) repository `.nvmrc` file as it is required.

Use the official [nvm guide](https://github.com/nvm-sh/nvm#installing-and-updating) to install nvm and corresponding Node version.

### Run Tests

This project uses [JenkinsPipelineUnit](https://github.com/jenkinsci/JenkinsPipelineUnit) to unit test Jenkins pipelines and shared libraries. See [tests/jenkins](tests/jenkins).
Expand Down Expand Up @@ -62,3 +67,21 @@ Each jenkins library should have a test case associated with it. Eg: [TestSignAr
- Jenkins' library test should extend [BuildPipelineTest.groovy](tests/jenkins/BuildPipelineTest.groovy)
- Create a dummy job such as [Hello_Jenkinsfile](tests/jenkins/jobs/Hello_Jenkinsfile) to call and test the function
and output [Hello_Jenkinsfile.txt](tests/jenkins/jobs/Hello_Jenkinsfile.txt)

### Code Linting

This project uses a [pre-commit hook](https://pre-commit.com/) for linting Groovy and Jenkinsfiles code.

Install this pre-commit hook using pip.

```
pip install pre-commit
pre-commit install
```

To run the linter outside of the pre-commit hook
:
```
npm-groovy-lint <files or directories>
```

Loading