Deploy Apps in a Multi-cluster Project Using a Jenkinsfile

    • You need to have a Docker Hub account.
    • You need to on your host cluster.
    • You need to create a workspace with multiple clusters, a DevOps project on your host cluster, a multi-cluster project (in this tutorial, this multi-cluster project is created on the host cluster and one member cluster), and an account (). This account needs to be invited to the DevOps project and the multi-cluster project with the role operator. For more information, see Create Workspaces, Projects, Accounts and Roles, and Multi-cluster Projects.

    Create a Docker Hub Access Token

    1. Click Security and New Access Token.

      dockerhub-create-token

    2. Enter the token name and click Create.

    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 need to create kubeconfig credentials for the access to the Kubernetes cluster.

    1. Log in to the web console of KubeSphere as project-regular. Go to your DevOps project and click Create in Credentials.

    2. In the dialog 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

      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 Credential ID 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.

      create-pipeline

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

      create-pipeline-2

    1. 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_CREDENTIAL, KUBECONFIG_CREDENTIAL_ID, , and MEMBER_CLUSTER_NAME with yours. When you finish, click OK.

      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

    After you save the Jenkinsfile, click Run. If everything goes well, you will see the Deployment workload in your multi-cluster project.