GitLab - Spawn a job with any command you want

The problem: you have many scripts (let's say that they are written in python and you just want to run them typing python your-script.py) and sometimes you want to run some of them, sometimes only one, etc. There is no pattern. Additionally you want to trigger these scripts via GitLab API.

How can you do this? The first idea: let's create a job for each of them! But... then what? You want to run only a small subset of them and each time this subset might be different ☹️. You might add variables, check them and run only these jobs when IF evaluates to true, like:

But, I think that you see the problem of this approach.

What about creating a common job without any command? It will be your job to provide a command for script section (for example python run-something-and-upload-to-s3.py). This way we will have only one job in GitLab and during triggering you must provide a command.

The code:

8 lines. Woah! We used rules keyword, because we want to spawn only this job when it is triggered by GitLab API. You can read more about triggering here. In the script section there is variable: $COMMAND_TO_RUN. It will be our command.

Okay, so now, how can I trigger any command? You can do it using curl for example:

What did happen here? We triggered a pipeline and additionally we passed our command $COMMAND_TO_RUN. Here is the project, so you can see the whole picture.

If you want to run other scripts then you need to change the value of $COMMAND_TO_RUN in a request, and that's it. You are able to run another script.

Here is the output of another script for example.

Comments

Popular posts from this blog

GitLab - extends keyword

GitLab - trigger keyword

Managing Secrets in GitLab / Git