[Docker] CI with Drone and Gogs

Now that we have a running Gogs installation for our source code (see here) - we can use Drone to build working software from those repos. Drone is an Continious Integration System, that comes in two flavors: Drone.io - which is the hosted service you could use - or Drone - as self-hosted service. We want to use the later one. To install drone in our existing Docker setup (with gogs already installed) we need to complete following steps:

# Create docker-compose.yml in ~/drone/
cd ~
mkdir drone
cd drone
vi docker-compose.yml
# Copy this content into your docker-compose.yml file

drone:
  restart: unless-stopped
  image: drone/drone:0.4.2
  volumes:
    - /var/drone:/var/lib/drone
    - /var/run/docker.sock:/var/run/docker.sock
  env_file:
    - ./dronerc
  ports:
    - "8000:8000"

# Create the dronerc file
vi dronerc
# Copy this content into your dronerc file
# replace yourserverip with the ip or dns name of your server, i.e. example.com
# replace the gogsport with the port of the http port of the gogs installation, i.e. was 3000 in our example

REMOTE_DRIVER=gogs
REMOTE_CONFIG=https://yourserverip:gogsport?open=false
DEBUG=true

# Create the needed folders
sudo mkdir /var/drone
sudo chown -R yourusername:yourusername /var/drone/

After that, an docker-compose up will start the service, you can end it via STRG + C and really start it in deattached mode with docker-compose up -d
You can then go to http://yourserverip:8000 and log in into drone with your gogs login and allow access.

The current readme for drone can be found on http://readme.drone.io/usage/overview/ and you'll need to include a .drone.yml in your repos to really build something.

In my example, I used this .drone.yml

cache:
  mount:
    - /drone/.m2

build:
  main:
    image: maven:3-jdk-8-onbuild
    commands:
      - mvn clean install -Pcoverage -Dmaven.repo.local=/drone/.m2
      - mvn package -Dmaven.repo.local=/drone/.m2
      - mvn test -Dmaven.repo.local=/drone/.m2
      - echo "Build has been completed."

debug: true

and this pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId> 
  <artifactId>WhereIsMyPi</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>
 
  <name>Where is my Pi</name>
  <url>http://www.nico-maas.de</url>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
       <!-- Build an executable JAR -->
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-jar-plugin</artifactId>
       <version>2.4</version>
       <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>WMP</mainClass>
          </manifest>
        </archive>
       </configuration>
      </plugin>
    </plugins>
  </build> 

</project>

to build my "WhereIsMyPi" project :).
Happy building!

[Docker] Install Gogs as Docker container

Gogs (Go Git Service) is an awesome Github/Gitlab like solution, completly written in Go(lang) - which makes it blazing fast - and lightweight.
And as there is even an Docker Container of Gogs available - I thought - why not using this to finally move from my SSH-only-Git to an "real" Gitservice :).

We are going to use docker-compose in this example - so I assume you have installed this as shown in my last guide on Docker.

# Create docker-compose.yml in ~/gogs/
cd ~
mkdir gogs
cd gogs
vi docker-compose.yml
# Copy this content into your docker-compose.yml file

gogs:
  restart: unless-stopped
  image: gogs/gogs
  volumes:
    - /var/gogs:/data
  ports:
    - "10022:22"
    - "3000:3000"

After that save the file and issue docker-compose up in your terminal. Docker will start to pull the gogs image from the hub and launching gogs. All your gogs files will be saved on your local drive in /var/gogs. You can find the overview of the file structure here.
After Docker is ready - launching your favorite browser and go to http://127.0.0.1:3000.

Now its time to configure gogs. Please bear in mind the important information of the Gogs Guide regarding Settings in a Docker installation.

Regarding this, we will use following settings:
As Database type, we choose SQLite3. As path, already data/gogs.db is choosen, which is important: A Docker Container is non-persistent - so if you restart that container, all files not saved in a mounted directory (like your /var/gogs, which is mounted as data in the docker session of gogs...) will be lost.
As name, we choose something catchy like PiGit or so - as you wish.
We won't touch the repo path (/data/git/gogs-repositories), nor the user (git). As Domain, we could choose our http://mydnshost.com.
As SSH Port, we choose 10022. HTTP Port remains as 3000 and the application domain should be something like http://mydnshost.com:3000 - while 3000 is the exposed HTTP port - so someone can actually access your Gogs service.

You can also configure your mail server - as you wish.

Regarding the server and additional settings, I did choose the Offline Mode, but enabled Gravatar, disabled User Registration and Captchas and I did enable "Need to be registred to see contents" - as I want my Gogs Server to be actually reachable from the net - but I only want to create user accounts by hand - and not have my server filed with stuff of foreign people.

The last step is to create an Admin User Account. I would recommend to do this "now". After that, click "Install Gogs". And then - you can login :)!

The cool thing about gogs: You can even migrate from other Git Services to your new Gogs Server via the "Migration" Tab (+ next to your User Icon after you have logged in). Please bear in mind, that only http/https and local paths do work for that.

If you create a new repo, you should always check the "Init the repo", so that you can directly clone and use it.

Regardings cloning: You can access your repo via the webpage (ip:3000) or ssh (ip:10022). To use ssh, you need to insert your public key in the "SSH Key" tab in your gogs settings.

If you expose the 3000 and 10022 ports via your Firewall/Router, you can access gogs from everywhere - or you just use VPN to get into your network.

Bonus: Making Gogs Secure with Letsencrypt
If you already have an Letsenrcypt certificate for your server / pc, you can easily get gogs to use that: Just go to /var/gogs/gogs/data and copy your fullchain.pem and privkey.pem from your letsencrypt folder ( /etc/letsencrypt/live/[yourdomain]/ ) and give your user access to it via chown.
After that, go to /var/gogs/gogs/conf open app.ini and add following settings under [server]:
PROTOCOL = https
CERT_FILE = data/fullchain.pem
KEY_FILE = data/privkey.pem

If there should be another entry like PROTOCOL = http, just delete it. Save that file, go back to your open docker terminal with gogs running, CTRL + C and enter docker-compose up -d. With that, it will restart in detached mode. And more important: Your service will automatically start on every reboot of your system.

If you ever would need to stop gogs, just go again to your docker-compose file, i.e. cd ~/gogs/ and enter docker-compose stop.
You can also watch what your docker container is doing with the command docker ps

Happy coding!