# Utils library

## Status to Bitbucket
*Available since 1.0*

Keep track of a step status in the CVS

### Signature

`utils.build_notify(currentBuild, String key, String title, Closure process, Closure error_eval = null)`

*Parameter*

- `currentBuild` is the meta object pointing to the current build
- `key` is used to uniquely identify a task within a build, It should not contain blankspace and non alphanumerical chars.
- `title` is the friendly title to be displayed to the user
- `process` is the wrapped steps to be taken for this step to complete. it is possible to **return a string** to override the step annotation, i.e: `Completed in 12 seconds`
- `error_eval` is the wrapped steps to be taken to evaluate an error annotation and **return it as a string** , i.e: `The Jira ticket was not approved` (Optional)

### Examples

Simple usage:

```groovy
utils.build_notify(currentBuild, 'my-tak-key', "My Task"){
    sh "echo proceed"
}
```

Step annotation usage:

```groovy
utils.build_notify(currentBuild, 'my-tak-key', "My Task"){
    if (my_var == "yes"){
        sh "echo proceed"
        return "Process was approved"
    } else {
        throw Exception("This wasn't approved")
    }
} {
    return "${my_other_var} wasn't approved to proceed."
}
```

## Artifact generation
*Available since 1.1*

Create a new artifact that can be reused by a CD pipeline

### Signature

`artifacts.create_artifact(String path)`

*Parameter*

- `path` is the path to the **folder** from which the artifact needs to be created

### Examples

Simple usage using `utils.build_notify`:

```groovy
utils.build_notify(currentBuild, 'my-packaging-step', "Deliver artifact"){
    sh 'mkdir -p build && echo generated > build/output.txt'
    artifacts.create_artifact('build')
}
```

## Get current M2A version
*Available in 1.0 with library `artifacts`*

Get the current version for which the active pipeline is running

### Signature

`artifacts.get_version(currentBuild)`

*Parameter*

- `currentBuild` is the meta object pointing to the current build
- **Return value** is a string containing the version, i.e: `20.11~beta~352`

### Examples

Simple usage using `utils.build_notify` and `artifacts.create_artifact`:

```groovy
utils.build_notify(currentBuild, 'my-packaging-step', "Deliver artifact"){
    sh "mkdir -p build && echo 'Current build is ${artifacts.get_version(currentBuild)}' > build/output.txt"
    artifacts.create_artifact('build')
}
```

## Get current semantic version
*Available in 1.0 with library `artifacts`*

Get the current version for which the active pipeline is running

### Signature

`get_semantic_version()`

*Parameter*

- `env` is the global env object that can be accessed during the build
- `currentBuild` is the meta object pointing to the current build
- **Return value** is a string containing the semantic version, i.e: `1.2.3`

### Examples

Simple usage using `utils.build_notify`:

```groovy
utils.build_notify(currentBuild, 'my-packaging-step', "Deliver artifact"){
    sh "mkdir -p build && echo 'Current build is ${get_semantic_version(env, currentBuild)}' > build/output.txt"
    artifacts.create_artifact('build')
}
```

## Check Terraform with Checkov
*Available in 1.5 with library `utils`*
* Note: You have to instann checkov inside your pipeline Dockerfile. Ex: `RUN pip3 install checkov`

### Signature

`utils.check_terraform(Map params = [:])`

Creates an analysis report from Dockerfile or other file and attach the report to the pipeline as an Archive

##### Only accept the below parameters as Map

*Parameter*

- `dir` String - Any directory name. (Default . (Current Directory) if not provide any files)
- `files` List - Any file name. Ex: Dockerfile/Something.txt . Only run if not provide any d (directory)
- `stageResult` String - FAILURE/UNSTABLE/SUCCESS. Change the stage result if checkov gets any error. Default FAILURE
- `report_file_name` String -  change the default report file name. Default $BUILD_NUMBER-report.txt
- `skip_check` List - list of checkov check list. Ex: ["CKV_DOCKER_2", "CKV_DOCKER_5"]. It will skip those check while checking
- `ignore_list` List - list of ignore directory/file list. Ex: ["dir1", "code", "file1"]. It will ignore those directories/files while checking
- `quiet` Boolean - If true then it will only return failed report. Default false
- `compact` Boolean - If true then it remove code from the report. Default false
- **Outcome** Function will attach the report file to the pipeline by archiveArtifacts

### Examples

Simple usage using:

```groovy
utils.check_terraform()
```
```
Note: This will check everything (tf files, Dockerfile, docker-compose, k8s, etc.) from the current location and 
retates a report file named <CURRENT_BUILD>-report.txt
```

Check only Terraform folder:

```groovy
utils.check_terraform('dir' : 'Terraform', "report_file_name": "my-custom-report-file.txt")
```
```
Note: This will check everything (tf files, Dockerfile, docker-compose, k8s, etc.) from the Terraform folder and 
retates a report file named my-custom-report-file.txt
```

Check some tf/Others files:

```groovy
utils.check_terraform('files' : ['file1.tf', 'main.tc', 'dockerfile', 'V1/main.tf')
```
```
Note: This will check only those files and 
retates a report file named <CURRENT_BUILD>-report.txt
```

Check with ignore_list:

```groovy
utils.check_terraform('ignore_list' : ["ignore1", "file1", "ignore_dir"])
utils.check_terraform('d' : "TF_files", 'ignore_list' : ["ignore1", "file1", "ignore_dir"])
```
```
Note: This will check everyting from current location/TF_files location except the ignore_list files/folder and 
retates a report file named <CURRENT_BUILD>-report.txt
```



## Check Dockerfile with Checkov
*Available in 1.5 with library `utils`*
* Note: You have to instann checkov inside your pipeline Dockerfile. Ex: `RUN pip3 install checkov`


### Signature

`utils.check_dockerfile(Map params = [:])`

Creates an analysis report from Dockerfile or other file and attach the report to the pipeline as an Archive

##### Only accept the below parameters as Map

*Parameter*

- `file` String - Any file name. Ex: Dockerfile/Something.txt . Default Dockerfile
- `stageResult` String - FAILURE/UNSTABLE/SUCCESS. Change the stage result if checkov gets any error. Default FAILURE
- `report_file_name` String -  change the default report file name. Default $BUILD_NUMBER-report.txt
- `skip_check` List - list of checkov check list. Ex: ["CKV_DOCKER_2", "CKV_DOCKER_5"]. It will skip those check while checking
- `quiet` Boolean - If true then it will only return failed report. Default false
- `compact` Boolean - If true then it remove code from the report. Default false
- **Outcome** Function will attach the report file to the pipeline by archiveArtifacts

### Examples

Simple usage using:

```groovy
utils.check_dockerfile()
```
```
Note: This will check Dockerfile from the current location and 
retates a report file named <CURRENT_BUILD>-Dockerfile-report.txt
```

Check another file:

```groovy
utils.check_dockerfile('file' : 'a-file-name.txt', "report_file_name": "my-custom-report-file.txt")
```
```
Note: This will check a-file-name.txt from the current location and 
retates a report file named my-custom-report-file.txt
```

Check another file:

```groovy
utils.check_dockerfile('file' : 'a-file-name.txt', "report_file_name": "my-custom-report-file.txt")
utils.check_dockerfile('file' : 'build/dockerfile', "skip_check": ["CKV_DOCKER_2", "CKV_DOCKER_5"])
```
```
Note: This will check build/dockerfile and 
retates a report file named <CURRENT_BUILD>-build/dockerfile-report.txt
```


## Add Git Tag for Deployed Environment
*Available in 1.5 with library `utils`*


### Signature

`utils.add_deploy_tag(String env_name)`

Tags the latest commit for git repo at CWD with deployed/$env_name

##### Only accept the below parameters as String

*Parameter*

- `env_name` String - The name of the environment that has been deployed

### Examples

Simple usage using:

```groovy
utils.add_deploy_tag("prod")
```
```
Note: This will override the previous deployed/prod tag and apply it
to the latest commit
```

Tag for a different environment:

```groovy
utils.add_deploy_tag("stable")
```
```
Note: This will override the previous deployed/stable tag and apply it
to the latest commit
```
