Local Jenkins in docker using GCP code repositories
Today I decided to setup my own jenkins CI server that runs on my desktop, using docker seems like a good idea. I also needed it to have access to GCP source repositories.
Here's what I did:
Create a volume and run a vanilla jenkins docker container which mounts it as it's home directory like so:
docker volume create --name jenkins_home --driver local docker run -d -p 8080:8080 -p 50000:50000 --name myjenkins \ -v jenkins_home:/var/jenkins_home \ jenkins
Create a service account just for jenkins and add read only source repository access to the role. This will give you a .json file with a private_key and associated project id.
Copy this into the jenkins_home docker volume
docker cp certs.json myjenkins:certs.json
You will need this to get a tty within the docker container:
docker exec -t -i myjenkins bash
Within the docker container:
Install the google-cloud-sdk in userspace into the jenkins_home volume, /var/jenkins_home/google-cloud-sdk.
curl https://sdk.cloud.google.com | bash # and follow instructions (there's a headless version of this too)
Activate the service account
gcloud auth activate-service-account --key-file certs.json
Tell git to use the gcloud git credential helper
git config --global credential.helper gcloud.sh
Within the jenkins UI, add gcloud tools to the path. There's an environment variables section, set name: PATH and value: $PATH:/var/jenkins_home/google-cloud-sdk/bin.
And now we are done! You can now create jenkins pipelines that check out GCP source repositories using their respective https git address. eg:
https://source.developers.google.com/p/my-project-name/r/my-code-repo
You don't have to do this again if you recreate the container, as all the changes were within the volume. So you can delete the container and recreate it with the same volume and all will be well.
The process seems a bit more manual than I'd like, but rather than automate it more I decided to document it.
Extra bonus work I should do: use google cloud storage for the volume driver instead of a local volume. This seems like it should be easy.










