GoCD with Docker

Quick Setup GoCD with Docker

Posted by boydfd on 2017-05-18 15:39:00 +0800

Recently by the same author:


喝水方案升级之路

You may find interesting:


Install Docker

Install Docker Automatically

Welfare

If you don’t want to know detail, you can download my git repository and everything is ready: portal

Situation

After Graduated for one year, I have participated 5 projects.In the early time of all projects, we have to setup CI/CD, as we already familiar with GoCD, so we use GoCD every time.

But, there is a pain point, for every project, we have to download GoCD on virtual machine(physics or cloud). Then start server and agent, most time, we should setup 3 or 4 agents to gain power of concurrency.So, we have to do the same things for 3 or 4 times.

Of course, we can depend on shell script to do this, But, one more nice way is using docker.

Basic Use

GoCD already offer official image for server and agent, so we can just use it.

Here is the basic docker-compose:

version: "3"
services:
  server:
    container_name: gocd-server
    ports:
      - 8153:8153
      - 8154:8154
    image: gocd/gocd-server:v17.4.0
  agent:
    image: gocd/gocd-agent-alpine-3.5:v17.4.0
    environment:
      - GO_SERVER_URL="https://gocd-server:8154/go"
    links:
      - server:gocd-server

Just link them, and set GO_SERVER_URL.

Then use docker-compose to start stack:

docker-compose up

Now, we get one server and one agent.access localhost:8153(you should be patient to wait server to start), and you can see our gocd is already running.

if you want to run more agent, show you the power of docker:

docker-compose scale agent=3

Now, we have one serve and 3 agent.You can now config GoCD as before.

More

SSH

Most time, we will make agent to ssh to other machine and run some script, so how can we config our agent to exempt from ssh password and first-time known host checking.

To do that, we should do two things:

  1. tell ssh don’t check known host, so we can mount a volume for agent on /home/go/.ssh/config so, we can change config of ssh outside the container.
  2. use file to ssh.

this is the docker-compose after doing 2 things above:

version: "3"
services:
  server:
    container_name: gocd-server
    ports:
      - 8153:8153
      - 8154:8154
    image: gocd/gocd-server:v17.4.0
  agent:
    image: gocd/gocd-agent-alpine-3.5:v17.4.0
    environment:
      - GO_SERVER_URL="https://gocd-server:8154/go"
    links:
      - server:gocd-server
    volumes:
      - ./certs:/home/go/certs
      - ./gocd-agent/config:/home/go/.ssh/config

In our config we can add something like this:

HOST $host
    StrictHostKeyChecking no
    UserKnownHostsFile=/dev/null

You have to replace $host to ip or domain.

And then generate a pair of ssh key, put public key to host, and put private key to ./certs file.

Then, when agent run task, you can use this private key to ssh:

ssh -i /home/go/certs/$key-name $user@$host

Persistent Stores

It’s very easy, the only thing you need to do is mount /godata directory on GoCD server.

version: "3"
services:
  server:
    container_name: gocd-server
    ports:
      - 8153:8153
      - 8154:8154
    image: gocd/gocd-server:v17.4.0
    volumes:
      - ./godata:/godata
  agent:
    image: gocd/gocd-agent-alpine-3.5:v17.4.0
    environment:
      - GO_SERVER_URL="https://gocd-server:8154/go"
    links:
      - server:gocd-server
    volumes:
      - ./certs:/home/go/certs
      - ./gocd-agent/config:/home/go/.ssh/config

more script?

visit this site: inertia script