Using Jenkins to Build a Pipeline
Obtaining a Long-Term Valid Login Command
During Jenkins installation and deployment, the Docker commands have been configured in the container (see 9). Therefore, no additional configuration is required for interconnecting Jenkins with SWR. You can directly run the Docker commands. You only need to obtain a long-term valid SWR login command. For details, see Obtaining a Long-Term Valid Login Command.
For example, the command of this account is as follows:
docker login -u ap-southeast-1@xxxxx -p xxxxx swr.ap-southeast-1.myhuaweicloud.com
Creating a Pipeline to Build and Push Images
In this example, Jenkins is used to build a pipeline to pull code from the code repository, package the code into an image, and push the image to SWR.
The pipeline creation procedure is as follows:
- Click New Item on the Jenkins page.
- Enter a task name and select Create Pipeline.
- Configure only the pipeline script.
The following pipeline scripts are for reference only. You can customize the script. For details about the syntax, see Pipeline.
Some parameters in the example need to be modified:
- git_url: Address of your code repository. Replace it with the actual address.
- swr_login: The login command obtained in Obtaining a Long-Term Valid Login Command.
- swr_region: SWR region.
- organization: The actual organization name in SWR.
- build_name: Name of the created image.
- credential: The cluster credential added to Jenkins. Enter the credential ID. If you want to deploy the service in another cluster, add the access credential of the cluster to Jenkins again. For details, see Setting Cluster Access Credentials.
- apiserver: IP address of the API server where the application cluster is deployed. Ensure that the IP address can be accessed from the Jenkins cluster.
//Define the code repository address. def git_url = 'https://github.com/lookforstar/jenkins-demo.git' //Define the SWR login command. //Define the SWR region. def swr_region = 'ap-southeast-1' //Define the name of the SWR organization to be uploaded. def organization = 'container' //Define the image name. def build_name = 'jenkins-demo' //Certificate ID of the cluster to be deployed def credential = 'k8s-token' //API server address of the cluster. Ensure that the address can be accessed from the Jenkins cluster. def apiserver = 'https://192.168.0.100:6443' pipeline { agent any stages { stage('Clone') { steps{ echo "1.Clone Stage" git url: git_url script { build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim() } } } stage('Test') { steps{ echo "2.Test Stage" } } stage('Build') { steps{ echo "3.Build Docker Image Stage" sh "docker build -t swr.${swr_region}.myhuaweicloud.com/${organization}/${build_name}:${build_tag} ." //${build_tag} indicates that the build_tag variable is obtained as the image tag. It is the return value of the git rev-parse --short HEAD command, that is, commit ID. } } stage('Push') { steps{ echo "4.Push Docker Image Stage" sh swr_login sh "docker push swr.${swr_region}.myhuaweicloud.com/${organization}/${build_name}:${build_tag}" } } stage('Deploy') { steps{ echo "5. Deploy Stage" echo "This is a deploy step to test" script { sh "cat k8s.yaml" echo "begin to config kubenetes" try { withKubeConfig([credentialsId: credential, serverUrl: apiserver]) { sh 'kubectl apply -f k8s.yaml' //The YAML file is stored in the code repository. The following is only an example. Replace it as required. } println "hooray, success" } catch (e) { println "oh no! Deployment failed! " println e } } } } } }
- Save the settings and execute the Jenkins job.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot