Build and Deploy a Go Project

Prerequisites

Create a Docker Hub Access Token

  1. Log in to Docker Hub, click your account in the upper-right corner, and select Account Settings from the menu.

  2. Click Security in the left navigation pane and then click New Access Token.

  3. In the displayed dialog box, enter a token name (go-project-token) and click Create.

  4. Click Copy and Close and make sure you save the access token.

Create Credentials

You need to create credentials in KubeSphere for the access token created so that the pipeline can interact with Docker Hub for imaging pushing. Besides, you also create kubeconfig credentials for the access to the Kubernetes cluster.

  1. Log in to the web console of KubeSphere as project-regular. In your DevOps project, go to Credentials under DevOps Project Settings and then click Create on the Credentials page.

  2. In the displayed dialog box, set a Name, which is used later in the Jenkinsfile, and select Username and password for Type. Enter your Docker Hub account name for Username and the access token just created for Password/Token. When you finish, click OK.

    Tip

    For more information about how to create credentials, see Credential Management.

  3. Click Create again and select kubeconfig for Type. Note that KubeSphere automatically populates the Content field, which is the kubeconfig of the current user account. Set a Name and click OK.

Create a Pipeline

With the above credentials ready, you can create a pipeline using an example Jenkinsfile as below.

  1. To create a pipeline, click Create on the Pipelines page.

  2. Set a name in the displayed dialog box and click Next.

  3. In this tutorial, you can use default values for all the fields. On the Advanced Settings tab, click Create.

Edit the Jenkinsfile

  1. In the pipeline list, click the pipeline name to go to its details page. Click Edit Jenkinsfile to define a Jenkinsfile and your pipeline runs based on it.

  2. Copy and paste all the content below to the displayed dialog box as an example Jenkinsfile for your pipeline. You must replace the value of DOCKERHUB_USERNAME, DOCKERHUB_CREDENTIAL, KUBECONFIG_CREDENTIAL_ID, and PROJECT_NAME with yours. When you finish, click OK.

  1. pipeline {
  2. agent {
  3. label 'go'
  4. }
  5. environment {
  6. // the address of your Docker Hub registry
  7. REGISTRY = 'docker.io'
  8. // your Docker Hub username
  9. DOCKERHUB_USERNAME = 'Docker Hub Username'
  10. // Docker image name
  11. APP_NAME = 'devops-go-sample'
  12. // 'dockerhubid' is the credentials ID you created in KubeSphere with Docker Hub Access Token
  13. DOCKERHUB_CREDENTIAL = credentials('dockerhubid')
  14. // the kubeconfig credentials ID you created in KubeSphere
  15. KUBECONFIG_CREDENTIAL_ID = 'go'
  16. // the name of the project you created in KubeSphere, not the DevOps project name
  17. PROJECT_NAME = 'devops-go'
  18. }
  19. stages {
  20. stage('docker login') {
  21. steps{
  22. container ('go') {
  23. sh 'echo $DOCKERHUB_CREDENTIAL_PSW | docker login -u $DOCKERHUB_CREDENTIAL_USR --password-stdin'
  24. }
  25. }
  26. }
  27. stage('build & push') {
  28. steps {
  29. container ('go') {
  30. sh 'git clone https://github.com/yuswift/devops-go-sample.git'
  31. sh 'cd devops-go-sample && docker build -t $REGISTRY/$DOCKERHUB_USERNAME/$APP_NAME .'
  32. sh 'docker push $REGISTRY/$DOCKERHUB_USERNAME/$APP_NAME'
  33. }
  34. }
  35. }
  36. stage ('deploy app') {
  37. steps {
  38. container ('go') {
  39. withCredentials([
  40. kubeconfigFile(
  41. credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
  42. variable: 'KUBECONFIG')
  43. ]) {
  44. sh 'envsubst < devops-go-sample/manifest/deploy.yaml | kubectl apply -f -'
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }

Note

If your pipeline runs successfully, images will be pushed to Docker Hub. If you are using Harbor, you cannot pass the parameter to docker login -u via the Jenkins credential with environment variables. This is because every Harbor robot account username contains a $ character, which will be converted to $$ by Jenkins when used by environment variables. Learn more.

Run the Pipeline

  1. After you finish the Jenkinsfile, you can see graphical panels are displayed on the dashboard. Click Run to run the pipeline.

  2. In Run Records, you can see the status of the pipeline. It may take a while before it successfully runs.

Verify Results

  1. A Deployment is created in the project specified in the Jenkinsfile if the pipeline runs successfully.

  2. Check the image that is pushed to Docker Hub.