Getting Started with GoCD on Kubernetes

Step 3: Import sample pipeline to build and deploy a sample application

In this section, we’ll import a set of GoCD pipelines that build and deploy a sample application with a Docker based build workflow.

This section uses GoCDs pipelines as code capability to import sample pipeline definitions from an external Git repository.

Prerequisites

  1. The sample pipelines build a sample application as a Docker image artifact and publish it to Dockerhub. To do this, make sure you have a Docker Hub account.

  2. GoCD’s pipelines as code configurations allow for the scripting of pipeline definitions. These do not include global objects like artifact stores. Global objects need to be setup using the GoCD user interface or the API. This sample requires an artifact store configured so pipelines can publish and fetch Docker image artifacts to it.

    You can configure a new DockerHub artifact store with the Admin -> Artifact Stores menu.

    You can now configure the artifact store with your DockerHub credentials.

    Importing a sample workflow - 图1

    To setup Dockerhub credentials, select the registry type: Others (Dockerhub, GCR, private)

    The Docker registry URL for Dockerhub is https://index.docker.io/v1

    Enter your Dockerhub user credentials.

  3. Setup secrets

    For deployment pipelines that need access to the Kubernetes API for target deployment Kubernetes clusters, an API token must be provided and made available to deployment scripts. Deployment scripts also need other secrets such as Dockerhub credentials. We setup these secrets in Kubernetes and make them available to the GoCD agents in the elastic agent’s pod yaml configuration.

    Kubernetes API Tokens

    To allow a deployment script to be able to communicate with the target Kubernetes cluster, you must create a service account for deployments. The API token for this service account can then be stored in a Kubernetes secret.

    Refer the Kubernetes RBAC guide for instructions on creating service accounts and assigning them to roles.

    You can create a secret with these credentials with the following Kubernetes configuration:

    1. cat <<EOF >./secrets-for-gocd.yaml
    2. apiVersion: v1
    3. kind: Secret
    4. metadata:
    5. name: secrets-for-gocd
    6. type: Opaque
    7. data:
    8. K8S_API_TOKEN: <Base64 encoded Kubernetes API token>
    9. DOCKERHUB_USERNAME: <Base64 encoded Dockerhub user name>
    10. DOCKERHUB_ORG: <Base64 encoded Dockerhub organization>
    11. EOF

    Encoding note: The serialized JSON and YAML values of secret data are encoded as base64 strings. Newlines are not valid within these strings and must be omitted. The following pipeline will strip newlines before base64 encoding strings.

    1. echo -n "string" | base64

    Apply the secrets-for-gocd.yaml file to create a secret with these credentials

    1. kubectl apply -f secrets-for-gocd.yaml -n gocd
  4. Configure the elastic profile.

    The sample pipelines are configured to use GoCD Kubernetes elastic agents. Elastic agents are build agents that are provisioned on-demand for a job and terminated thereafter.

    Elastic agents use elastic profiles to provision these on-demand agents. A Kubernetes elastic profile includes information about the container image for the GoCD agents, and the pod configuration yaml.

    The GoCD Helm chart sets up an elastic profile after installation. To view this elastic profile configuration, navigate to Admin > Elastic Profiles.

    Importing a sample workflow - 图2

    The GoCD elastic agents need to be configured with secrets such as a Kubernetes API token to allow it to be able to perform deployments of applications to the cluster.

    We configure the secrets created in the previous step in the configuration of the elastic profile mentioned earlier. Replace the pod yaml with one below.

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: pod-name-prefix-{{ POD_POSTFIX }}
    5. labels:
    6. app: web
    7. spec:
    8. containers:
    9. - name: gocd-agent-container-{{ CONTAINER_POSTFIX }}
    10. image: gocddemo/gocd-agent-dind:webinar
    11. env:
    12. - name: KUBE_TOKEN
    13. valueFrom:
    14. secretKeyRef:
    15. name: secrets-for-gocd
    16. key: K8S_API_TOKEN
    17. - name: DOCKERHUB_USERNAME
    18. valueFrom:
    19. secretKeyRef:
    20. name: secrets-for-gocd
    21. key: DOCKERHUB_USERNAME
    22. - name: DOCKERHUB_ORG
    23. valueFrom:
    24. secretKeyRef:
    25. name: secrets-for-gocd
    26. key: DOCKERHUB_ORG
    27. - name: NAMESPACE
    28. valueFrom:
    29. fieldRef:
    30. fieldPath: metadata.namespace
    31. securityContext:
    32. privileged: true

Setup external pipeline configuration repository

GoCD pipelines can be defined in code in either YAML or JSON format. These pipeline definitions can be stored in a source code repository, either in your application’s repository or a separate repository.

The GoCD sample pipelines build and publish an image of a sample nodejs application called ‘Bulletin Board’. These pipeline configurations are available in the repository:

  1. https://github.com/gocd-demo/sample-k8s-workflow

You can add a new configuration repository with the Admin -> Config Repositories menu.

You can now configure the location of the repository(ies) to pick up pipeline definitions.

Note: The PluginID dropdown allows you to select either the JSON or YAML configuration plugin, based on the format of your pipeline configuration.

Importing a sample workflow - 图3

Imported sample pipelines

Once imported, the dashboard page should display the sample pipelines.

Now that the pipelines have been imported, we can run them and verify that our application is built and its Docker image is published to DockerHub.

Importing a sample workflow - 图4

To run the build_and_publish_image pipeline, unpause the pipelines in the GoCD dashboard.