Unit testing GitLab CI/CD job scripts via ShellSpec
2.7K
A collection of helper functions for unit testing script clauses of GitLab CI/CD configuration files (.gitlab-ci.yml) via ShellSpec.
Assume the following .gitlab-ci.yml pipeline configuration:
variables:
USER: Michael
job-1:
script:
- echo "Hello, World!"
- echo "Hello, $USER!"
A unit test can verify the output:
#shellcheck shell=bash
Describe '.gitlab-ci.yml'
BeforeEach ci_spec::export_all_global_variables
It 'says hello to the world and to me'
When call ci_spec::eval_job 'job-1'
The lines of output should equal 2
The 1st line should equal 'Hello, World!'
The 2nd line should equal 'Hello, Michael!'
End
End
The topic is also discussed in the blog post on Docker-based ShellSpec Tests for GitLab CI/CD Job Scripts.
To execute unit tests locally via Docker, run:
docker run --rm -t -v ".:/src" mgrafl/gitlab-ci-shellspec
.gitlab-ci.ymlIn the GitLab CI/CD configuration file .gitlab-ci.yml, use the Docker image, but override the entrypoint (see GitLab documentation on overriding the entrypoint of an image).
shellspec:
image:
name: mgrafl/gitlab-ci-shellspec
entrypoint: [""]
stage: test
script:
- gitlab-ci-shellspec
mgrafl/gitlab-ci-shellspec on GitLab.
This project is licensed under the MIT license.
Content type
Image
Digest
sha256:b40edde9c…
Size
17.6 MB
Last updated
about 2 years ago
docker pull mgrafl/gitlab-ci-shellspec