Gitlab CI - Publish Failed Test Results to Pages - gradle

I'm creating a simple java project using Gradle which generates a test report (i.e. BDD Cucumber, JUnit, etc.). This project is deployed to Gitlab where the project is built as part of the Gitlab CI process.
My JUnit reports are generated in the folder build/reports/tests/test/ relative to the project path (as an index.html and some CSS files, etc.).
How do I configure my .gitlab-ci.yml to publish the content of build/reports/tests/test/ to the Gitlab Pages even after my test cases fail?
This is what I have in my .gitlab-ci.yml: (My repo can be found HERE)
Version 1: Doesn't publish anything to pages
image: java:8-jdk
stages:
- test
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
test:
stage: test
script:
- mkdir public
- ./gradlew test
artifacts:
paths:
- build/reports/tests/test/
only:
- master
after_script:
- mv build/reports/tests/test/* public
Version 2: Doesn't execute the deploy stage since test has failed.
image: java:8-jdk
stages:
- test
- deploy
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
test:
stage: test
script:
- ./gradlew test
artifacts:
paths:
- build/reports/tests/test/
pages:
stage: deploy
dependencies:
- test
script:
- mkdir public
- mv build/reports/tests/test/* public
artifacts:
paths:
- public
only:
- master

I solved the issue by adding the when: always at the end of my pages stage. It now executes the stage regardless of exit code from the dependent stage.

Related

gitlab CI/CD not cache maven dependencies

I am new to Gitlab CI/CD, I have setup a simple pipeline to run jobs for a maven project when merge request is sent. here is the merged content for the pipeline:
---
stages:
- ".pre"
- pre-merge
- build-artifact
- unit-test
- build-image
- tag-image
- deploy
- ".post"
compile:
stage: pre-merge
script:
- mvn $MAVEN_CLI_OPTS compile
only:
- merge_requests
build:
stage: build-artifact
script:
- mvn $MAVEN_CLI_OPTS clean package
artifacts:
untracked: false
expire_in: 1 days
paths:
- target/*.jar
only:
- main
unit-test:
stage: unit-test
script:
- mvn $MAVEN_CLI_OPTS clean test
only:
- merge_requests
- main
build-image:
stage: build-image
script:
- echo build image using docker and publish it to artifactory
only:
- main
deploy-to-dev:
stage: deploy
script:
- update the argocd direcotry
environment:
name: development
only:
- main
tag-image:
stage: tag-image
script:
- re-tag the image and publish
only:
- tags
deploy-to-pr:
stage: deploy
script:
- update the argocd direcotry
environment:
name: production
only:
- tags
default:
tags:
- maven
variables:
MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository
-Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
MAVEN_CLI_OPTS: "--batch-mode"
HELM_CHART: my-chart
cache:
paths:
- ".m2/repository"
However, seems like the cached repository is not used. I can see maven is download all the dependencies again in compile and 'unit-test' jobs. (I know I can just use the verify but I just want to test the cache in Gitlab CI)
My Setup:
Runner is using kubernetes executor
Runner is using image maven:3.8.6-openjdk-11-slim
I have exec to the runner pod and confirm that the .m2/repository is populated with jars.
no shared cache, only local

Generate plantuml pdfs GitLab CICD

I have a maven project. Inside it I have a _models/ folder where I have some .puml files.
I would like to generate the .pngs from those plantuml files each time I commit.
This is my .gitlab-ci.yml
stages:
- test
- build
- uml
.maven:
image: maven:3.6.3-jdk-11
test:
extends: .maven
script:
- mvn test
build:
extends: .maven
script:
- mvn package -B
uml:
image: miy4/plantuml
script:
- plantuml -charset UTF-8 ./_models/sequence_diagram.puml
- plantuml -charset UTF-8 ./_models/class_diagram.puml
- plantuml -charset UTF-8 ./_models/object_diagram.puml
The pipeline passes but the diagrams.png are not created in the _models folder. Do you know why?
In the GitLab pipelines it says: No diagram found but when I run locally those commands everything works fine
Thanks :)

Why is a job artifact not being added in the pipeline?

UPDATE: added when:always under artifacts fixed the issue, since the unit tests were failing, so the coverage folder was not created as an artifact
When unit tests are run, a coverage folder is created. I want to save that coverage folder as an artifact in the pipeline so that sonarqube can access the reports in that folder to give an accurate coverage report. When I push up any code, I'm not seeing the coverage folder being saved as an artifact after the unit tests are run in the pre-build stage, so it is not being passed along to sonarqube in the build stage.
This is the yml file:
stages:
- Pre-Build
- Build
- etc.
Unit Tests:
stage: Pre-Build
allow_failure: true
script:
- npm ci
- npm run test
artifacts:
paths:
- coverage
when: always
SonarQube:
stage: Build
needs: ['Unit Tests']
except:
refs:
- tags
try add slash in dir-path
Unit Tests:
stage: Pre-Build
allow_failure: true
script:
- npm ci
- npm run test
artifacts:
paths:
- coverage/
when: always

How to merge artifacts across jobs for the same stage in Gitlab CI?

In Gitlab CI artifacts are segregated based on the jobs which generated them and hence when downloading, you can only download it on a per-job basis.
Is there a way to download all the artifacts, or pass on the artifacts to some other stage and upload from there? Basically some way to merge all the artifacts of a stage.
A possible scenario where it can be needed: Let's say in a stage deploy, I am deploying my project on 10 different servers, using 10 different parallel jobs. Each of these generates some artifacts. However, there is no way to download them all from the UI.
So does anyone know of a workaround? I am not looking for API based solution, but instead UI based or editing the CI yaml file to make it work.
You can create a "final" (package) stage in your pipeline which combines all the artifacts together, using the artifacts syntax.
For example:
stages:
- build
- package
.artifacts_template:
artifacts:
name: linux-artifact
paths:
- "*.txt"
expire_in: 5 minutes
build:linux-1:
extends: .artifacts_template
stage: build
script:
- touch hello-world-linux-1.txt
build:linux-2:
extends: .artifacts_template
stage: build
script:
- touch hello-world-linux-2.txt
build:linux-3:
extends: .artifacts_template
stage: build
script:
- touch hello-world-linux-3.txt
package:
stage: package
script:
- echo "packaging everything here"
needs:
- build:linux-1
- build:linux-2
- build:linux-3
artifacts:
name: all-artifacts
paths:
- "*.txt"
expire_in: 1 month

Gitlab CI include is not triggering

Expecting:CI need to trigger build and test then it should scan for dependencies vulnerability
Current Behaviour CI trigger but only run build and test not running Dependency-Scanning.gitlab-ci.yml
stages:
- build
- test
build:
stage: build
script:
- echo "Building"
test:
stage: test
script:
- echo "Testing"
include:
- template: Dependency-Scanning.gitlab-ci.yml
Dependency-Scanning.gitlab-ci.yml can be found in the following URL
https://gitlab.com/gitlab-org/gitlab/blob/master/lib/gitlab/ci/templates/Security/Dependency-Scanning.gitlab-ci.yml
You can try moving it to the top of the file. Works for me.
Can you toss the error in your post? Are you getting something like this if you use the CI linter? https://gitlab.com/amishpanda/cheatsheet/-/ci/lint
Found errors in your .gitlab-ci.yml:
dependency_scanning job: stage parameter should be .pre
setup
build
.post
You can also test your .gitlab-ci.yml in CI Lint

Resources