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

     1  # Using LXD with the Custom executor
     2  
     3  In this example, we use LXD to create a container per build and clean it
     4  up afterwards.
     5  
     6  Here, we are using a bash script for each stage. Users
     7  [can't yet specify which image to use](https://gitlab.com/gitlab-org/gitlab-runner/issues/4357),
     8  so we are going to use an Ubuntu 18.04 image to run the jobs.
     9  
    10  These scripts have the following prerequisites:
    11  
    12  - [LXD](https://linuxcontainers.org/lxd/introduction/)
    13  - [GitLab Runner](https://docs.gitlab.com/runner/install/linux-manually.html)
    14  
    15  ## Configuration
    16  
    17  ```toml
    18  [[runners]]
    19    name = "lxd-executor"
    20    url = "https://www.gitlab.com"
    21    token = "xxxxxxxxxxx"
    22    executor = "custom"
    23    builds_dir = "/builds"
    24    cache_dir = "/cache"
    25    [runners.custom]
    26      prepare_exec = "/opt/lxd-executor/prepare.sh" # Path to a bash script to create lxd container and download dependencies.
    27      run_exec = "/opt/lxd-executor/run.sh" # Path to a bash script to run script inside the container.
    28      cleanup_exec = "/opt/lxd-executor/cleanup.sh" # Path to bash script to delete container.
    29  ```
    30  
    31  ## Base
    32  
    33  Each stage [prepare](#prepare), [run](#run), and [cleanup](#cleanup)
    34  will use this script to generate variables that are used throughout the
    35  scripts.
    36  
    37  It's important that this script is located in the same directory as the
    38  other scripts, in this case `/opt/lxd-executor/`.
    39  
    40  ```sh
    41  #!/usr/bin/env bash
    42  
    43  # /opt/lxd-executor/base.sh
    44  
    45  CONTAINER_ID="runner-$CUSTOM_ENV_CI_RUNNER_ID-project-$CUSTOM_ENV_CI_PROJECT_ID-concurrent-$CUSTOM_ENV_CI_CONCURRENT_PROJECT_ID-$CUSTOM_ENV_CI_JOB_ID"
    46  ```
    47  
    48  ## Prepare
    49  
    50  The prepare script will do the following:
    51  
    52  - Destroy a container with the same name if there is one running.
    53  - Start a container and wait for it to start.
    54  - Install [prerequisite
    55    dependencies](../custom.md#prerequisite-software-for-running-a-job).
    56  
    57  ```sh
    58  #!/usr/bin/env bash
    59  
    60  # /opt/lxd-executor/prepare.sh
    61  
    62  currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
    63  source ${currentDir}/base.sh # Get variables from base.
    64  
    65  set -eo pipefail
    66  
    67  # trap any error, and mark it as a system failure.
    68  trap "exit $SYSTEM_FAILURE_EXIT_CODE" ERR
    69  
    70  CONTAINER_ID="runner-$CUSTOM_ENV_CI_RUNNER_ID-project-$CUSTOM_ENV_CI_PROJECT_ID-concurrent-$CUSTOM_ENV_CI_CONCURRENT_PROJECT_ID"
    71  
    72  start_container () {
    73      if lxc info "$CONTAINER_ID" >/dev/null 2>/dev/null ; then
    74          echo 'Found old container, deleting'
    75          lxc delete -f "$CONTAINER_ID"
    76      fi
    77  
    78      # Container image is harcoded at the moment, since Custom executor
    79      # does not provide the value of `image`. See
    80      # https://gitlab.com/gitlab-org/gitlab-runner/issues/4357 for
    81      # details.
    82      lxc launch ubuntu:18.04 "$CONTAINER_ID"
    83  
    84      # Wait for container to start, we are using systemd to check this,
    85      # for the sake of brevity.
    86      for i in $(seq 1 10); do
    87          if lxc exec "$CONTAINER_ID" -- sh -c "systemctl isolate multi-user.target" >/dev/null 2>/dev/null; then
    88              break
    89          fi
    90  
    91          if [ "$i" == "10" ]; then
    92              echo 'Waited for 10 seconds to start container, exiting..'
    93              # Inform GitLab Runner that this is a system failure, so it
    94              # should be retried.
    95              exit "$SYSTEM_FAILURE_EXIT_CODE"
    96          fi
    97  
    98          sleep 1s
    99      done
   100  }
   101  
   102  install_dependencies () {
   103      # Install Git LFS, git comes pre installed with ubuntu image.
   104      lxc exec "$CONTAINER_ID" -- sh -c "curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash"
   105      lxc exec "$CONTAINER_ID" -- sh -c "apt-get install git-lfs"
   106  
   107      # Install gitlab-runner binary since we need for cache/artifacts.
   108      lxc exec "$CONTAINER_ID" -- sh -c "curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
   109      lxc exec "$CONTAINER_ID" -- sh -c "chmod +x /usr/local/bin/gitlab-runner"
   110  }
   111  
   112  echo "Running in $CONTAINER_ID"
   113  
   114  start_container
   115  
   116  install_dependencies
   117  ```
   118  
   119  ## Run
   120  
   121  This will run the script generated by GitLab Runner by sending
   122  the content of the script to the container via `STDIN`.
   123  
   124  ```sh
   125  #!/usr/bin/env bash
   126  
   127  # /opt/lxd-executor/run.sh
   128  
   129  currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
   130  source ${currentDir}/base.sh # Get variables from base.
   131  
   132  lxc exec "$CONTAINER_ID" /bin/bash < "${1}"
   133  if [ $? -ne 0 ]; then
   134      # Exit using the variable, to make the build as failure in GitLab
   135      # CI.
   136      exit $BUILD_FAILURE_EXIT_CODE
   137  fi
   138  ```
   139  
   140  ## Cleanup
   141  
   142  Destroy the container since the build has finished.
   143  
   144  ```sh
   145  #!/usr/bin/env bash
   146  
   147  # /opt/lxd-executor/cleanup.sh
   148  
   149  currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
   150  source ${currentDir}/base.sh # Get variables from base.
   151  
   152  echo "Deleting container $CONTAINER_ID"
   153  
   154  lxc delete -f "$CONTAINER_ID"
   155  ```