gitlab.yml

Yaml code posted
created at 12 Dec 14:58

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
stages:
  - build
  - test
  - quality
  - security_static
  - security_dynamic
  - deploy
  - container_scan

variables:
  MAVEN_CLI_OPTS: "-B -DskipTests"
  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"
  # SonarQube variables should be set in project settings
  SONAR_HOST_URL: "${SONAR_HOST_URL}"
  SONAR_TOKEN: "${SONAR_TOKEN}"

cache:
  paths:
    - .m2/repository
    - target

before_script:
  - apt-get update -qq && apt-get install -y curl unzip jq
  - export PATH=$PATH:$CI_PROJECT_DIR

# ---------------------
# Build & Test Stage
# ---------------------

build:
  stage: build
  image: maven:3.8.7-eclipse-temurin-17
  script:
    - mvn $MAVEN_CLI_OPTS clean package
  artifacts:
    paths:
      - target/*.jar
    expire_in: 1 week

test:
  stage: test
  image: maven:3.8.7-eclipse-temurin-17
  script:
    - mvn test
  artifacts:
    reports:
      junit: target/surefire-reports/*.xml
    expire_in: 1 week

# ---------------------
# Code Quality with SonarQube
# ---------------------

sonarqube:
  stage: quality
  image: maven:3.8.7-eclipse-temurin-17
  script:
    - mvn sonar:sonar -Dsonar.login=$SONAR_TOKEN -Dsonar.host.url=$SONAR_HOST_URL
  allow_failure: false
  dependencies:
    - build
    - test

# ---------------------
# Security Static: SAST & Dependency Scanning & IaC Scanning
# ---------------------

# SAST
include:
  - template: SAST.gitlab-ci.yml

# Dependency Scanning (SCA)
include:
  - template: Dependency-Scanning.gitlab-ci.yml

# IaC Scanning
include:
  - template: IaC-Scanning.gitlab-ci.yml

# By including these templates, the following jobs will appear automatically:
# * sast
# * dependency_scanning
# * iac_scanning
#
# They run in `security_static` stage by default if you override them:
#
# If needed, explicitly set them to run in a desired stage and with needed dependencies:
sast:
  stage: security_static
  needs: ["build"]
  # optional customization here

dependency_scanning:
  stage: security_static
  needs: ["build"]
  # optional customization

iac_scanning:
  stage: security_static
  # Add any IaC files scanning dependencies if needed
  # optional customization

# ---------------------
# DAST (Dynamic Application Security Testing)
# ---------------------

# We assume that a review environment is deployed in the 'deploy' stage before DAST
# DAST scanning requires the app to be accessible at a known URL.
#
# Include the DAST template:
include:
  - template: DAST.gitlab-ci.yml

dast:
  stage: security_dynamic
  variables:
    DAST_WEBSITE: "http://myapp-review.$CI_PROJECT_NAMESPACE.$CI_PROJECT_NAME.example.com"
  # Wait for deploy job to complete (if the environment isn't ready yet)
  needs: ["deploy_app"]
  # optional additional configuration

# ---------------------
# Deploy to Review Environment
# ---------------------
# This assumes a Dockerfile is present at the root of the repository.
# Adjust image name, registry, and deployment steps as appropriate.

deploy_app:
  stage: deploy
  image: docker:24.0.6
  services:
    - docker:24.0.6-dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
    IMAGE_NAME: "$CI_REGISTRY_IMAGE/app"
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $IMAGE_NAME:$CI_COMMIT_SHORT_SHA .
    - docker push $IMAGE_NAME:$CI_COMMIT_SHORT_SHA
    # Here you would run your deployment commands to a staging/review environment,
    # e.g. using Helm/Kubectl or Docker Compose to start up the app.
    # For example:
    # - helm upgrade --install myapp charts/myapp --set image.tag=$CI_COMMIT_SHORT_SHA
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: "http://myapp-review.$CI_PROJECT_NAMESPACE.$CI_PROJECT_NAME.example.com"
  only:
    - merge_requests
    - branches

# ---------------------
# Container Security Scan with Trivy
# ---------------------

container_scan:
  stage: container_scan
  image: aquasec/trivy:latest
  services:
    - docker:24.0.6-dind
  variables:
    DOCKER_TLS_CERTDIR: ""
    IMAGE_NAME: "$CI_REGISTRY_IMAGE/app"
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
    # Pull the previously pushed image
    - docker pull $IMAGE_NAME:$CI_COMMIT_SHORT_SHA
    # Run Trivy scan on the image
    - trivy image --exit-code 0 --severity HIGH,CRITICAL $IMAGE_NAME:$CI_COMMIT_SHORT_SHA
    # Modify exit-code or severity thresholds as needed
  allow_failure: false
  needs: ["deploy_app"]

# ---------------------
# End of Pipeline
# ---------------------
4.6 KB in 5 ms with coderay