Simple automated build environment

Posted by hagman on Mon, 28 Feb 2022 08:53:02 +0100

previously on

At the end of 2021, idle teachers began to prepare for the service of enterprise projects and built a basic cluster.
Of course, the cluster should be used when it is built. This article introduces how to use open source software to build a set of automatically built project environment.
With this environment, project development will be easier to standardize and indirectly improve project quality.

Note: if you want to know how to build a cluster, you can read the last blog post of the idle teacher.

Construction process

1. Build Gitea code warehouse

Similar to GitHub, Bitbucket and GitLab, Gitea is a lightweight open source code hosting solution written in Go language. It is forked from the Gogs project. Its goal is to provide the simplest, fastest and easiest way to build its own Git warehouse service. It supports all platforms and architectures supported by Go language, including Linux, macOS and Windows on amd64, i386, ARM, PowerPC and other architectures.

  • Open the container address, select the management node, enter the storage volume management, and create a new global volume: gitea and mysql
  • Open the port address, select the management node, enter stack management, and create a new stack as needed: gitea
version: "3.7"
networks:
  app:
    external: true
volumes:
  gitea:
    external: true
  mysql:
    external: true
services:
  server:
    image: gitea/gitea:1.16.0-rootless
    environment:
      - GITEA__database__DB_TYPE=mysql
      - GITEA__database__HOST=mysql:3306
      - GITEA__database__NAME=gitea
      - GITEA__database__USER=gitea
      - GITEA__database__PASSWD=gitea
    restart: always
    user: 1001
    networks:
      - app
    volumes:
      - gitea:/var/lib/gitea
    depends_on:
      - mysql
  mysql:
    image: mysql:8
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=gitea
      - MYSQL_USER=gitea
      - MYSQL_PASSWORD=gitea
      - MYSQL_DATABASE=gitea
    networks:
      - app
    volumes:
      - mysql:/var/lib/mysql
  • Open the konga management interface (set as konga.dev.abc.com in the previous article)
  • New service: Name:Git warehouse service, Url:http://gitea_server:3000
  • Enter the service and create a new route: Name:Git warehouse service, hosts: gitea dev.abc. com

Note: if necessary, add Config in the portal and map it to / etc / gitea / APP in gitea Ini or use environment variables for configuration overrides

Portal: install,to configure

2. Build a Registry image warehouse

Registry is a stateless, highly scalable server-side application that stores and allows you to distribute Docker images. Registry is open source under the loose Apache license.

  • Open the port address, select the management node, enter the storage volume management, and create a new global volume: registry
  • Open the port address, select the management node, enter stack management, and create a new stack as needed: registry
version: "3.7"
networks:
  app:
    external: true
volumes:
  registry:
    external: true
services:
  server:
    image: registry
    restart: always
    networks:
      - app
    ports:
      - 5000:5000 # Internal use without opening
    volumes:
      - registry:/var/lib/registry
  • Open the konga management interface (set as konga.dev.abc.com in the previous article)
  • New service: Name:Docker warehouse service, Url:http://registry_server:5000
  • Enter the service and create a new route: Name:Docker warehouse service, hosts: Registry dev.abc. com

Note: if it is only used as internal CI temporary storage, it is not necessary to expose the Port port or deploy it in Konga

Portal: Official documents

3. Install DroneCI and associate it with Git warehouse

Drone is a Docker based self-service continuous integration platform tool that enables busy teams to automate their build, test and release workflows.

  • Go to Gitea personal background > Application > manage OAuth2 Application > enter name and URI > Save > copy Key and Secret
    • Application connection: http://git.dev.abc.com/user/settings/applications >
    • Application Name: drone
    • Redirect URI: http://drone.dev.abc.com/login
  • To generate a local random Secret, you can use the command openssl rand -hex 16
  • Open the portal address, select the management node, enter the storage volume management, and create a new global volume: drone
  • Open the port address, select the management node, enter stack management, and create a new stack as needed: drop
version: '3.7'
volumes:
  drone:
    external: true
networks:
  app:
    external: true
services:
  server:
    image: drone/drone:2
    restart: always
    networks:
      - app
    volumes:
      - drone:/data
    environment:
      - DRONE_GITEA_CLIENT_ID=stay Gitea Generated in Key
      - DRONE_GITEA_CLIENT_SECRET=stay Gitea Generated in Secret
      - DRONE_GITEA_SERVER=http://gitea.dev.abc.com
      - DRONE_GIT_ALWAYS_AUTH=true
      - DRONE_RPC_SECRET=Local random generated in the previous step Secret
      - DRONE_SERVER_HOST=drone.dev.abc.com
      - DRONE_SERVER_PROTO=https
      - DRONE_USER_CREATE=username:administrators Gitea user name,admin:true #Set a Gitea user as Administrator
  agent:
    image: drone/drone-runner-docker:1
    restart: always
    depends_on:
      - server
    networks:
      - app
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - DRONE_RPC_PROTO=http
      - DRONE_RPC_HOST=server
      - DRONE_RPC_SECRET=Local random generated in the previous step Secret
  • Open the konga management interface (set as konga.dev.abc.com in the previous article)
  • New service: Name:Drone auto build, Url:http://drone_server:5000
  • Enter the service, create a new route: Name:Drone, auto build, hosts: drone dev.abc. com
  • Open the drone address, log in with the administrator account, select the project to be built automatically, enter Setting and start the automatic construction

Note: the difference between the drone administrator account and the ordinary account is that it can open more permissions for the project and allow it to call the host file. Docker if needed Sock permission, the Trusted item must be opened

Portal: Official documents

4. Let the code deploy automatically

Note: a React project is packaged in this example

  • Open the port address, select the management node, enter stack management, and create a new stack as needed: docker react app
version: '3.7'
networks:  
  app:
    external: true
services:
  dev:
     image: docker-react-app:latest
     networks:  
       - app
  prod:
     image: docker-react-app:release
     networks:  
       - app
  • Add Dockerfile file
FROM nginx:1.21.1-alpine
COPY ./build /usr/share/nginx/html
  • Join drone yml
---
kind: pipeline
name: devlop
clone:
  depth: 1 #Clone depth 1, speed up cloning
trigger:
  branch: master
  event: push
volumes: # Trusted permission is required to use this item
  - name: docker
    host:
      path: /var/run/docker.sock
steps:
  - name: install
    pull: if-not-exists
    image: node:16.6.0-alpine
    commands:
      - yarn config set registry https://registry.npm.taobao.org
      - yarn install
      - yarn build
    environment:
      NODE_ENV: production
  - name: build
    pull: if-not-exists
    image: docker:dind
    privileged: true
    volumes:
      - name: docker
        path: /var/run/docker.sock
    commands:
      - "echo '${DRONE_COMMIT}' > build/version.html" #Enter a static configuration file into the project
      - "docker build -t docker-react-app:${DRONE_COMMIT} -f Dockerfile ." #Build project image
      - "docker tag docker-react-app:${DRONE_COMMIT} docker-react-app:latest" #Label the image latest
      - "docker service update --image docker-react-app:${DRONE_COMMIT} docker-react-app_dev" # Restart the service with the new image
---
kind: pipeline
name: prodution

clone:
  depth: 1
trigger:
  event: tag # Run only when labeling
volumes:
  - name: docker
    host:
      path: /var/run/docker.sock
steps:
  - name: install
    pull: if-not-exists
    image: node:16.6.0-alpine
    commands:
      - yarn config set registry https://registry.npm.taobao.org
      - yarn install
      - yarn build
    environment:
      NODE_ENV: production
      REACT_APP_TARGET: production
  - name: build
    pull: if-not-exists
    image: docker:dind
    privileged: true
    volumes:
      - name: docker
        path: /var/run/docker.sock
    commands:
      - "echo '${DRONE_TAG}' > build/version.html"
      - "docker build -t docker-react-app:${DRONE_TAG} -f Dockerfile ." # Use the TAG number as the mirror version number
      - "docker tag docker-react-app:${DRONE_TAG} docker-react-app:release"  # Update the release label to the latest official version
      - "docker service update --image docker-react-app:${DRONE_TAG} docker-react-app_prod"
  • Submitting through GIT will automatically trigger the build process and restart the project service
  • Open the Drone address to view the automatic build progress
  • Open the konga management interface (set as konga.dev.abc.com in the previous article)
  • New service: Name:DockerReactApp,Url:http://docker-react-app_dev
  • Enter the service and create a new route: Name: dockerreactapp, hosts: docker react app dev.abc. com
  • Open the project routing address in the previous step to view the automatic deployment results

Deployment complete

Well, we have a cluster built automatically by CI.
All business contents can be tested and deployed through services.
The knowledge of architecture is extensive and profound. If you have any other questions, you are welcome to join the QQ group of idle teachers to learn from each other: 1033245535

Topics: Docker architecture Container microservice