8

I run Jenkins and my app is dockerized, i.e. when I run the container it exposes port 3000 and I can point my browser there. On every Github PR I would like to deploy that git commit to a running container somewhere and have Jenkins post back to the PR the link where it can be accessed. On any PR updates it gets auto re-deployed and on PR close/resolve it gets torn down.

I have looked at kubernetes and a little rancher, but what's the easiest way to get this going assuming I can only deploy to one box?

tesserakt
  • 3,231
  • 4
  • 27
  • 40
  • Super easy alternative to this setup would be CircleCi or Travis as they dedicate virtual machines for each build ( for each PR ) and then after build machine is destroyed. – Jacek Lawniczak Mar 01 '17 at 21:27
  • Jenkins is what we use b/c of its flexibility. We will not be switching. – tesserakt Mar 03 '17 at 01:24
  • I don't really know about jenkins, but if there is a "on PR" script, you could write a script which builds the image from the source repo/branch and launch it on another port (i.e 3001). On a new PR, it checks if the container exists and deletes it before new build... – Sylvain GIROD Mar 13 '17 at 23:14
  • yeah Gitlab has review apps which are what I was trying to recreate. – tesserakt Nov 14 '17 at 17:46

3 Answers3

3

There is a jenkins plugin github-pullrequest can resolve your problem.

Prerequisites:

  1. You have a jenkins server can access by internet if you want trigger your build by a webhook.
  2. Your have a github API token to access/admin your git repository, it can be generate by yourself in settings.

Please follow the guide configuration to setup your jenkins integration with github.

After configuration:

  • you can trigger your build by PR events: opened/commit changed/closed, or comment with specific pattern.
  • you can get a PR status via environment variable ${GITHUB_PR_STATE}, so you can start or stop a container on specific value.
  • you can publish a comment to a PR to tell the address of your web service after you started docker container.

About expose port of cotainer with multi PR, your can just run container with -p 3000, it will auto expose a port in a range on the docker host, docker port <container> will show the specific port number, so for example:

  • container1 with address <host>:32667 for PR1
  • container2 with address <host>:35989 for PR2
Shawyeok
  • 1,186
  • 1
  • 8
  • 15
1

I think the simplest solution to this would be to create two different Jenkins Jobs, one which deploys and the other which nukes it. The trigger for this can be set 2 webhooks in GitHub one for PR create and one for PR resolve.

Jeel
  • 2,227
  • 5
  • 23
  • 38
1

As Sylvain GIROD pointed out:

With only one box to run the application you need to change the port that is exposed. When a GitHub PR happens you deploy your application (docker run -p newport:containerport). If you are deploying services you change the target port.

Then you send the link with this port back to the user (email?).

Additionally you need some key-value store to remember which pod was created for which users so that you can decide on a new PR whether to destroy the old containers.

I would also suggest giving the services a time to live and regularly cleaning up stale containers/services.

herm
  • 14,613
  • 7
  • 41
  • 62