Search code examples
gitunit-testingdockertest-kitchen

What is the best way to run test in docker before deploying


My current settings is that I have set my git bare repository in my VPS call it staging.

So, when I do:

git push staging staging

I rerun my docker with mounted directory refer to the git-workdirs.

Now, I'd like to also add running the test inside that docker, and if something fails, roll it back to the previous (working) commit. [1]

what is the best way to do [1]

my current hooks looks like this:

$worktree = /my/work/directory
$gitdir = /my/repo/dir.git
$containername = mycontainer
git --work-tree=$worktree  --git-dir=$gitdir checkout staging -f
cd $worktree
docker stop $containername || true
docker kill $containername || true
docker rm $containername || true
docker run -d -p $port1:$port2 --name $containername -v $worktree:/root/src -it $imagename bash -c $runscript

Solution

  • (May not answer your question directly, but with different approach, that you needn't re-invent wheel)

    I did it with Kitchen test, which can start a new test instance , it can be vmware, ec2 instance, etc, it can be different drivers (vagrant, ec2, docker, etc, of course, you can't test docker in docker) and you can run the exact same codes on it to verify automatically.

    Here is Getting started guide

    If everything is set properly, one command kitchen test to run all deployment, test and destroy the instance directly.

    The test case is using serverspec , which you can easily test the instance running, port listening, etc. For detail about the test case, refer serverspec resource types

    You can also manually run below steps to get troubleshooting step-by-step

    kitchen test = kitchen create + kitchen converge + kitchen verify + kitchen destroy