Skip to content

Instantly share code, notes, and snippets.

@adamstraube
Last active December 14, 2022 12:41
Show Gist options
  • Save adamstraube/b5c8eae3034f3d8d5561cfb143751d7e to your computer and use it in GitHub Desktop.
Save adamstraube/b5c8eae3034f3d8d5561cfb143751d7e to your computer and use it in GitHub Desktop.
Bash shell wrapper for running git-runner locally within WSL on a Windows 10 machine using Docker Desktop.
#!/usr/bin/env bash
if [[ $# -eq 0 ]]
then
echo "Usage: {up|down|runStages}"
exit 0
fi
PWD_RESOLVED="$(pwd -P)"
function gitlabLoop() {
stages=($(echo "$1" | tr ',' '\n'))
if [ -z "$stages" ]; then
echo "Running test stage only"
gitlabExec "test"
else
echo "Running stage(s): ${stages[*]}"
for stage in ${stages[*]};
do
gitlabExec $stage
done
fi
}
function gitlabExec() {
docker exec -w $PWD_RESOLVED -it gitlab-runner \
gitlab-runner exec docker $1 \
--docker-privileged \
--docker-disable-cache \ --cache-dir=/tmp/gitlabrunner \
--docker-volumes '/var/run/docker.sock:/var/run/docker.sock' \
--env ROOT_PWD=$PWD_RESOLVED
}
case "$1" in
## TO CREATE/START DOCKER CONTAINER FOR GITLAB
up)
docker run -d --name gitlab-runner --restart always \
-v $PWD_RESOLVED:$PWD_RESOLVED \
-v /var/run/docker.sock:/var/run/docker.sock \
--workdir $PWD_RESOLVED \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
gitlab/gitlab-runner:latest
;;
## TO RUN RUNNER AFTER GITLAB CONTAINER HAS STARTED
runStages)
mkdir -p /tmp/gitlabrunner
gitlabLoop ${2}
rm -r /tmp/gitlabrunner
;;
## TO STOP GITLAB CONTAINER
down)
docker stop gitlab-runner
docker rm gitlab-runner
;;
esac
@yonixw
Copy link

yonixw commented Jun 22, 2020

It looks like now you need to write job name and not step name, here is my updated version that worked in WSL2:

#!/usr/bin/env bash

# 1. Start containter: `rungit.sh up`
# 2. Run stage:  `rungit.sh job <job>`
# 3. Stop containter: `rungit.sh down`

if [[ $# -eq 0 ]]
then
        echo "Usage: {up|down|job}"
        exit 0
fi

PWD_RESOLVED="$(pwd -P)"

function gitlabLoop() {
        stages=($(echo "$1" | tr ',' '\n'))
        if [ -z "$stages" ]; then
                echo "Please pass job name"
        else
                echo "Running jobs(s): ${stages[*]}"
                for stage in ${stages[*]};
                        do
                                gitlabExec $stage
                        done
        fi
}

function gitlabExec() {
    echo PWD=$PWD_RESOLVED
    echo JOB=$1
    docker exec -w $PWD_RESOLVED -it gitlab-runner gitlab-runner exec docker $1 --docker-privileged --docker-disable-cache --docker-volumes '/var/run/docker.sock:/var/run/docker.sock' --env ROOT_PWD=$PWD_RESOLVED
}

case "$1" in

        ## TO CREATE/START DOCKER CONTAINER FOR GITLAB
        up)
                docker run -d --name gitlab-runner --restart always \
                  -v $PWD_RESOLVED:$PWD_RESOLVED \
                  -v /var/run/docker.sock:/var/run/docker.sock \
                  --workdir $PWD_RESOLVED \
                  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
                  gitlab/gitlab-runner:latest
        ;;

        ## TO RUN RUNNER AFTER GITLAB CONTAINER HAS STARTED
        job)
                mkdir -p /tmp/gitlabrunner
                gitlabLoop ${2}
                rm -r /tmp/gitlabrunner
        ;;

        ## TO STOP GITLAB CONTAINER
        down)
                docker stop gitlab-runner
                docker rm gitlab-runner
        ;;
esac

@dragon788
Copy link

I've updated this for a bit more robustness and to make it almost functionally equivalent to running Docker in Docker on Gitlab CI. I haven't tested under WSL, but it works great on macOS and should still work in Linux.

https://gist.github.com/dragon788/485a483f20bcb205bd75866cbfb921f6

@inorton
Copy link

inorton commented May 5, 2022

@inorton
Copy link

inorton commented May 5, 2022

Aha! I just realised that exec doesn't work for jobs that are included from other files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment