CI sample setup with GO - 24i/fokuson-developer-portal GitHub Wiki

Setup an environment for continuous integration and have your widgets build and tested on a per push, by cron or on demand basis. Create pipelines for snapshots, milestones, release candidates or releases.

Prerequisites

Dedicated server with Internet access and docker engine installed

Setup GOCD environment

  1. Create gocd home directory on server

    mkdir GOCD; cd GOCD; mkdir go godata; chmod 777 ../GOCD go godata
    
  2. Start GO server

    docker run -d -p8153:8153 gocd/gocd-server:v20.5.0
    

    Point browser to: http://<your_server>:8153/go

  3. Start GO agent

    GO_SERVER_URL=http://<your_server>:8153/go gocd/gocd-agent-ubuntu-16.04:v20.5.0
    

    NB! is where the GOCD directory was created in step 1 and “<your_server>” refers to the docker server, not container.

Configure SSH connection to BitBucket

Repeat the following steps for both server and agent container:

Screen Shot 2020-07-09 at 11.30.14.png

Login to container

#!

# docker exec -it -u 0 <container ID> /bin/bash

Set up an SSH key

  1. Change to user “go”
#!

# su - go
  1. Create key and add to the ssh agent
#!

$ ssh-keygen
#!

eval `ssh-agent`
#!

ssh-add ~/.ssh/id_rsa 

If you renamed the key, replace id_rsa with the key file name.

Add the public key to your Account settings

  1. From Bitbucket, choose Personal settings from your avatar in the lower left. The Account settings page opens.

  2. Click SSH keys. If you've already added keys, you'll see them on this page.

  3. In your terminal window, copy all of the contents of your public key file. If you renamed the key, replace id_rsa.pub with the public key file name.

$ cat ~/.ssh/id_rsa.pub
  1. Select and copy the key output in the clipboard. If you have problems with copy and paste, you can open the file directly with Notepad. Select the contents of the file (just avoid selecting the end-of-file characters).

  2. From Bitbucket, click Add key.

  3. Enter a Label for your new key. e.g.: docker_gocd_agent, docker_gocd_server These labels are only for you for referencing, and you can name them however you choose.

  4. Paste the copied public key into the SSH Key field. You may see an email address on the last line when you paste. It doesn't matter whether or not you include the email address in the Key.

  5. Click Save. After you add a key, you can edit the key's Label but not the key itself. To change the key's contents, you need to delete and re-add the key.

  6. Return to the terminal window and verify your configuration and username by entering the following command:

#!

$ ssh -T [email protected]

You should see something similar to a successful connection. The resulting message tells you which of your Bitbucket accounts can log in with that key.

#!
$ ssh -T [email protected]
The authenticity of host 'bitbucket.org (18.205.93.0)' can't be established.
RSA key fingerprint is SHA256:azXQOXSRBEiUtuE1AikJYKwbHaxvSc0ojez4YXaGp1A.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'bitbucket.org,18.205.93.0' (RSA) to the list of known hosts.
logged in as aronM

You can use git or hg to connect to Bitbucket. Shell access is disabled

If you get an error message with

#!
Permission denied (publickey)

, check the [Troubleshoot SSH issues](Troubleshoot SSH issues) page for help.

add_ssh_key.png

After you've got a SSH key set up, use the SSH URL the next time you clone a repository. If you already have a repository that you cloned over HTTPS, change the remote URL for your repository to use its SSH URL.

Ensure SSH has been setup for both containers before continuing.

Configure GOCD agent

The next step is to configure the GOCD agent, so the one listed with the image name “gocd/gocd-agent” when listing Docker images with docker ps

Login

#!

# docker exec -it -u 0 <container ID> /bin/bash

Install Node.js and npm from NodeSource repository (as root)

First, update the local repository to ensure you install the latest versions of Node.js and npm. Execute the following command:

#!

# apt-get -y update
#!

# apt-get -y upgrade

Next, add the NodeSource repository to the system with:

#!

# curl -sL https://deb.nodesource.com/setup_12.x | bash -

(or change URL to latest stable as of https://nodesource.com/)

The output will prompt you to use the following command if you want to install Node.js and npm:

#!

# apt-get install -y nodejs gcc g++ make

Verify the installed software with the commands:

#!

# node --version

# npm --version

You should have an output similar to this:

#!

root@7da19e4a4cdd:/# node --version
v12.18.2
root@7da19e4a4cdd:/# npm --version
6.14.5

Configure GO server

Connect to server

  1. Point your browser to:

    http://<your_server>:8153/go

Enable agent and add environment

  1. Enable the agent (Agents > Static > Enable) b375c5f6-1d84-4a7e-9b6d-5a96ae6cdcbf.png

  2. Add Environment “build_widgets” 7a83a9b2-9e72-4b8e-8d6e-0fca9b6ff5f1.png a0720794-63e5-4ba4-b870-eb50052b9437.png

  3. Add the agent to the environment 3a14d8cf-952d-4ba8-bdf7-a9a8370b675e.png

Add a pipeline

Edit the Config XML

2d8c67f9-5711-4e5e-b016-cbfaf08cfb74.png Insert snippet between and tags. Replace “acme” with your name.

The Git URL ([email protected]:nordija/widgets-acme.git) needs to be correct.

Also, the branch="bugfix_3_27" needs to be the right branch of the repository

#!


  <pipelines group="Acme">
    <pipeline name="acme_widgets_bugfix_3_27">
      <materials>
        <git url="[email protected]:nordija/widgets-acme.git" branch="bugfix_3_27" dest="work" materialName="acme_widgets" />
      </materials>
      <stage name="build_package" cleanWorkingDir="true">
        <jobs>
          <job name="build_widgets">
            <tasks>
              <exec command="npm" workingdir="work">
                <arg>install</arg>
                <runif status="passed" />
              </exec>
              <exec command="npm" workingdir="work">
                <arg>run</arg>
                <arg>build</arg>
                <runif status="passed" />
              </exec>
            </tasks>
            <artifacts>
              <artifact type="build" src="work/dist/*.zip" dest="pkg" />
              <artifact type="build" src="work/dist/zips/**/*.zip" dest="pkg" />
            </artifacts>
          </job>
        </jobs>
      </stage>
    </pipeline>
  </pipelines>

GOCD Config XML snippet

Map pipeline to environment

2d428f51-b7dd-4646-a4ef-870e104f2dca.png

Run pipeline

999cd7b8-bad8-4a38-afbe-c8e2c415d9dc.png

Download artifacts

a6830579-de2a-4099-bc64-960bcb53206d.png

Related articles

GOCD Documentation https://docs.gocd.org/current/

SSH Keys https://bitbucket.org/account/settings/ssh-keys/

GOCD server https://hub.docker.com/r/gocd/gocd-server

GOCD Agent https://hub.docker.com/r/gocd/gocd-agent-ubuntu-16.04

⚠️ **GitHub.com Fallback** ⚠️