Create a CI/CD Pipeline Based on Jenkinsfile

    • Make sure you have created your personal GitHub and account.
    • Be familiar with the basics of Git and version control.

    About 30 - 50 minutes.

    The following flow chart briefly illustrates the process of the entire pipeline.

    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.

    Create Credentials

    Sign in with project-regular, enter into devops-demo, we are going to create 3 credentials totally.

    Step 1: Create a DockerHub Credential

    1. Choose Credentials and click Create credentials.

    Create a Credential

    1. Fill in the basic information for DockerHub Credential.
    • Credential ID: enter dockerhub-id
    • Type: Account Credentials
    • Username: Your personal username for DockerHub
    • Token/password: Your personal password for DockerHub
    • Description: DockerHub Credential

    Click OK when you’re done.

    Step 2: Create a GitHub Credential

    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.

    Step 3: Create a Kubeconfig Credential

    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.

    Credential list

    The next step is to modify the corresponding 3 credential IDs to above 3 ones in jenkinsfile.

    Step 1: Fork Project

    Fork the repository devops-docs-sample to your GitHub.

    Step 2: Modify the Jenkinsfile

    1. Enter into Jenkinsfile in the root directory.

    Inspect Jenkinsfile

    1. Click edit icon to edit Jenkinsfile, then modify values to yours in “environment”.

    1. Click Commit changes when you’ve modified above environments.

    Commit changes

    Create Two Projects

    Pipeline will deploy the Docs web service to both Dev and Production environment according to the 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.

    Step 1: Basic Information

    1. Redirect to the Workbench, choose Projects → Create and select Create a resource project.

    1. Fill in the basic information, name this project as kubesphere-docs-dev, others can be customized by yourself.
    1. Click Next, keep default values in Advanced Settings for this demo, then select Create.

    The first project kubesphere-docs-dev has been created successfully which represents the Dev environment.

    Step 2: Create the Second Project

    Same as above, create the second project 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.

    Create the Second Project

    Step 1: Basic Information

    1. Redirect to Workbench, then select DevOps Projects tab and enter demo-devops that we created in the .

    1. Fill in the basic information, e.g. jenkinsfile-in-SCM. Then click the Code Repository (Optional) to add GitHub repo.

    Fill in the basic information

    Step 2: Add a Repository

    1. Click get token to create a new personal access token for your GitHub account.

    1. 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.

    2. Copy your access token and switch to KubeSphere, paste it into Token and choose Confirm.

    3. In this step you will see all of the repositories under your personal account, then click Select this repo to choose the which already has its own Jenkinsfile in the root of this repository.

    Select this repo

    1. Click Next when you’ve completed basic information.

    Step 3: Advanced Settings

    1. Check Discard old branch and leave the default value (-1) in Days to keep old items and Max period of old items to keep.

    1. As for Behavioral strategy, choose the following option:
    • Discover Branches: choose Exclude branches that are also filed as PRs.
    • Discover PR from the original repo: choose Source code version of PR itself.
    • Discover PR from Forks: click delete icon since this example will not use this strategy.

    Behavioral strategy

    1. Leave the default value in Script Path since the Jenkinsfile location is in the root folder.

    2. Choose Scan interval as 5 minutes in Scan Repo Trigger, then click Create.

    Step 4: Run the Pipeline

    1. Refresh your browser then you will be able to see there is a pipeline in Running status, which is triggered automatically.

    Run the Pipeline

    1. Click Run button, then leave the default branch (master) and input a version number in TAG_NAME (e.g. v0.0.1). Note that this tag is used to generate release and image in GitHub and DockerHub respectively.

    1. Choose OK, you will see it generates a new pipeline in activity list.

    Step 5: Review the Pipeline

    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 , 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.

    Review the Pipeline

    Note: If you would like to point someone like project-admin to review, you can specify the name in Jenkinsfile as following:

    1. ···
    2. input(id: 'release-image-with-tag', message: 'release image with tag?', submitter: 'project-admin')
    3. ···

    View the Pipeline

    1. 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.

    2. 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:

    EnvironmentAccessing URLProjectDeploymentService
    Devhttp://EIP:30860 (i.e. ${EIP}:${NODEPORT} )kubesphere-docs-devks-docs-sample-devks-docs-sample-dev
    Production (i.e. ${EIP}:${NODEPORT} )kubesphere-docs-prodks-docs-sampleks-docs-sample
    1. At this point, you can verify the status of their deployment and service in related project.

    2. Then you can visit your profile in DockerHub and look at the image details.

    View DockerHub

    1. Redirect to tour GitHub, you can verify the release result.

    Verify release

    Access the service

    Accessing the Docs service of Dev and Production environment:

    Dev Environment

    Enter 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.