From 45dfa367f8208c039dbf9603fa6da8d5b637ac91 Mon Sep 17 00:00:00 2001 From: Craig Osterhout Date: Tue, 31 Oct 2023 14:01:40 -0700 Subject: [PATCH 1/4] update deploy and ci-cd in go guide Signed-off-by: Craig Osterhout --- content/language/golang/configure-ci-cd.md | 133 +++++++++++-- content/language/golang/deploy.md | 214 ++++++++++++++++++++- content/language/golang/develop.md | 2 +- 3 files changed, 332 insertions(+), 17 deletions(-) diff --git a/content/language/golang/configure-ci-cd.md b/content/language/golang/configure-ci-cd.md index 8b8227ca4b1..74c297fb300 100644 --- a/content/language/golang/configure-ci-cd.md +++ b/content/language/golang/configure-ci-cd.md @@ -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" >}} \ No newline at end of file diff --git a/content/language/golang/deploy.md b/content/language/golang/deploy.md index 113814e4631..5209b70feab 100644 --- a/content/language/golang/deploy.md +++ b/content/language/golang/deploy.md @@ -1,7 +1,213 @@ --- -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" >}} \ No newline at end of file +## 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: + - image: DOCKER_USERNAME/REPO_NAME + name: server + imagePullPolicy: Always + ports: + - containerPort: 80 + 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: example + - name: POSTGRES_PASSWORD + value: example + 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: 80 + 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 5432/TCP 2m8s + kubernetes ClusterIP 10.96.0.1 443/TCP 164m + server NodePort 10.102.94.225 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 browser and visit your app at `localhost:30001`. You should see your + application. + +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) \ No newline at end of file diff --git a/content/language/golang/develop.md b/content/language/golang/develop.md index d60fe2f9306..7c62cfbfdc6 100644 --- a/content/language/golang/develop.md +++ b/content/language/golang/develop.md @@ -273,7 +273,7 @@ func initStore() (*sql.DB, error) { } if _, err := db.Exec( - "CREATE TABLE IF NOT EXISTS message (value STRING PRIMARY KEY)"); err != nil { + "CREATE TABLE IF NOT EXISTS message (value TEXT PRIMARY KEY)"); err != nil { return nil, err } From 9db17608653ffdce9bd5f57621604882e908bef9 Mon Sep 17 00:00:00 2001 From: Craig Osterhout Date: Tue, 31 Oct 2023 14:21:17 -0700 Subject: [PATCH 2/4] update repo Signed-off-by: Craig Osterhout --- content/language/golang/develop.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/language/golang/develop.md b/content/language/golang/develop.md index 7c62cfbfdc6..282bd206ef6 100644 --- a/content/language/golang/develop.md +++ b/content/language/golang/develop.md @@ -169,12 +169,12 @@ Now that you have started and configured the database engine, you can switch you The example application for this module is an extended version of `docker-gs-ping` application you've used in the previous modules. You have two options: * You can update your local copy of `docker-gs-ping` to match the new extended version presented in this chapter; or -* You can clone the [docker/docker-gs-ping-roach](https://github.com/docker/docker-gs-ping-roach) repository. This latter approach is recommended. +* You can clone the [docker/docker-gs-ping-dev](https://github.com/docker/docker-gs-ping-dev) repository. This latter approach is recommended. To checkout the example application, run: ```console -$ git clone https://github.com/docker/docker-gs-ping-roach.git +$ git clone https://github.com/docker/docker-gs-ping-dev.git # ... output omitted ... ``` From 06037566e576514605b4ecb481ac9dc63a9cd9b9 Mon Sep 17 00:00:00 2001 From: Craig Osterhout Date: Tue, 31 Oct 2023 14:29:57 -0700 Subject: [PATCH 3/4] update toc Signed-off-by: Craig Osterhout --- data/toc.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/toc.yaml b/data/toc.yaml index 7c597e71fe1..aa124055f67 100644 --- a/data/toc.yaml +++ b/data/toc.yaml @@ -85,7 +85,7 @@ Guides: path: /language/golang/run-tests/ - title: "Configure CI/CD" path: /language/golang/configure-ci-cd/ - - title: "Deploy your app" + - title: "Test your deployment" path: /language/golang/deploy/ - sectiontitle: C# (.NET) section: From 6e4621bacb33d30435cea1930ce988b99f3b7799 Mon Sep 17 00:00:00 2001 From: Craig Osterhout Date: Tue, 31 Oct 2023 14:51:42 -0700 Subject: [PATCH 4/4] polish deploy Signed-off-by: Craig Osterhout --- content/language/golang/deploy.md | 39 +++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/content/language/golang/deploy.md b/content/language/golang/deploy.md index 5209b70feab..2903252dbd8 100644 --- a/content/language/golang/deploy.md +++ b/content/language/golang/deploy.md @@ -50,11 +50,22 @@ spec: image: busybox:1.28 command: ['sh', '-c', 'until nc -zv db 5432; do echo "waiting for db"; sleep 2; done;'] containers: - - image: DOCKER_USERNAME/REPO_NAME + - 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: 80 + - containerPort: 8080 hostPort: 8080 protocol: TCP resources: {} @@ -83,9 +94,11 @@ spec: containers: - env: - name: POSTGRES_DB - value: example + value: mydb - name: POSTGRES_PASSWORD - value: example + value: whatever + - name: POSTGRES_USER + value: postgres image: postgres name: db ports: @@ -107,7 +120,7 @@ spec: ports: - name: "8080" port: 8080 - targetPort: 80 + targetPort: 8080 nodePort: 30001 selector: service: server @@ -194,8 +207,20 @@ To learn more about Kubernetes objects, see the [Kubernetes documentation](https 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 browser and visit your app at `localhost:30001`. You should see your - application. +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.