Use Gogs,drone to build automated deployments

Source: Internet
Author: User

Use Gogs,drone to build an automated deployment using Gogs,drone,docker to build an automated test environment

Gogs is a self-help git service developed using the Go language, supporting all platforms
Docker is an open-source container engine using GO development
Drone is a continuous integration platform based on container technology. Each build is executed in a temporary docker container that gives developers complete control over their build environment and guarantees isolation. The drone is easy to install and use, with the goal of replacing Jenkins

The function of this article is to automatically update the binaries of your test environment and restart them when you push the code to Gogs, to implement automatic deployment (take the Go Development API service as an example, the test environment is Ubuntu)

The entire process is:

    1. Push code
    2. Drone build temporary containers pull up the latest code compilation, compile binary files from the SCP copy in the staging container to the test server, and then control the test Environment application service restart via SSH (SUPERVISORCTL)
Steps (Ubuntu)

Gogs and Docker are installed by default, and application services are deployed using supervisor (optional for other deployment methods)

    1. Installing Docker
      Specific installation steps visible on official website
    2. Installing Gogs
      Official website Installation document (need FQ, also can search the related installation document by oneself)
    3. Installing drone (v0.5)
      Install via Docker
      1. Download drone image

        docker pull drone/drone:0.5 
      2. Start drone server

         docker run -d     -e DRONE_GOGS=true     -e DRONE_GOGS_URL=http://127.0.0.1:3000 \ //gogs服务器地址    -e DRONE_SECRET=...     -e DRONE_OPEN=true     -v /var/lib/drone:/var/lib/drone -p 80:8000 --restart=always --name=drone drone/drone:0.5
        This command starts a sqlite as a storage database, optional mysql,postgres can be configured according to their own situation, see the documentation
        Drone start successfully, can be accessed through the Web, using gogs account login, find project open management.
      3. Start DRONE agent

         Docker run-d-e drone_server=ws://172.17.0.1/ws/broker \//server address-e drone_secret= ... \ //is consistent with server-v /var/run/docker.sock:/var/run/docker.sock--restart=always--name=drone-agent drone/drone:0.5 agent          
    4. Generate a custom Golang image (Configure the image as a build image in the. DRONE.YML configuration)
      1. Pull a base mirror
        Docker Pull Goang:latest selectable version
      2. Custom Mirroring
        1. Create and start the Golang container
          Docker Run--ti Golang:latest/bin/bash
        2. Generate SSH public key and set SSH password-free login test server
          inside the container :
          • Execute ssh-keygen-t RSA
          • Generates ID_RSA and id_rsa.pub in the $HOME/.SSH directory
          • Copy the id_rsa.pub to the test server via SCP
            Test Server :
          • Create the. SSH folder in the home directory
          • and Cat id_rsa.pub >>. Ssh/authorized_keys
          • chmod. Ssh/authorized_keys
            SSH Encryption login is configured
          Download the dependent package that you need for your project go get ... (The Gopath of the official Golang Mirror is/go)
          When ready, exit the container and save the changes in the container as a new image
          such as: Docker commit [container ID] Golang:dev
    5. Add a. drone.yml file to the project root path
      Example configuration:

      Workspace    Base:/root/go Path:src/projectnamePipeline Build Image:golang:dev//Specify build image Environment: -Gopath=/go:/root/go -Ssh_args=-p22-o stricthostkeychecking=No (you don't need to enter the first time you log onYes -Scp_args=-p22-o stricthostkeychecking=No -Build_name=buildname -App_name=appname -[Email protected]172.17.0.1 -Run_path=/data/go/project (Configure your own test Environment app save run address) Commands -Go build-o $BUILD _name -Eval $ (ssh-agent-s) -ssh-add/root/.ssh/id_rsa -scp $SCP _args  "$BUILD _ NAME "" $TEST _server ": " $RUN _path " / "$BUILD _name" _ "$ (date ' +%y%m%d ')" _ "$ (git rev-parse head| cut-c1-10)"//Copy file  -ssh $SSH _args  "$TEST _server"  "ln-s-F $RUN _path/$BUILD _name\_$ (date ' +%y%m%d ') _$ (git rev-parse head| cut-c1-10) $RUN _path/$ Build_name && supervisorctl Restart $APP _name "//restart with soft connection for backup       
        • Workspace: The working path, as configured above, will clone your project to/root/go/projectname, and $pwd=/root/go/projectname
        • Image: Specify the build image
        • Environment: Build environment variables for temporary containers, equivalent to Docker run-e .....
        • Commands: Commands executed on the shell inside the container

      The above configuration file is set to two Gopath because in the previous test, if I set the
      Base/root
      Gopath=/go
      will be the previous image download the dependency package to empty, do not know where to match the wrong or what reason, temporarily only to find a way to set up another gopath to solve

      Write well. Drone.yml after adding the warehouse push code will automatically build the deployed

Use Gogs,drone to build automated deployments

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.