From d98093d83b6b595dce9df9a454947b01841fbc1b Mon Sep 17 00:00:00 2001
From: Christophe Mathias <christophe226@gmail.com>
Date: Wed, 15 Sep 2021 10:10:23 +0000
Subject: [PATCH] feat: add JSON output format for Hadolint report

Signed-off-by: Christophe Mathias <christophe.mathias@orange.com>
---
 templates/gitlab-ci-docker.yml | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/templates/gitlab-ci-docker.yml b/templates/gitlab-ci-docker.yml
index fd1a44b..7fdc911 100644
--- a/templates/gitlab-ci-docker.yml
+++ b/templates/gitlab-ci-docker.yml
@@ -451,18 +451,20 @@ docker-hadolint:
     - autoconfig_hadolint
     - mkdir -p reports
     - chmod o+rwx reports
-    - hadolint_report_name="hadolint-$(md5sum "$DOCKER_FILE" | cut -d" " -f1).json"
-    # Force one run with tty format in order to have a nice output without failure
-    - hadolint --no-fail $DOCKER_HADOLINT_ARGS $hadolint_config_opts "$DOCKER_FILE"
-    # Make one run with ad hoc output for gitlab integration
-    - hadolint -f gitlab_codeclimate $DOCKER_HADOLINT_ARGS $hadolint_config_opts "$DOCKER_FILE" > "reports/$hadolint_report_name"
+    - dockerfile_hash=$(md5sum "$DOCKER_FILE" | cut -d" " -f1)
+    # Output in Code Climate format (GitLab integration)
+    - hadolint --no-fail -f gitlab_codeclimate $DOCKER_HADOLINT_ARGS $hadolint_config_opts "$DOCKER_FILE" > "reports/hadolint-cc-${dockerfile_hash}.json"
+    # Output in JSON format
+    - hadolint --no-fail -f json $DOCKER_HADOLINT_ARGS $hadolint_config_opts "$DOCKER_FILE" > "reports/hadolint-json-${dockerfile_hash}.json"
+    # las run with console output (with failure)
+    - hadolint $DOCKER_HADOLINT_ARGS $hadolint_config_opts "$DOCKER_FILE"
   artifacts:
     name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
     expire_in: 1 day
     when: always
     reports:
       codequality:
-        - "reports/hadolint-*.json"
+        - "reports/hadolint-cc-*.json"
     paths:
       - "reports/hadolint-*.json"
   rules:
-- 
GitLab