Build and Deploy a Go Project

Prerequisites

Create Docker Hub Access Token

  1. Sign in Docker Hub and select Account Settings from the menu in the top right corner.

    dockerhub-settings

  2. Click Security and New Access Token.

    dockerhub-create-token

  3. Enter the token name and click Create.

    dockerhub-token-ok

  4. Click Copy and Close and remember to save the access token.

    dockerhub-token-copy

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 the web console of KubeSphere as project-regular. Go to your DevOps project and click Create in Credentials.

    create-dockerhub-id

  2. In the dialogue that appears, set a Credential ID, which will be used later in the Jenkinsfile, and select Account Credentials for Type. Enter your Docker Hub account name for Username and the access token just created for Token/Password. When you finish, click OK.

    credential-docker-create

    提示

    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 Credential ID and click OK.

    create-kubeconfig

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.

    create-pipeline

  2. Set a name in the pop-up window and click Next directly.

    set-pipeline-name

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

    create-pipeline-2

Edit Jenkinsfile

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

    edit-jenkinsfile

  2. Copy and paste all the content below to the pop-up window 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. node {
    4. label 'maven'
    5. }
    6. }
    7. environment {
    8. // the address of your harbor registry
    9. REGISTRY = 'docker.io'
    10. // your docker hub username
    11. DOCKERHUB_USERNAME = 'yuswift'
    12. // docker image name
    13. APP_NAME = 'devops-go-sample'
    14. // ‘dockerhubid’ is the credential id you created in KubeSphere for docker access token
    15. DOCKERHUB_CREDENTIAL = credentials('dockerhubid')
    16. //the kubeconfig credential id you created in KubeSphere
    17. KUBECONFIG_CREDENTIAL_ID = 'go'
    18. // the name of the project you created in KubeSphere, not the DevOps project name
    19. PROJECT_NAME = 'devops-go'
    20. }
    21. stages {
    22. stage('docker login') {
    23. steps{
    24. container ('maven') {
    25. sh 'echo $DOCKERHUB_CREDENTIAL_PSW | docker login -u $DOCKERHUB_CREDENTIAL_USR --password-stdin'
    26. }
    27. }
    28. }
    29. stage('build & push') {
    30. steps {
    31. container ('maven') {
    32. sh 'git clone https://github.com/yuswift/devops-go-sample.git'
    33. sh 'cd devops-go-sample && docker build -t $REGISTRY/$DOCKERHUB_USERNAME/$APP_NAME .'
    34. sh 'docker push $REGISTRY/$DOCKERHUB_USERNAME/$APP_NAME'
    35. }
    36. }
    37. }
    38. stage ('deploy app') {
    39. steps {
    40. container('maven') {
    41. kubernetesDeploy(configs: 'devops-go-sample/manifest/deploy.yaml', kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
    42. }
    43. }
    44. }
    45. }
    46. }

    备注

    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 Pipeline

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

    run-pipeline

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

    pipeline-running

Verify Results

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

    view-deployments

  2. Check whether the image is pushed to Docker Hub as shown below:

    docker-image-1

    docker-image-2