zk/work/gitea-6.md

87 lines
7.1 KiB
Markdown

# Gitea and GoCD
## Introduction
In the process of developing and releasing software, one of the most useful tools at a developer's disposal is a solution that performs **continuous integration** and **continuous deployment**, or **CI/CD**.
Continuous integration (CI) refers to the practice of multiple developers merging their changes to a codebase back into the main branch often, perhaps daily or even more frequently, relying on smaller chunks of work that are more easily reviewed to and tested during the process of development. In order to ensure that these changes don't leave the main branch in a broken state, a tool that helps with continuous integration will often include functionality that runs test suites within the software package. When a developer proposes a merge of their work back into the main branch, the tests will run automatically and report back whether or not they pass, which may mean that the code is not ready to land.
Continuous deployment or continuous delivery (CD) refers to the idea that changes to code --- whether they're security patches, bug fixes, or new features --- be released quickly and seamlessly to the end users of the application. In the case of applications that the user runs, this will mean a frequent release schedule, offering incremental improvements to the software they are using. In the case of a service such as a website, this will mean frequent deployments of the new features in the code to the site so that the user's experience is continuously improving.
There are several tools that can help with CI/CD. These run as their own web services, which work with many source code management (SCM) systems to perform these tasks automatically. [GoCD](https://gocd.org) is one such solution, that provides a way of executing **pipelines** --- sets of steps that the service will run through such as building the software and running tests --- that are described in the code itself. GoCD is a full-featured CI/CD tool with a flexible, declarative pipeline syntax that allows you to accomplish many tasks.
This tutorial aims to show how GoCD can integrate with the source code management tool Gitea in order to offer fully self-hosted solutions for SCM and CI/CD. You will be connecting GoCD with Gitea, and creating a sample project for GoCD to run tests on in order to see how the two services work together.
### Prerequisites
To complete this tutorial, you will need the following:
* An Ubuntu 20.04 server configured with a non-root sudo user and firewall by following the [Ubuntu 20.04 initial server setup guide](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04).
* A domain or subdomain pointed to your server that you will use to access the GoCD server. This must be separate from the domain that you use for your Gitea installation. If you are using a DigitalOcean Droplet, you can accomplish this by following our [Domains and DNS](https://docs.digitalocean.com/products/networking/dns/) documentation. This tutorial will use `<^>your_domain<^>` in examples throughout.
* An instance of Gitea installed on a separate server. For an example of how to install Gitea, you can follow the [Now To Install Gitea on Ubuntu Using Docker](https://www.digitalocean.com/community/tutorials/how-to-install-gitea-on-ubuntu-using-docker#creating-a-test-repository) tutorial. For this tutorial, you will need the following:
* An Ubuntu 20.04 server configured with a non-root sudo user and firewall by following the [Ubuntu 20.04 initial server setup guide](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04).
* Docker installed. You can find out how to do this by following **Steps 1 and 2** of the [How to Install and Use Docker on Ubuntu 20.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04) tutuorial.
* Docker Compose installed. You can find out how to do this by following **Step 1** of the [How to Install and Use Docker Compose on 20.04](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-20-04#step-1-installing-docker-compose) tutorial.
*
Once you have these steps complete, you can continue installing GoCD on your new server.
## Step 1 — Installing GoCD
There are several ways to install GoCD. As a Java binary, it is fine to run on its own, but this makes keeping the service up to date more difficult. You can also install it in a Docker container, which is a good way to keep the server containerized if you are running multiple applications on a single server.
For the purposes of this tutorial, however, we will be installing the service via the APT repository. This allows easier updates and lets the service run without the overhead of a Docker container.
### Installing the GoCD server
To begin, add the APT source to the sources list:
```command
echo "deb https://download.gocd.org /" | sudo tee /etc/apt/sources.list.d/gocd.list
curl https://download.gocd.org/GOCD-GPG-KEY.asc | sudo apt-key add -
sudo apt-get update
```
These commands will first add the repository like to a new file in `/etc/apt/sources.list.d/gocd.list`, then fetch the GoCD PGP key in order to verify the install. Finally, it will update the list of cached package information.
From here, install the GoCD server using APT:
```command
sudo apt install go-server
```
### Installing the GoCD Agent
Every instance of GoCD needs at least one **agent**. Agents are the mechanism by which the server runs pipelines. They spin up an environment in which to execute the commands specified within the pipeline, and once they are finished running those commands, they clean up the environment.
As before, there are several different ways to install agents, each with their own benefits and drawbacks. For instance, installing with Docker will allow multiple agents to be running at a time and is suitable for multiple agents running at the same time. This may be preferable if you foresee running multiple pipelines concurrently. As before, you can also run agents through a Java binary downloaded from the GoCD website.
Once again, you will be using an agent installed from the APT repository that you set up earlier in this step.
```command
sudo apt install go-agent
```
This will install the GoCD agent on the server and allow you to run at least one pipeline at a time.
### Setting Up Your Domain Name and HTTPS
In order to view the GoCD web interface, you will need a domain name pointed at your server as mentioned in the prerequisites. Once you have that DNS record properly in place, you will need to open the firewall ports to allow HTTP(S) connections:
```command
sudo ufw allow
edit a configuration file that the GoCD agent has installed in order to listen for requests on the proper address and ports.
Open the configuration file for editing. The following example uses `nano`. This file will contain environment variables that `go-server` relies on for its operation:
```command
nano /usr/share/go-server/wrapper-config/wrapper-properties.conf
```
## Step 2 — Hooking GoCD Up to Gitea
## Step 3 — Setting Up a Pipeline on GoCD
## Conclusion