Skip to content

Commit

Permalink
update go language-specific guide (#18562)
Browse files Browse the repository at this point in the history
* update deploy and ci-cd in go guide

Signed-off-by: Craig Osterhout <[email protected]>
  • Loading branch information
craig-osterhout authored Nov 2, 2023
1 parent e447913 commit c8f0c28
Show file tree
Hide file tree
Showing 4 changed files with 360 additions and 20 deletions.
133 changes: 121 additions & 12 deletions content/language/golang/configure-ci-cd.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,131 @@
---
title: Configure CI/CD for your Go application
keywords: ci, cd, ci/cd, continuous integration, continuous deployment, deployment,
github, github actions, go, golang, development
description: Learn how to set up CI/CD pipeline for your application.
aliases:
- /get-started/golang/configure-ci-cd/
keywords: go, CI/CD, local, development
description: Learn how to Configure CI/CD for your Go application
---

## Get started with GitHub Actions
## Prerequisites

{{< include "gha-tutorial.md" >}}
Complete the previous sections of this guide, starting with [Build your Go image](build-images.md). You must have a [GitHub](https://github.com/signup) account and a [Docker](https://hub.docker.com/signup) account to complete this section.

## Next steps
## Overview

In this section, you'll learn how to set up and use GitHub Actions to build and push your Docker image to Docker Hub. You will complete the following steps:

1. Create a new repository on GitHub.
2. Define the GitHub Actions workflow.
3. Run the workflow.

## Step one: Create the repository

Create a GitHub repository, configure the Docker Hub secrets, and push your source code.

1. [Create a new repository](https://github.com/new) on GitHub.

2. Open the repository **Settings**, and go to **Secrets and variables** >
**Actions**.

3. Create a new secret named `DOCKER_USERNAME` and your Docker ID as value.

4. Create a new [Personal Access Token (PAT)](../../security/for-developers/access-tokens.md/#create-an-access-token) for Docker Hub. You can name this token `docker-tutorial`.

5. Add the PAT as a second secret in your GitHub repository, with the name
`DOCKERHUB_TOKEN`.

6. In your local repository on your machine, run the following command to change
the origin to the repository you just created. Make sure you change
`your-username` to your GitHub username and `your-repository` to the name of
the repository you created.

```console
$ git remote set-url origin https://github.com/your-username/your-repository.git
```

7. Run the following commands to stage, commit, and push your local repository to GitHub.

```console
$ git add -A
$ git commit -m "my commit"
$ git push -u origin main
```

## Step two: Set up the workflow

Set up your GitHub Actions workflow for building, testing, and pushing the image
to Docker Hub.

1. Go to your repository on GitHub and then select the **Actions** tab.

In this module, you have learned how to set up GitHub Actions workflow to an existing dockerized Go project, optimize your workflow to improve build times, and reduce the number of pull requests. Finally, you learned how to push only specific versions to Docker Hub.
2. Select **set up a workflow yourself**.

You can also consider deploying your application to a public Cloud provider, such as Azure and AWS or to an orchestration platform such as Kubernetes.
This takes you to a page for creating a new GitHub actions workflow file in
your repository, under `.github/workflows/main.yml` by default.

3. In the editor window, copy and paste the following YAML configuration.

```yaml
name: ci

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest
```
For more information about the YAML syntax used here, see [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions).
## Step three: Run the workflow
Save the workflow file and run the job.
1. Select **Commit changes...** and push the changes to the `main` branch.

After pushing the commit, the workflow starts automatically.

2. Go to the **Actions** tab. It displays the workflow.

Selecting the workflow shows you the breakdown of all the steps.

3. When the workflow is complete, go to your
[repositories on Docker Hub](https://hub.docker.com/repositories).

If you see the new repository in that list, it means the GitHub Actions
successfully pushed the image to Docker Hub.

## Summary

In this section, you learned how to set up a GitHub Actions workflow for your application.

Related information:
- [Introduction to GitHub Actions](../../build/ci/github-actions/index.md)
- [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions)

## Next steps

In the next module, you’ll look into some options for doing so.
Next, learn how you can locally test and debug your workloads on Kubernetes before deploying.

{{< button text="Deploy your app" url="deploy.md" >}}
{{< button text="Test your deployment" url="./deploy.md" >}}
239 changes: 235 additions & 4 deletions content/language/golang/deploy.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,238 @@
---
title: Deploy your Go app
keywords: deploy, ACI, ECS, local, development, Go, Golang, cloud, deployment
description: Learn how to deploy your application
title: Test your Go deployment
keywords: deploy, go, local, development
description: Learn how to deploy your Go application
---

{{< include "deploy.md" >}}
## Prerequisites

- Complete all the previous sections of this guide, starting with [Build
your Go image](build-images.md).
- [Turn on Kubernetes](/desktop/kubernetes/#turn-on-kubernetes) in Docker
Desktop.

## Overview

In this section, you'll learn how to use Docker Desktop to deploy your
application to a fully-featured Kubernetes environment on your development
machine. This allows you to test and debug your workloads on Kubernetes locally
before deploying.

## Create a Kubernetes YAML file

In your project directory, create a file named
`docker-go-kubernetes.yaml`. Open the file in an IDE or text editor and add
the following contents. Replace `DOCKER_USERNAME/REPO_NAME` with your Docker
username and the name of the repository that you created in [Configure CI/CD for
your Go application](configure-ci-cd.md).

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
service: server
name: server
namespace: default
spec:
replicas: 1
selector:
matchLabels:
service: server
strategy: {}
template:
metadata:
labels:
service: server
spec:
initContainers:
- name: wait-for-db
image: busybox:1.28
command: ['sh', '-c', 'until nc -zv db 5432; do echo "waiting for db"; sleep 2; done;']
containers:
- env:
- name: PGDATABASE
value: mydb
- name: PGPASSWORD
value: whatever
- name: PGHOST
value: db
- name: PGPORT
value: "5432"
- name: PGUSER
value: postgres
image: DOCKER_USERNAME/REPO_NAME
name: server
imagePullPolicy: Always
ports:
- containerPort: 8080
hostPort: 8080
protocol: TCP
resources: {}
restartPolicy: Always
status: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
service: db
name: db
namespace: default
spec:
replicas: 1
selector:
matchLabels:
service: db
strategy:
type: Recreate
template:
metadata:
labels:
service: db
spec:
containers:
- env:
- name: POSTGRES_DB
value: mydb
- name: POSTGRES_PASSWORD
value: whatever
- name: POSTGRES_USER
value: postgres
image: postgres
name: db
ports:
- containerPort: 5432
protocol: TCP
resources: {}
restartPolicy: Always
status: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
service: server
name: server
namespace: default
spec:
type: NodePort
ports:
- name: "8080"
port: 8080
targetPort: 8080
nodePort: 30001
selector:
service: server
status:
loadBalancer: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
service: db
name: db
namespace: default
spec:
ports:
- name: "5432"
port: 5432
targetPort: 5432
selector:
service: db
status:
loadBalancer: {}
```
In this Kubernetes YAML file, there are four objects, separated by the `---`. In addition to a Service and Deployment for the database, the other two objects are:

- A Deployment, describing a scalable group of identical pods. In this case,
you'll get just one replica, or copy of your pod. That pod, which is
described under `template`, has just one container in it. The container is
created from the image built by GitHub Actions in [Configure CI/CD for your
Go application](configure-ci-cd.md).
- A NodePort service, which will route traffic from port 30001 on your host to
port 8080 inside the pods it routes to, allowing you to reach your app
from the network.

To learn more about Kubernetes objects, see the [Kubernetes documentation](https://kubernetes.io/docs/home/).

## Deploy and check your application

1. In a terminal, navigate to the project directory
and deploy your application to Kubernetes.

```console
$ kubectl apply -f docker-go-kubernetes.yaml
```

You should see output that looks like the following, indicating your Kubernetes objects were created successfully.

```shell
deployment.apps/db created
service/db created
deployment.apps/server created
service/server created
```

2. Make sure everything worked by listing your deployments.

```console
$ kubectl get deployments
```

Your deployment should be listed as follows:

```shell
NAME READY UP-TO-DATE AVAILABLE AGE
db 1/1 1 1 76s
server 1/1 1 1 76s
```

This indicates all of the pods are up and running. Do the same check for your services.

```console
$ kubectl get services
```

You should get output like the following.

```shell
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.96.156.90 <none> 5432/TCP 2m8s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 164m
server NodePort 10.102.94.225 <none> 8080:30001/TCP 2m8s
```

In addition to the default `kubernetes` service, you can see your `server` service and `db` service. The `server` service is accepting traffic on port 30001/TCP.

3. Open a terminal and curl your application to verify that it's working.

```console
$ curl --request POST \
--url http://localhost:30001/send \
--header 'content-type: application/json' \
--data '{"value": "Hello, Oliver!"}'
```

You should get the following message back.

```json
{"value":"Hello, Oliver!"}
```

4. Run the following command to tear down your application.

```console
$ kubectl delete -f docker-go-kubernetes.yaml
```

## Summary

In this section, you learned how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine.

Related information:
- [Kubernetes documentation](https://kubernetes.io/docs/home/)
- [Deploy on Kubernetes with Docker Desktop](../../desktop/kubernetes.md)
- [Swarm mode overview](../../engine/swarm/_index.md)
Loading

0 comments on commit c8f0c28

Please sign in to comment.