James, a Java engineer, likes to learn and document the current hot technologies and verify their advantages and disadvantages, as well as the implementation and promotion of the current hot cloud native solutions.

foreword

The Jenkins-based KubeSphere DevOps system is specially designed for CI/CD workflow in Kubernetes, it provides a one-stop solution to help development and operation and maintenance teams build, test and release applications to Kubernetes in a very simple way. It also features plugin management, Binary-to-Image (B2I), Source-to-Image (S2I), code dependency caching, code quality analysis, pipeline logging, and more.

DevOps systems provide users with an automated environment where applications can be automatically published to the same platform. It is also compatible with third-party private mirror repositories (such as Harbor) and code repositories (such as GitLab/GitHub/SVN/BitBucket). It provides users with a comprehensive and visual CI/CD pipeline, creating an excellent user experience, and this highly compatible pipeline capability is very useful in offline environments.

In short, DevOps can help us pull code, compile projects, build mirrors, push mirrors, and deploy projects in a fully automatic one-stop service.

For how to install the DevOps function, please refer to: https://kubesphere.io/en/docs/pluggable-components/devops/

Create DevOps Project && Create Pipeline

Create a DevOps project: Enterprise Space -> DevOps Project -> Create;
Fill in the project name and click OK;

Create a pipeline: Click on the DevOps project you just created, find the pipeline menu, click Create, and enter the pipeline name;

The code repository may or may not be filled in. If you fill it in now, there will be a webhook address. This address can go to the Git (or other repository) repository to automatically run our pipeline when the code is submitted, which is equivalent to a callback, so that Committing code to project deployment is a fully automated process.

Then here we first demonstrate the case of not filling in, fill in the name of the pipeline, click Next, and click Create.

Edit pipeline

Click on the pipeline you just created to enter, and click Edit pipeline:

As you can see, there are two sets of templates, here we select the second set, and then click Save:

You can see the effect, and then we click on the editing pipeline:

After coming in, you can see that there is an agent on the right, and there are agent type and lable below. Agent type Here we choose node. The following label has four options: base, go, maven, nodejs:

  • If we are packaging java related projects, then we can choose maven;
  • If it is a front-end project, you can use nodejs;
  • If you are developing in go language, you can choose go;
  • If it is other, you can choose base;

Official website explanation: https://kubesphere.io/zh/docs/devops-user-guide/how-to-use/choose-jenkins-agent/

Here the editor is deploying a java project, so I chose maven;

1. Pull the code

  • Step 1: Click to see the window on the right;
  • Step 2: Fill in the name of the stage
  • Step 3: Delete the specified base container, because we want to use the maven container

Click to add a step:

Select the specified container:

Fill in the container name and click OK:

Click Add Nested Step:

Choose to pull the code through git, of course, if you are svn, you can choose the following checkout. I am using git.

Fill in the gie repository Url, and branch, and then click New Credentials:

  • Enter the certificate id: this is named casually;
  • Type: Select Account Credentials;
  • username: git username
  • token/password: git password

Then click OK, OK.

If you want to display the pulled code directory, you can add nested steps:

Then click OK, Save.

After finishing, you can click Run to see the effect.

2. Compile the project

After running, we click the edit pipeline again, click the second step, and fill in the name: project compilation.

Also specify the maven container, add a nested step: shell script, enter the maven packaging command:

$ mvn clean package -Dmaven.test.skip=true

Click to confirm, click to save, you can also run to see the effect, whether it is successful.

3. Build the image

After running, we click the edit pipeline again, click the third step, and fill in the name: build image; agent type selection: none.

The same specified maven container, add nested steps: shell script, enter the command, display the files in the directory:

ls isee-gateway/target

Enter the docker build image command:

$ docker build -t isee-gateway:latest -f isee-gateway/Dockerfile isee-gateway/
Parameter Description:
isee-gateway:latest: image name has been tagged
-f: Specify the Dockerfile to build
isee-gateway/: in this directory

In practice, it is impossible for us to build one image at a time. We may have multiple, dozens of microservices that need to build images, so here we can also add parallel steps, and then follow the above steps to build:

4. Push image

After running, we click the edit pipeline again, click the fourth step,

  • Fill in the name: push image; (we want to push the image to the Alibaba Cloud image repository, of course, you can also push to other repositories or private repositories)
  • Proxy type selection: none

The same specified maven container, add nested steps: add credentials

Click OK, select the credential we just created after creation, fill in the username and password variables (meaning to use the username and password as a variable, and then log in to the Alibaba Cloud warehouse, push images and other operations to use this variable, in this case, No need to manually enter username and password)

[Login to Alibaba Cloud Warehouse] Add a nested step, select the shell script, and enter the following (here we use the username and password variables):

$ echo "$DOCKER_PWD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin

[Tagging the image] Add a nested step, select the shell script, and enter the following:

$ docker tag isee-gateway:latest $REGISTRY/$DOCKERHUB_NAMESPACE/isee-gateway:SNAPSHOT-$BUILD_NUMBER

[Push Mirror] Add nested steps, select shell script, and enter the following:

$ docker push $REGISTRY/$DOCKERHUB_NAMESPACE/isee-gateway:SNAPSHOT-$BUILD_NUMBER

Of course, it is also possible to add parallel steps here and push multiple images to the repository at the same time.

At this point, you will have a doubt. Not only the two variables DOCKER_PWD_VAR and DOCKER_USER_VAR are used, but also the REGISTRY variable, so what is this variable? Where is it defined?

After saving the pipeline, click Edit Jenkinsfile:

At the bottom, you can see that the REGISTRY variable is defined. We only need to change the value of the REGISTRY variable to the address of our Alibaba Cloud warehouse.

After saving, you can run it to see if it is successful.

5. Deployment

Old rules, follow the steps:

Follow the steps to continue:

New Credentials:

There is a certificate ID that needs to be filled in. How to fill in this?

First save the pipeline, click Edit Jenkinsfile, there is a variable at the bottom named: KUBECONFIG_CREDENTIAL_ID, and the variable value is: demo-kubeconfig We can fill in this value.

After creating the certificate, click OK to complete.

Save the pipeline, click Run, at this point, the entire pipeline is edited!

This article is published by the blog OpenWrite !

KubeSphere
124 声望57 粉丝

KubeSphere 是一个开源的以应用为中心的容器管理平台,支持部署在任何基础设施之上,并提供简单易用的 UI,极大减轻日常开发、测试、运维的复杂度,旨在解决 Kubernetes 本身存在的存储、网络、安全和易用性等痛...