Continuous integration using Jenkins (PHP code)

Posted by s_dhumal on Fri, 31 May 2019 21:06:13 +0200

In the docker project, jenkins built by a single organization use gitlab web hook to integrate the project continuously. Note that this project is php project without build and test part. As for the test part, we will only carry out a submission and deployment function in the next section. This session will not talk about how to use docker to deliver.

Project structure description

Where data/jenkins is the directory where the status jenkins are stored

CI-server.yml is the jenkisn startup structure

version: "3.0"
services:
  jenkins:
    image: jenkins:latest
    environment:
       DOCKER_URL: ''
    volumes:
      - "/etc/localtime:/etc/localtime:ro"
#Backup data
      - "./data/jenkins:/var/jenkins_home"
      - "./dockerfiles/jenkins/.ssh:/var/jenkins_home/.ssh"
    ports:
      - "8999:8080"
#      - "50000:50000"

docker-compose orchestrated project structure

The difference between docker-production.yml and docker-compose.yml is that the secure port is closed and some configuration files are used online.

version:  '3.2'
services:
  nginx:
    image: nginx:1.13.3
    ports:
      - "80:80"
    links:
      - "php-fpm:php-fpm"
    volumes:
#Website directory
      - ./app:/var/www/html
      - ./dockerfiles/nginx/conf.d:/etc/nginx/conf.d:ro
#log file
      - ./logs/nginx:/var/log/nginx
      - /etc/localtime:/etc/localtime:ro
    restart: always
    command: nginx -g 'daemon off;'
##mysql db
  mysql-db:
    image: mysql:5.7
    volumes:
      - ./data/mysql:/var/lib/mysql:rw
      - ./logs/mysql:/var/lib/mysql-logs:rw
      - ./dockerfiles/mysql/conf.d:/etc/mysql/conf.d:ro
      - /etc/localtime:/etc/localtime:ro
    restart: always
    environment:
        MYSQL_ROOT_PASSWORD: zhaojun
        MYSQL_DATABASE: package_v1
        MYSQL_USER: zhaojun
        MYSQL_PASSWORD: zhaojun

  redis-db:
    build: ./dockerfiles/redis
    restart: always
    volumes:
      - ./data/redis:/data
      - /etc/localtime:/etc/localtime:ro

  php-fpm:
    build: ./dockerfiles/php/
    restart: always
    environment:
        TP_APP_DEBUG: 0
        APP_DEBUG: 1
        APP_DOCKER_DEV: 0
        MYSQL_HOST: mysql-db
        MYSQL_DATABASE: zushouyou_v1
        MYSQL_USER: root
        MYSQL_PASSWORD: zhaojun
        MYSQL_PORT: 3306
    volumes:
#Website directory
      - ./app:/var/www/html:rw
#configuration file
      - ./dockerfiles/php/php.ini:/usr/local/etc/php/php.ini:ro
      - ./dockerfiles/php/php-fpm.conf:/usr/local/etc/php-fpm.conf:ro
      - /etc/localtime:/etc/localtime:ro
#Mount site logs
      - ./logs/php-fpm:/var/log/php-fpm:rw
    depends_on:
      - mysql-db
      - redis-db
    links:
      - mysql-db:mysql-db
      - redis-db:redis-db
    command: php-fpm

##job
  takser:
    image: node:6.9
    environment:
        MYSQL_HOST: mysql-db
        SERVER_HOST: server
    volumes:
      - ./app/node-task:/task
      - /etc/localtime:/etc/localtime:ro
    links:
      - mysql-db:mysql-db
      - redis-db:redis-db
      - nginx:server
    working_dir: /task
    command: /bin/bash -c "npm install && node ./index.js"

##composer build tool
  composer:
    image: composer:1.4.2
    volumes:
    #Website directory
      - ./app:/app:rw
    working_dir: /app/webroot
    command: composer install

##Front-end tools
  bower:
    image: digitallyseamless/nodejs-bower-grunt
    volumes:
      - ./app:/app:rw
    working_dir: /app/webroot
    command: bower install

Step1, Single Server Deployment of jenkins and docker Choreographed Projects

Here we call the server command zushouyou

Server pre-requiring

  • 1. optimization
  • 2. Install git docker
  • 3.clone Project
git clone project..

Step2, start jenkins after cloning the project

docker-compose -f CI-server.yml up -d

If you find that the jenkins container is hanging, check the log, add permissions, and restart the container.

Step3, configure Jenkins

In CI-server.yml, we mapped port 8999 - > 8080, so we can access jenkins as long as we visit this port. If the service is rejected, please check the server vendor security group, iptables rules, firewalld and other firewalls.

Then we can visit the welcome screen.

Look at the initial password, where our container id is f8

docker exec -it f8 bash -c 'cat /var/jenkins_home/secrets/initialAdminPassword'
60e74c836b25402581cb0daae95b65a4

For the first time, plug-ins need to be installed. Here we choose to select some plug-ins manually, because we don't need many plug-ins if we just integrate.

Plug-in selection I don't take a screenshot to show that plug-ins in build tools and pipline can be cancelled first.
The required plug-ins are as follows

Configure user name and account after installation, and finally install some third-party plug-ins


System Management > Management Plug-ins
1.ssh, this plug-in facilitates execution of commands on remote SSH hosts

2.webhook, here we are gitlab's other equivalents

After the installation is completed, it's better to restart it to avoid problems.

Step4, Configure Job

System Settings Configure SSH Server and Git

Let the host pull through ssh first

1. generate rsa

2. Add to git website public key

3. If you can pull directly, it means OK.

4. Configure the server of jenkins

Configure SSH host

5. Go back to the home page and start your job.

  • 1.
  • 2.
  • 3.
  • 4. Execution Construction

Finally, just configure the notification.
Configure gitlab's webhook

$RUL/generic-webhook-trigger/invoke?token=fuck

This completes an automated integration process. Keep in mind that this article just gives us some simple principles, including build, test and finally ci. If we don't do the previous work, it doesn't make sense, just make an automated script.

Topics: PHP MySQL jenkins Docker