In this tutorial, it will demonstrate how to setup a CI/CD pipeline in KubeSphere using an example GitHub repository, it includes 8 stages and will deploy a Documentation web service to Dev and Production environment respectively.
About 30 - 50 minutes.
The following flow chart briefly illustrates the process of the entire pipeline.
- Instructions
- Stage 1 - Checkout SCM: Pull source code from GitHub.
- Stage 2 - Get dependencies: install all of the dependencies via yarn.
- Stage 3 - Unit test: If the unit test passes, then continue the following tasks.
- Stage 4 - Build and push snapshot image: Build an image based on the branch selected in the behavioral strategy, and push the image with tag
SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER
to DockerHub.- Stage 5 - Push the latest image: Put the tag of master branch as "latest" and push it to DockerHub.
- Stage 6 - Deploy to Dev: Deploy the master branch to the Dev environment, which needs to be reviewed.
- Stage 7 - Push with tag: Generate the tag and release to GitHub and DockerHub.
- Stage 8 - Deploy to production: Deploy the published package to the Production environment.
For the convenience of demonstration, this document uses the GitHub repository devops-docs-sample as an example. You can fork it into your GitHub and modify the environment variables to actual parameters.
Sign in with project-regular
, enter into devops-demo
, we are going to create 3 credentials totally.
Click OK when you're done.
Same as above, create a credential for GitHub, then fill in the basic information, Credential ID is named github-id, other blanks according to your personal GitHub information.
Same as above, click Create Credentials to create a credential for kubeconfig
, name Credential ID as demo-kubeconfig, then click OK when you're done.
At this point, we have created 3 credentials totally.
The next step is to modify the corresponding 3 credential IDs to above 3 ones in jenkinsfile.
Fork the repository devops-docs-sample to your GitHub.
Jenkinsfile
in the root directory.edit
icon to edit Jenkinsfile, then modify values to yours in "environment".Key | Value | Description |
---|---|---|
DOCKERHUB_CREDENTIAL_ID | 'dockerhub-id' | This is the Dockerhub credential we created earlier, which is used to log in to your DockerHub |
GITHUB_CREDENTIAL_ID | 'github-id' | This is the GitHub credential created earlier which is used to push the tag to your GitHub |
KUBECONFIG_CREDENTIAL_ID | 'demo-kubeconfig' | This is the kubeconfig credential ID we created earlier, which is used to access a running Kubernetes cluster |
DOCKERHUB_NAMESPACE | 'your-dockerhub-account' | Replace with your DockerHub account name (It can also be the organization name under your account) |
GITHUB_ACCOUNT | 'your-github-account' | Replace with your GitHub account name (It can also be the organization name under the account) |
APP_NAME | devops-docs-sample | Application name, you can keep default value. |
···
environment {
DOCKERHUB_CREDENTIAL_ID = 'dockerhub-id'
GITHUB_CREDENTIAL_ID = 'github-id'
KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
DOCKERHUB_NAMESPACE = 'your-dockerhub-account'
GITHUB_ACCOUNT = 'your-github-account'
APP_NAME = 'devops-docs-sample'
}
···
Pipeline will deploy the Docs web service to both Dev and Production environment according to the yaml file, thus we are going to create 2 projects (i.e. kubesphere-docs-dev
and kubesphere-docs-prod
) as the Dev and Production environment respectively.
kubesphere-docs-dev
, others can be customized by yourself. The first project kubesphere-docs-dev
has been created successfully which represents the Dev environment.
Same as above, create the second project kubesphere-docs-prod
as the Production environment, you can reference above step 1 to create this project.
At this point, we have created two projects as the Dev and Production environment, thus the pipeline will deploy the web service and deployment to Dev and Production in sequence.
Redirect to Workbench, then select DevOps Projects tab and enter demo-devops that we created in the Admin Quick Start.
Click on Create button.
jenkinsfile-in-SCM
. Then click the Code Repository (Optional) to add GitHub repo.Enter any description into Token description in GitHub page, e.g. DevOps demo
. Leave the default selections in Select scopes, then click Generate token
, you will see a new access token generated by GitHub which is used to authentication.
Copy your access token and switch to KubeSphere, paste it into Token and choose Confirm.
In this step you will see all of the repositories under your personal account, then click Select this repo to choose the devops-docs-sample which already has its own Jenkinsfile in the root of this repository.
Exclude branches that are also filed as PRs
.Source code version of PR itself
.delete
icon since this example will not use this strategy.Leave the default value in Script Path since the Jenkinsfile location is in the root folder.
Choose Scan interval as 5 minutes
in Scan Repo Trigger, then click Create.
At this point, this pipeline has already been triggered and showing running.
Enter into this pipeline that we ran it manually, then watch its running status.
It will be paused when it runs to deploy to dev
stage, thus you could choose Proceed
in this stage. For the convenience, we will use project-regular to review directly.
Accordingly, the last 3 stages including deploy to dev
, push with tag
, deploy to production
require review 3 times in sequence. Note that it will not continue to run unless you click Proceed button.
Note: If you would like to point someone like project-admin to review, you can specify the name in Jenkinsfile as following:
···
input(id: 'release-image-with-tag', message: 'release image with tag?', submitter: 'project-admin')
···
Click into the pipeline under the activity list to inspect the running status and build progress in a visual way. Note that it might only display a log output page since it's still in the initialization phase, once the initialization finished it will direct to the visual page.
It also supports you to inspect logs for each stage, click Show Log button it will direct to a detailed popup window.
Once each stage of this pipeline ran successfully, the image with different tag (e.g. snapshot, TAG_NAME(v0.0.1), latest) will be pushed to DockerHub, then it will also generate a new release in GitHub, as well as the deployment and service will be deployed to kubesphere-docs-dev
and kubesphere-docs-prod
respectively, see the table as following:
Environment | Accessing URL | Project | Deployment | Service |
---|---|---|---|---|
Dev | http://EIP:30860 (i.e. ${EIP}:${NODEPORT} ) |
kubesphere-docs-dev | ks-docs-sample-dev | ks-docs-sample-dev |
Production | http://EIP:30960 (i.e. ${EIP}:${NODEPORT} ) |
kubesphere-docs-prod | ks-docs-sample | ks-docs-sample |
At this point, you can verify the status of their deployment and service in related project.
Then you can visit your profile in DockerHub and look at the image details.
Accessing the Docs service of Dev and Production environment:
Dev Environment
Enter http://EIP:30860/
in your browser to preview the service.
Production Environment
Enter http://EIP:30960/
in your browser to preview the service.
At this point, we have successfully created a pipeline based on the Jenkinsfile in the repository, it's recommeded you to follow with the next tutorial.