currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" source${currentDir}/base.sh # Get variables from base.
set -eo pipefail
# trap any error, and mark it as a system failure. trap"exit $SYSTEM_FAILURE_EXIT_CODE" ERR
start_container () { # 如果有同名container就删掉 if lxc info "$CONTAINER_ID" >/dev/null 2>/dev/null ; then echo'Found old container, deleting' lxc delete -f "$CONTAINER_ID" fi # 这里就是跟官方不同的地方,我们复制一份模板(grbase)来做runner lxc copy grbase "$CONTAINER_ID" # 需要手动start lxc start "$CONTAINER_ID"
# Wait for container to start, we are using systemd to check this, # for the sake of brevity. for i in $(seq 1 10); do if lxc exec"$CONTAINER_ID" -- sh -c "systemctl isolate multi-user.target" >/dev/null 2>/dev/null; then break fi
if [ "$i" == "10" ]; then echo'Waited for 10 seconds to start container, exiting..' # Inform GitLab Runner that this is a system failure, so it # should be retried. exit"$SYSTEM_FAILURE_EXIT_CODE" fi
sleep 1s done }
echo"Running in $CONTAINER_ID"
start_container
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/bash
# /opt/lxd-driver/run.sh
currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" source${currentDir}/base.sh # Get variables from base.
lxc exec"$CONTAINER_ID" /bin/bash < "${1}" if [ $? -ne 0 ]; then # Exit using the variable, to make the build as failure in GitLab # CI. exit$BUILD_FAILURE_EXIT_CODE fi
1 2 3 4 5 6 7 8 9 10 11
#!/usr/bin/env bash
# /opt/lxd-driver/cleanup.sh
currentDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" source${currentDir}/base.sh # Get variables from base.
[[runners]] name = "lxd-driver" url = "你的gitlab的网址" token = "你的token" executor = "custom" builds_dir = "/builds" cache_dir = "/cache" [runners.custom] prepare_exec = "/opt/lxd-driver/prepare.sh"# Path to a bash script to create lxd container and download dependencies. run_exec = "/opt/lxd-driver/run.sh"# Path to a bash script to run script inside the container. cleanup_exec = "/opt/lxd-driver/cleanup.sh"# Path to bash script to delete container.