gitlabgitlab-ci-runnergitlab-apigitlab-ce

Gitlab-runner on host with endpoint-device


I am wondering about one theoretical problem, i have several gitlab runners as Docker Containers on hosts to which endpoint device are connected via Ethernet cable.

I would like the endpoint device to be turned on only when Gitlab calls a specific Gitlab-runner (the endpoint device is controlled separately) and turned off as soon as the job is completed and the given gitlab-runner is temporarily unemployed.

I don’t know how to approach this problem yet, any suggestions?

Gitlab Server - 16.0.3-CE Gitlab-runner - 16.0.1

Thanks for all ideas


Solution

  • There's a few ways you might be able to implement this.

    1. Implementing required steps in your runner configuration
    2. Using a custom runner executor
    3. Using script steps in your GitLab jobs

    Using runner configuration

    The runner configuration has two key configuration parameters: pre_build_script and post_build_script which can be used to run scripts before and after a job runs. You could configure your pre_build_script to turn on your device and configure your post_build_script to turn the device off.

    These can be set in the runner config.toml:

    [[runners]]
    # ...
    pre_build_script = /usr/local/bin/turn-on-device
    post_build_script = /usr/local/bin/turn-off-device
    

    Caveat: this assumes your runner is configured to only allow one job at a time (the concurrent setting is set to 1). Otherwise, if you plan to have concurrent jobs access the device simultaneously, you will need your pre_build and post_build scripts to make a determination of whether the device is already on or in use by another job to ensure that your post_build script does not turn off the device while another job is using the device.

    Using a custom runner

    Similar to the above, custom executors can define a prepare_exec program to run to prepare the job environment and a cleanup_exec program to cleanup the job environment after the job, which you can configure to execute scripts used to turn your device on and off.

    This might be a useful approach if you're already using a custom executor for some other reason.

    [[runners]]
      name = "custom"
      executor = "custom"
      # ...
      [runners.custom]
        # ...
        prepare_exec = "/usr/local/bin/turn-on-device"
        # ...
        cleanup_exec = "/usr/local/bin/turn-off-device"
        # ...
    

    Using script steps in your GitLab job

    Yet another approach would be to implement something similar to the approaches described above, but using before_script: and after_script: steps to turn the device on and off as part of the job itself.

    before_script:
      - ./turn-device-on.sh # or whatever
    
    after_script:
      - ./turn-device-off.sh # or whatever
    

    This approach would work well with cases where you do not control the runner configuration.

    Caveat: in some cases, after_script: may not run, such as in the case of a runner system failure, or if a job is canecelled