Fork me on GitHub

bars and waves

caprice blog

bars and waves

Automatic deployment using docker hub webhook and jenkins

Previously, based on the principle of minimal code and simple operation, the automatic deployment of my blog was performed using travis. After the travis test passed, the deployment script was executed. The script was actually ssh connected to the deployment server and run a few commands. Successful connection, you also need to put the encrypted id_rsa into the project. Obviously, the problem with this method is that the project code is highly coupled with the deployment code, and the actual operation is not much, which violates the simplest operation principle! Or It is up to the most professional tool, jenkins, to deploy this.

jenkins Simplest Deployment

Since we are pursuing the simplest, then we must go to docker, and docker hup just happens to support monitoring the github repository to automatically build an image and issue a webhook. So far the deployment plan has been formed: git push After github issues a webhook to the docker hub, docker hub starts to build the image After pushing the webhook to Jenkins, Jenkins only needs to pull down the latest image and deploy it, and the whole task is completed.

Practical

  1. First install the java runtime environment:
     sudo apt-get update
     sudo apt-get install default-jre
    Then install and run jenkins.
    wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
    java -jar jenkins.war --httpPort = 8080
    Visit http: // your-jenkins-location: 8080 After completing some basic jenkins configuration wizards, go to the plugin management and install the plugin "Generic Webhook Trigger Plugin" to receive the webhook from the docker hub.

! [jenkins-build-trigger] (/static/picture/jenkins-build-trigger.png)

  1. Create a new project in jenkins, set the build trigger to "Generic Webhook Trigger", and optionally set the token, and note that the webhook address is http: // JENKINS_URL / generic-webhook-trigger / invoke, which will be used later.

    Then configure the build command:

    docker pull derekchuank / bars-and-waves
    docker stop bars-and-waves-container || true
    docker rm bars-and-waves-container || true
    docker run --name = bars-and-waves-container --restart = always -d \
     -p 80:80 derekchuank / bars-and-waves
  2. Enter the docker hub project repository and connect to the corresponding github repository to automatically build the image. Enter the webhook configuration page, fill in the address obtained in the second step, and finish.