使用scp的原因

现在有一个项目,环境基本不变,使用jenkins更新时只更新代码

提前准备

我所使用的环境CI/CD

jenkins+k8s+rancher+gitlab

安装sshpass

这里我使用的是sshpass,可以直接把用户名和密码输入。

ubuntu
sudo apt install sshpass

运行sshpass

如果使用的是docker,要在docker容器中启动scp命令,如果是在服务器上直接启动jenkins,要在服务器上运行一下scp

编写Jenkinsfile

在创建工作流时已经配置过git的相关配置,所以关于git的配置在这里不需要,这里根据when选择分支,scp到不同的服务器

pipeline {
	agent none
	environment {
		registryMasterCredential = 'a3f90d3b-4be2-4b3c-aff8-5a85c577d4cd'
		registryCredential = '6e976e49-75c8-4adf-8eed-b4d677da41b1'
	}
	triggers {
		gitlab(
            triggerOnPush: true,
            triggerOnMergeRequest: false,
            branchFilterType: 'All'
		)
	}
	stages {
		stage('Test') {
			agent any
			steps {
				sh 'echo test'
			}
		}
		stage('Dev Scp') {
			agent any
			when {
                branch pattern: "dev", comparator: "REGEXP"
            }
			steps {
				script {
					sh 'sshpass -p "password" scp -r * user@ip:path'
					}
				}
			}
		stage('Master Scp') {
			agent any
			when {
                branch pattern: "master_temp", comparator: "REGEXP"
            }
			steps {
				script {
				   sh 'sshpass -p "password" scp -r * user@ip:path'
					}
				}
			}
		stage('Dev Environment Deploy') {
            agent any
            when {
                branch pattern: "dev", comparator: "REGEXP"
            }
            steps {
                script {
                    withKubeConfig([credentialsId: registryCredential]) {
                        sh 'kubectl apply -f deployment_dev.yaml'
                    }
                }
            }
        }
        stage('Master Environment Deploy') {
            agent any
            when {
                branch pattern: "master_temp", comparator: "REGEXP"
            }
            steps {
                script {
                    withKubeConfig([credentialsId: registryMasterCredential]) {
                        sh 'sed -i "s/{namespace}/test/g" deployment_master.yaml'
                        sh 'kubectl apply -f deployment_master.yaml'
                    }
                }
            }
        }

	}
}

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐