2

Context

We have a repo, hosted on a local gitlab instance.

It contains a ressource file, which should be valid. The validity is checked by a given script.

What I want

I want to ensure that, whenever the master branch is updated on the gitlab repo, the file at the new head of master passes the validation script.

Possibilities explored

  • all updates to master should go through a Merge Request

    1. I haven't found a way to enable / disable the Merge button based on the result of a script,
    2. I could upload a script in the bare hooks/ directory of the bare git repo managed by gitlab, but I don't know of a pre-merge hook in git
  • updates to master can be pushed by developpers

    1. I know how to write and upload an update hook, which validates the master branch on the bare git repo,
      I don't know how to prevent other ways to update the master branch

Question

Using gitlab and git, how can I make sure that updates to the master branch only allow hops to validated versions of the file ?

1 Answer 1

2

Using Gitlab pipelines

What you're trying to setup is Continous Integration, (in Gitlab known as "Pipeline") to check whether a merge request will pass or fail your script. Check out the Gitlab features page

The setup is quite straightforward:

  1. You create a .gitlab-ci.yml file, which describes what script needs to be run in order to test your resource file. Please note that the script should exit with 0 if the test passes and with a non-zero value otherwise.
  2. Dependent on whether your project is open source you choose if you want to use Gitlab's shared runners. A "runner" is a server application which runs your script every time it is toggled. You can setup a runner on your own server or use Gitlab's Runners.
  3. Once setup correctly GitLab will always show if your resource file is valid or not.

A configured pipeline Source: https://docs.gitlab.com/ee/ci/pipelines.html

Example

This short example might help you to setup your .gitlab-ci.yml file

stages:
    - tests

ResourceFileTests:
    stage: tests
    allow_failure: false
    script:
        - bash /path/to/the/validation_script.sh

You can use GitLab's CI lint tool to check whether your configuration file is valid or not.

Not the answer you're looking for? Browse other questions tagged or ask your own question.