Seems there is a lot of info about this already, but it still took me literally days to get what i wanted, so here is a quick recap, in case you are having issues with this too.
- you probably don’t need the docker plugin for jenkins, i spent way too much time trying to understand how it worked, and it didn’t do what i want in the end.
- you can easily execute docker commands in a shell script by adding jenkins to the docker group, no need to add a tcp socket in docker configuration to use the rest api, simple commands just work.
- you can simply mount the workspace into the docker container (-v $WORKSPACE:/home/user/workspace -w /home/user/workspace) and pass the right command to build it.
- you’ll probably want the user in the docker instance to have the same uid as the jenkins user doing the setup, just get it from /etc/passwd and use the –uid option to give the same one to your user
- docker doesn’t set the HOME env var to the user one, unless the image you are using has been fixed for that, so pass -e HOME=/home/user
- you don’t want “-t” to docker run, because that will make the build success immediatly without waiting for the end of your commands
- to avoid docker containers piling up, just add –rm so the container gets destroyed at the end of the job, the result should be in the workspace anyway.
So in the end, i just did a freestyle or multiconfiguration project, set up the scm (git), and put a shell script build step, along the lines of
USER=jenkins RM=--rm # comment out to keep the instance around for debugging #TTY=-t #useful to debug IMAGE=jenkins-docker COMMAND="buildozer android debug" HOME=/home/$USER docker run $RM $TTY -i -v $WORKSPACE:$HOME/build -u $USER -w $HOME/build -e HOME=$HOME $IMAGE $COMMAND
bonus point: you can totally run that out of jenkins to test it manually (just set WORKSPACE to a copy of your project), which can ease the preparation of your image and avoid cluttering jenkins with unneeded failed builds.