Docker+jenkins+golang Continuous Integration Practice [reprint Self's blog]

Source: Internet
Author: User
Tags jenkins docker
This is a creation in Article, where the information may have evolved or changed.

Blog Address: yeqown.github.com

Cause

Because production needs have recently re-toss Jenkins and Docker. The main purpose is to automatically compile, package, and deploy some Golang httpserver. So decided to use Jenkins to do this continuous integration carrier, choose Jenkins for two reasons:

1. Used before, get started faster
2. Community more mature, plug-in and document Rich

Installing Docker and pull Jenkins mirrors

This step is omitted as a precondition and is not the main step to be described in this article. There are also a lot of references on the Internet ~

Jenkins & Docker-compose Configuration

For the sake of convenience I only use docker-compose this tool, Docker-compose Foundation can see my docker-compose to get started. Here directly on the configuration:

version: '2'services:  jenkins:    container_name: jenkins-lts    ports:      - 9001:8080      - 50000:50000    image: jenkins/jenkins:lts    volumes:      - /home/worker/jenkins/jenkins_home:/var/jenkins_home

Configuration is also an official sample configuration.

Note: Mount the host's/home/worker/jenkins/jenkins_home as the container's/var/jenkins_home directory. The purpose of this is to avoid the loss of Jenkins data if the container is accidentally deleted.

Here, we just need to execute the docker-compose up -d Jenkins container to run, and then configure the Nginx, you can directly access to the Jenkins page, and initialize.

My directory structure is as follows:

➜  jenkins lltotal 8.0K-rw-rw-r--  1 worker worker  220 May  2 17:19 docker-compose.ymldrwxrwxr-x 19 worker worker 4.0K May  3 15:53 jenkins_home➜  jenkins pwd/home/worker/jenkins➜  jenkins docker-compose up -d # 运行

Publish over SSH configuration

Publish Over SSHConfiguration, because we are Jenkins running through Docker, we want to specifically configure SSH to facilitate the Jenkins deployment project. Here are the steps listed first:

    1. InstallationPublish Over SSH
    2. In-Container Ssh-keygen
    3. ConfigurationPublish Over SSH
    4. Configuring the GIT Repository Deployment public key #这一步相当于拉取代码的Credentials

Golang build-env

Because the default Jenkins image does not have the Go compiler tool, it is necessary to install a Go plugin Go-plugin-jenkins
The steps are as follows:

    1. Installing the Jenkins Go plugin
    2. In the Global tool configuration, install the Go
    3. In the corresponding task configuration, build environment item, select Go version

Detailed steps in this step can be found in the first section of the reference documentSetup Go Build Environment

Note: The official documentation says that global configuration go is done in the system setup, but I use Jenkins ver. 2.107.2 this configuration in the Global tool configuration.

How to package a deployment

Write a makefile to package the project and scp distribute the deployment service, which is also Publish Over SSH the purpose of the configuration.

because the Jenkins image does not have a make-related tool (or even vim), you need to install make and its associated tools. At the same time, if the project uses the relevant dependency management tools, it is also necessary to configure the relevant go environment variables. Where go installs the path:
The/var/jenkins_home/tools/org.jenkinsci.plugins.golang.golanginstallation/$GOVERSION.

Put my makefile in here.

# to test, build, deploy offline-tasks#-: Ignore the Commnad error# @: No display current COMMNAD to STD output# Com Mnads declaregocmd=gogotest=$ (gocmd) testgobuild=$ (gocmd) build# Params definemain_path=. /mainpackage_path=. /packagepackage_bin_path=. /package/binbin=offline-tasksfilename=offline-tasks.tar.gz# Deploy paramsdev_host=zy-devdev_tar_path=/home/ Worker/project/offline-tasksprod_host=zy-pro2prod_tar_path=/home/worker/project/offline-tasksdefault:build Packtest: # Testing-$ (gotest). /...-vbuild: # building MkDir $ (package_path) mkdir $ (package_bin_path) CD $ (Main_path) && $ (gobuild)-O $ (B IN) MV "$ (main_path)/$ (BIN)" $ (Package_bin_path) cp-r ". /configs "$ (package_path) CP". /sh/start.sh "$ (Package_bin_path) Pack: # Packing CD $ (package_path) && TAR-ZCVF. /$ (FILENAME)./* mv.                                                /$ (FILENAME) $ (package_path) ################################################### # # Deploy:from Zy-dEV to execute # deploy-dev:from Dev-ci to execute # # Deploy-prod:from Prod-ci to execute # # ################################################## #deploy: Clean Build PAC K # Deploy Dev from dev CP $ (package_path)/$ (FILENAME) $ (dev_tar_path) CD $ (dev_tar_path) && TAR zxvf $ (filenam E) && supervisorctl-c configs/dev.supervisord.conf Restart Offline-tasksdeploy-dev:clean Build Pack # Deploy-de  V from CI SCP $ (package_path)/$ (FILENAME) $ (dev_host): $ (dev_tar_path) SSH $ (dev_host) "CD $ (dev_tar_path) && TAR ZXVF $ (FILENAME) && supervisorctl-c configs/dev.supervisord.conf restart Offline-tasks "Deploy-prod:clean Build Pack # Deploying prod from Dev or CI SCP $ (package_path)/$ (FILENAME) $ (prod_host): $ (prod_tar_path) SSH $ (prod_hos  T) "CD $ (prod_tar_path) && TAR zxvf $ (FILENAME) && supervisorctl-c configs/prod.supervisord.conf Restart Offline-tasks "Clean: # CleaniNg Rm-fr $ (package_path) rm-fr.. /$ (FILENAME)

Summarize

After a series of operations above, there is only one more awkward question: If the Go Code warehouse vendor does not have a dependency on the project, then get the dependent action will be manually to operate the ~. Perhaps you can add a deps to the makefile, as follows:

# default set $CURDIR="$PROJ_ROOT/sh"# preparing works...GVT_RESTORE=gvt restorePROJ_ROOT=../deps:  cd ($PROJ_ROOT) && $(GVT_RESTORE)build: deps  # building  mkdir $(PACKAGE_PATH)  mkdir $(PACKAGE_BIN_PATH)  cd $(MAIN_PATH) && $(GOBUILD) -o $(BIN)   mv "$(MAIN_PATH)/$(BIN)" $(PACKAGE_BIN_PATH)  cp -r "../configs" $(PACKAGE_PATH)  cp "../sh/start.sh" $(PACKAGE_BIN_PATH)# other commands...

and add the Deps command, add the build command, and check dependencies every time you pack.

Resources

    • https://zpjiang.me/2017/08/09/Setup-Jenkins-for-Go-Projects/
    • Https://wiki.jenkins.io/display/JENKINS/Go+Plugin
Related Article

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.