github.com/nilium/gitlab-runner@v12.5.0+incompatible/docs/executors/custom.md (about)

     1  # The Custom executor
     2  
     3  > [Introduced](https://gitlab.com/gitlab-org/gitlab-runner/issues/2885) in GitLab Runner 12.1
     4  
     5  GitLab Runner provides the Custom executor for environments that it
     6  doesn't support natively, for example, Podman or Libvirt.
     7  
     8  This gives you the control to create your own executor by configuring
     9  GitLab Runner to use some executable to provision, run, and clean up
    10  your environment.
    11  
    12  ## Limitations
    13  
    14  Below are some current limitations when using the Custom executor:
    15  
    16  - No support for [`image`](https://docs.gitlab.com/ee/ci/yaml/#image).
    17    See [#4357](https://gitlab.com/gitlab-org/gitlab-runner/issues/4357)
    18    for more details.
    19  - No support for
    20    [`services`](https://docs.gitlab.com/ee/ci/yaml/#services). See
    21    [#4358](https://gitlab.com/gitlab-org/gitlab-runner/issues/4358) for
    22    more details.
    23  - No [Interactive Web
    24    Terminal](https://docs.gitlab.com/ee/ci/interactive_web_terminal/) support.
    25  
    26  ## Configuration
    27  
    28  There are a few configuration keys that you can choose from. Some of them are optional.
    29  
    30  Below is an example of configuration for the Custom executor using all available
    31  configuration keys:
    32  
    33  ```toml
    34  [[runners]]
    35    name = "custom"
    36    url = "https://gitlab.com"
    37    token = "TOKEN"
    38    executor = "custom"
    39    builds_dir = "/builds"
    40    cache_dir = "/cache"
    41    [runners.custom]
    42      config_exec = "/path/to/config.sh"
    43      config_args = [ "SomeArg" ]
    44      config_exec_timeout = 200
    45  
    46      prepare_exec = "/path/to/script.sh"
    47      prepare_args = [ "SomeArg" ]
    48      prepare_exec_timeout = 200
    49  
    50      run_exec = "/path/to/binary"
    51      run_args = [ "SomeArg" ]
    52  
    53      cleanup_exec = "/path/to/executable"
    54      cleanup_args = [ "SomeArg" ]
    55      cleanup_exec_timeout = 200
    56  
    57      graceful_kill_timeout = 200
    58      force_kill_timeout = 200
    59  ```
    60  
    61  For field definitions and which ones are required, see
    62  [`[runners.custom]`
    63  section](../configuration/advanced-configuration.md#the-runnerscustom-section)
    64  configuration.
    65  
    66  In addition both `builds_dir` and `cache_dir` inside of the
    67  [`[[runners]]`](../configuration/advanced-configuration.md#the-runners-section)
    68  are required fields.
    69  
    70  ## Prerequisite software for running a Job
    71  
    72  The user must set up the environment, including the following that must
    73  be present in the `PATH`:
    74  
    75  - [Git](https://git-scm.com/download): Used to clone the repositories.
    76  - [Git LFS](https://git-lfs.github.com/): Pulls any LFS objects that
    77    might be in the repository.
    78  - [GitLab Runner](https://docs.gitlab.com/runner/install/): Used to
    79    download/update artifacts and cache.
    80  
    81  ## Stages
    82  
    83  The Custom executor provides the stages for you to configure some
    84  details of the job, prepare and cleanup the environment and run the job
    85  script within it. Each stage is responsible for specific things and has
    86  different things to keep in mind.
    87  
    88  Each stage executed by the Custom executor is executed at the time
    89  a builtin GitLab Runner executor would execute them.
    90  
    91  For each step that will be executed, specific environment variables are
    92  exposed to the executable, which can be used to get information about
    93  the specific Job that is running. All stages will have the following
    94  environment variables available to them:
    95  
    96  - Standard CI/CD [environment
    97    variables](https://docs.gitlab.com/ee/ci/variables/), including
    98    [predefined
    99    variables](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html).
   100  - All environment variables provided by the Custom Runner host system.
   101  
   102  Both CI/CD environment variables and predefined variables are prefixed
   103  with `CUSTOM_ENV_` to prevent conflicts with system environment
   104  variables. For example, `CI_BUILDS_DIR` will be available as
   105  `CUSTOM_ENV_CI_BUILDS_DIR`.
   106  
   107  The stages run in the following sequence:
   108  
   109  1. `config_exec`
   110  1. `prepare_exec`
   111  1. `run_exec`
   112  1. `cleanup_exec`
   113  
   114  ### Config
   115  
   116  The Config stage is executed by `config_exec`.
   117  
   118  Sometimes you might want to set some settings during execution time. For
   119  example settings a build directory depending on the project ID.
   120  `config_exec` reads from STDOUT and expects a valid JSON string with
   121  specific keys.
   122  
   123  For example:
   124  
   125  ```sh
   126  #!/usr/bin/env bash
   127  
   128  cat << EOS
   129  {
   130    "builds_dir": "/builds/${CUSTOM_ENV_CI_CONCURRENT_PROJECT_ID}/${CUSTOM_ENV_CI_PROJECT_PATH_SLUG}",
   131    "cache_dir": "/cache/${CUSTOM_ENV_CI_CONCURRENT_PROJECT_ID}/${CUSTOM_ENV_CI_PROJECT_PATH_SLUG}",
   132    "builds_dir_is_shared": true,
   133    "hostname": "custom-hostname",
   134    "driver": {
   135      "name": "test driver",
   136      "version": "v0.0.1"
   137    }
   138  }
   139  EOS
   140  ```
   141  
   142  Any additional keys inside of the JSON string will be ignored. If it's
   143  not a valid JSON string the stage will fail and be retried two more
   144  times.
   145  
   146  | Parameter | Type | Required | Allowed empty | Description |
   147  |-----------|------|----------|---------------|-------------|
   148  | `builds_dir` | string | ✗ | ✗ | The base directory where the working directory of the job will be created. |
   149  | `cache_dir` | string | ✗ | ✗ | The base directory where local cache will be stored. |
   150  | `builds_dir_is_shared` | bool | ✗ | n/a | Defines whether the environment is shared between concurrent job or not. |
   151  | `hostname` | string | ✗ | ✓ | The hostname to associate with job's "metadata" stored by Runner. If undefined, the hostname is not set. |
   152  | `driver.name` | string | ✗ | ✓ | The user-defined name for the driver. Printed with the `Using custom executor...` line. If undefined, no information about driver is printed. |
   153  | `driver.version` | string | ✗ | ✓ | The user-defined version for the drive. Printed with the `Using custom executor...` line. If undefined, only the name information is printed. |
   154  
   155  The `STDERR` of the executable will print to the job log.
   156  
   157  The user can set
   158  [`config_exec_timeout`](../configuration/advanced-configuration.md#the-runnerscustom-section)
   159  if they want to set a deadline for how long GitLab Runner should wait to
   160  return the JSON string before terminating the process.
   161  
   162  If any of the
   163  [`config_exec_args`](../configuration/advanced-configuration.md#the-runnerscustom-section)
   164  are defined, these will be added in order to the executable defined in
   165  `config_exec`. For example we have the `config.toml` content below:
   166  
   167  ```toml
   168  ...
   169  [runners.custom]
   170    ...
   171    config_exec = "/path/to/config"
   172    config_args = [ "Arg1", "Arg2" ]
   173    ...
   174  ```
   175  
   176  GitLab Runner would execute it as `/path/to/config Arg1 Arg2`.
   177  
   178  ### Prepare
   179  
   180  The Prepare stage is executed by `prepare_exec`.
   181  
   182  At this point, GitLab Runner knows everything about the job (where and
   183  how it's going to run). The only thing left is for the environment to be
   184  set up so the job can run.  GitLab Runner will execute the executable
   185  that is specified in `prepare_exec`.
   186  
   187  This is responsible for setting up the environment (for example,
   188  creating the virtual machine or container, or anything else). After this
   189  is done, we expect that the environment is ready to run the job.
   190  
   191  This stage is executed only once, in a job execution.
   192  
   193  The user can set
   194  [`prepare_exec_timeout`](../configuration/advanced-configuration.md#the-runnerscustom-section)
   195  if they want to set a deadline for how long GitLab Runner
   196  should wait to prepare the environment before terminating the process.
   197  
   198  The `STDOUT` and `STDERR` returned from this executable will print to
   199  the job log.
   200  
   201  If any of the
   202  [`prepare_exec_args`](../configuration/advanced-configuration.md#the-runnerscustom-section)
   203  are defined, these will be added in order to the executable defined in
   204  `prepare_exec`. For example we have the `config.toml` content below:
   205  
   206  ```toml
   207  ...
   208  [runners.custom]
   209    ...
   210    prepare_exec = "/path/to/bin"
   211    prepare_args = [ "Arg1", "Arg2" ]
   212    ...
   213  ```
   214  
   215  GitLab Runner would execute it as `/path/to/bin Arg1 Arg2`.
   216  
   217  ### Run
   218  
   219  The Run stage is executed by `run_exec`.
   220  
   221  The `STDOUT` and `STDERR` returned from this executable will print to
   222  the job log.
   223  
   224  Unlike the other stages, the `run_exec` stage is executed multiple
   225  times, since it's split into sub stages listed below in sequential
   226  order:
   227  
   228  1. `prepare_script`
   229  1. `get_sources`
   230  1. `restore_cache`
   231  1. `download_artifacts`
   232  1. `build_script`
   233  1. `after_script`
   234  1. `archive_cache`
   235  1. `upload_artifact_on_success` OR `upload_artifact_on_failure`
   236  
   237  For each stage mentioned above, the `run_exec` executable will be
   238  executed with:
   239  
   240  - The usual environment variables.
   241  - Two arguments:
   242    - The path to the script that GitLab Runner creates for the Custom
   243      executor to run.
   244    - Name of the stage.
   245  
   246  For example:
   247  
   248  ```sh
   249  /path/to/run_exec.sh /path/to/tmp/script1 prepare_executor
   250  /path/to/run_exec.sh /path/to/tmp/script1 prepare_script
   251  /path/to/run_exec.sh /path/to/tmp/script1 get_sources
   252  ```
   253  
   254  If you have `run_args` defined, they are the first set of arguments
   255  passed to the `run_exec` executable, then GitLab Runner adds others. For
   256  example, suppose we have the following `config.toml`:
   257  
   258  ```toml
   259  ...
   260  [runners.custom]
   261    ...
   262    run_exec = "/path/to/run_exec.sh"
   263    run_args = [ "Arg1", "Arg2" ]
   264    ...
   265  ```
   266  
   267  GitLab Runner will execute the executable with the following arguments:
   268  
   269  ```sh
   270  /path/to/run_exec.sh Arg1 Arg2 /path/to/tmp/script1 prepare_executor
   271  /path/to/run_exec.sh Arg1 Arg2 /path/to/tmp/script1 prepare_script
   272  /path/to/run_exec.sh Arg1 Arg2 /path/to/tmp/script1 get_sources
   273  ```
   274  
   275  This executable should be responsible of executing the scripts that are
   276  specified in the first argument. They contain all the scripts any GitLab
   277  Runner executor would run normally to clone, download artifacts, run
   278  user scripts and all the other steps described below. The scripts can be
   279  of the following shells:
   280  
   281  - Bash
   282  - PowerShell
   283  - Batch (Deprecated)
   284  
   285  We generate the script using the shell configured by `shell` inside of
   286  [`[[runners]]`](../configuration/advanced-configuration.md#the-runners-section).
   287  If none is provided the defaults for the OS platform are used.
   288  
   289  The table below is a detailed explanation of what each script does and
   290  what the main goal of that script is.
   291  
   292  | Script Name | Script Contents |
   293  |:-----------:|:---------------:|
   294  | `prepare_script` | Simple debug info on which machine the Job is running on. |
   295  | `get_sources`    | Prepares the Git config, and clone/fetch the repository. We suggest you keep this as is since you get all of the benefits of Git strategies that GitLab provides. |
   296  | `restore_cache` | Extract the cache if any are defined. This expects the `gitlab-runner` binary is available in `$PATH`. |
   297  | `download_artifacts` | Download artifacts, if any are defined. This expects `gitlab-runner` binary is available in `$PATH`. |
   298  | `build_script` | This is a combination of [`before_script`](https://docs.gitlab.com/ee/ci/yaml/#before_script-and-after_script) and [script](https://docs.gitlab.com/ee/ci/yaml/#script). |
   299  | `after_script` | This is the [`after_script`](https://docs.gitlab.com/ee/ci/yaml/#before_script-and-after_script) defined from the job. This is always called even if any of the previous steps failed. |
   300  | `archive_cache` | Will create an archive of all the cache, if any are defined. |
   301  | `upload_artifact_on_success` | Upload any artifacts that are defined. Only executed when `build_script` was successful. |
   302  | `upload_artifact_on_failure` | Upload any artifacts that are defined. Only exected when `build_script` fails. |
   303  
   304  ### Cleanup
   305  
   306  The Cleanup stage is executed by `cleanup_exec`.
   307  
   308  This final stage is executed even if one of the previous stages failed.
   309  The main goal for this stage is to clean up any of the environments that
   310  might have been set up. For example, turning off VMs or deleting
   311  containers.
   312  
   313  The result of `cleanup_exec` does not affect job statuses. For example,
   314  a job will be marked as successful even if the following occurs:
   315  
   316  - Both `prepare_exec` and `run_exec` are successful.
   317  - `cleanup_exec` fails.
   318  
   319  The user can set
   320  [`cleanup_exec_timeout`](../configuration/advanced-configuration.md#the-runnerscustom-section)
   321  if they want to set some kind of deadline of how long GitLab Runner
   322  should wait to clean up the environment before terminating the
   323  process.
   324  
   325  The `STDOUT` of this executable will be printed to GitLab Runner logs at a
   326  DEBUG level. The `STDERR` will be printed to the logs at a WARN level.
   327  
   328  If any of the
   329  [`cleanup_exec_args`](../configuration/advanced-configuration.md#the-runnerscustom-section)
   330  are defined, these will be added in order to the executable defined in
   331  `cleanup_exec`. For example we have the `config.toml` content below:
   332  
   333  ```toml
   334  ...
   335  [runners.custom]
   336    ...
   337    cleanup_exec = "/path/to/bin"
   338    cleanup_args = [ "Arg1", "Arg2" ]
   339    ...
   340  ```
   341  
   342  GitLab Runner would execute it as `/path/to/bin Arg1 Arg2`.
   343  
   344  ## Terminating and killing executables
   345  
   346  GitLab Runner will try to gracefully terminate an executable under any
   347  of the following conditions:
   348  
   349  - `config_exec_timeout`, `prepare_exec_timeout` or `cleanup_exec_timeout` are met.
   350  - The job [times out](https://docs.gitlab.com/ee/user/project/pipelines/settings.html#timeout).
   351  
   352  When a timeout is reached, a `SIGTERM` is sent to the executable, and
   353  the countdown for
   354  [`exec_terminate_timeout`](../configuration/advanced-configuration.md#the-runnerscustom-section)
   355  starts. The executable should listen to this signal to make sure it
   356  cleans up any resources. If `exec_terminate_timeout` passes and the
   357  process is still running, a `SIGKILL` is sent to kill the process and
   358  [`exec_force_kill_timeout`](../configuration/advanced-configuration.md#the-runnerscustom-section)
   359  will start. If the process is still running after
   360  `exec_force_kill_timeout` has finished, GitLab Runner will abandon the
   361  process and will not try to stop/kill anymore. If both these timeouts
   362  are reached during `config_exec`, `prepare_exec` or `run_exec` the build
   363  is marked as failed.
   364  
   365  ## Error handling
   366  
   367  There are two types of errors that GitLab Runner can handle differently.
   368  These errors are only handled when the executable inside of
   369  `config_exec`, `prepare_exec`, `run_exec`, and `cleanup_exec` exits with
   370  these codes. If the user exits with a non-zero exit code, it should be
   371  propagated as one of the error codes below.
   372  
   373  If the user script exits with one of these code it has to
   374  be propagated to the executable exit code.
   375  
   376  ### Build Failure
   377  
   378  GitLab Runner provides `BUILD_FAILURE_EXIT_CODE` environment
   379  variable which should be used by the executable as an exit code to
   380  inform GitLab Runner that there is a failure on the users job. If the
   381  executable exits with the code from
   382  `BUILD_FAILURE_EXIT_CODE`, the build is marked as a failure
   383  appropriately in GitLab CI.
   384  
   385  If the script that the user defines inside of `.gitlab-ci.yml` file
   386  exits with a non-zero code, `run_exec` should exit with
   387  `BUILD_FAILURE_EXIT_CODE` value.
   388  
   389  NOTE: **Note:**
   390  We strongly suggest using `BUILD_FAILURE_EXIT_CODE` to exit
   391  instead of a hard coded value since it can change in any release, making
   392  your binary/script future proof.
   393  
   394  ### System Failure
   395  
   396  You can send a system failure to GitLab Runner by exiting the process with the
   397  error code specified in the `SYSTEM_FAILURE_EXIT_CODE`. If this error
   398  code is returned, on certain stages GitLab Runner will retry the stage, if none
   399  of the retries are successful the job will be marked as failed.
   400  
   401  Below is a table of what stages are retried, and by how many times.
   402  
   403  | Stage Name           | Number of attempts                                          | Duration to wait between each retry |
   404  |----------------------|-------------------------------------------------------------|-------------------------------------|
   405  | `prepare_exec`       | 3                                                           | 3 seconds                           |
   406  | `get_sources`        | Value of `GET_SOURCES_ATTEMPTS` variable. (Default 1)       | 0 seconds                           |
   407  | `restore_cache`      | Value of `RESTORE_CACHE_ATTEMPTS` variable. (Default 1)     | 0 seconds                           |
   408  | `download_artifacts` | Value of `ARTIFACT_DOWNLOAD_ATTEMPTS` variable. (Default 1) | 0 seconds                           |
   409  
   410  NOTE: **Note:**
   411  We strongly suggest using `SYSTEM_FAILURE_EXIT_CODE` to exit
   412  instead of a hard coded value since it can change in any release, making
   413  your binary/script future proof.
   414  
   415  ## Examples
   416  
   417  A set of example executors using the Custom executor can be found in
   418  the [examples page](custom_examples/index.md).