Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Using Jenkins to Build a Pipeline

Updated on 2025-01-08 GMT+08:00

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:

  1. Click New Item on the Jenkins page.
  2. Enter a task name and select Create Pipeline.

  3. 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
                    }
                    }
                }
            }
        }
    }

  4. Save the settings and execute the Jenkins job.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback