Skip to content

GitLab CI template for bash script validation

This project implements a GitLab CI/CD template to test and analyse your shell code.

Usage

This template can be used both as a CI/CD component or using the legacy include:project syntax.

Use as a CI/CD component

Add the following to your gitlab-ci.yml:

include:
  # 1: include the component
  - component: gitlab.com/to-be-continuous/bash/gitlab-ci-bash@3.4.1
    # 2: set/override component inputs
    inputs:
      bats-enabled: true # ⚠ this is only an example

Use as a CI/CD template (legacy)

Add the following to your gitlab-ci.yml:

include:
  # 1: include the template
  - project: 'to-be-continuous/bash'
    ref: '3.4.1'
    file: '/templates/gitlab-ci-bash.yml'

variables:
  # 2: set/override template variables
  BASH_BATS_ENABLED: "true" # ⚠ this is only an example

Jobs

bash-shellcheck job

This job performs a static analysis of your shell scripts using ShellCheck.

Input / Variable Description Default value
shellcheck-disabled / BASH_SHELLCHECK_DISABLED Set to true to disable ShellCheck none (enabled)
shellcheck-image / BASH_SHELLCHECK_IMAGE The Docker image used to run ShellCheck registry.hub.docker.com/koalaman/shellcheck-alpine:stable
shellcheck-files / BASH_SHELLCHECK_FILES Shell file(s) pattern to analyse **/*.sh
shellcheck-opts / BASH_SHELLCHECK_OPTS ShellCheck options none

bash-bats job

This job performs unit tests based on Bats (Bash Automated Testing System).

The job uses the following variables:

Input / Variable Description Default value
bats-enabled / BASH_BATS_ENABLED Set to true to enable bats tests none (disabled)
bats-image / BASH_BATS_IMAGE The Docker image used to run Bats registry.hub.docker.com/bats/bats:latest
bats-tests / BASH_BATS_TESTS The path to a Bats test file, or the path to a directory containing Bats test files tests
bats-opts / BASH_BATS_OPTS Bats options none
bats-libraries / BASH_BATS_LIBRARIES Coma separated list of Bats libraries and add-ons (formatted as lib_name_1@archive_url_1 lib_name_2@archive_url_2 ...) none

In addition to a textual report in the console, this job produces the following reports, kept for one day:

Report Format Usage
reports/*.bats.xml xUnit test report(s) GitLab integration

How to manage libraries and add-ons

The Docker image used only contains bats-core. If you wish to used Bats libraries and add-ons, you have two options:

  1. either use the Git submodule technique whenever possible,
  2. or configure the list of libraries to load prior to running tests with the BASH_BATS_LIBRARIES variable. The loaded libraries will then be available in $BATS_LIBRARIES_DIR/$lib_name.

Example of a project using bats-support and bats-assert:

  • BASH_BATS_LIBRARIES: "bats-support@https://github.com/bats-core/bats-support/archive/v0.3.0.zip bats-assert@https://github.com/bats-core/bats-assert/archive/v2.0.0.zip"
  • in test scripts, libraries can be loaded as follows:
    #!/usr/bin/env bats
    load "$BATS_LIBRARIES_DIR/bats-support/load.bash"
    load "$BATS_LIBRARIES_DIR/bats-assert/load.bash"
    
    @test "test something" {
        run echo "Hello there"
        assert_line "Hello there"
    }