k8s devops 切换阿里云镜像
devops
·
1、登录阿里云,开通 【容器镜像服务】
阿里云链接
2、创建镜像仓库
创建命名空间 dome
选择本地仓库
3、使用阿里云镜像仓库 上传nginx镜像
1)登录
docker login --username= xxxx registry.cn-beijing.aliyuncs.com
然后输入密码【之前在阿里云创建 之前在阿里云创建 容器镜像服务设置的密码】
2)拉取镜像
docker pull registry.cn-shanghai.aliyuncs.com/XXXX/gulimall-nginx:v1.0
3)推送
docker commit -a "XXXX" -m "nginx" 97b83151128b XXXX/gulimall-nginx:v1.0
docker images查看上面的image ID
docker tag [ImageId] registry.cn-shanghai.aliyuncs.com/XXXX/gulimall-nginx:v1.0
docker push registry.cn-shanghai.aliyuncs.com/XXXX/gulimall-nginx:v1.0
4)上传成功
4、修改Jenkinsfile文件
1)镜像仓库地址 + 名称空间
之前是REGISTRY='docker.io’修改为REGISTRY=‘registry.cn-shanghai.aliyuncs.com’
DOCKERHUB_NAMESPACE = ‘XXXXX’
2)创建凭证aliyun的凭证
点击DevOps工程,选择工程,点击凭证,点击创建
名称:aliyun-hub-id
类型:账户凭证
用户名:mrwanzh
密码:创建阿里云镜像仓库时设置的密码
3)使用aliyun的凭证
DOCKER_CREDENTIAL_ID = 'aliyun-hub-id'
5、然后重新创建一个流水线,
Jenkinsfile-online
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('拉取代码') {
steps {
git(url: 'https://gitee.com/gpc12138/devops-java-sample.git', credentialsId: 'gitee-ik', branch: 'master', changelog: true, poll: false)
}
}
stage('unit test') {
steps {
container('maven') {
sh 'mvn clean -gs `pwd`/configuration/settings.xml test'
}
}
}
stage('sonarqube analysis') {
steps {
container('maven') {
withCredentials([string(credentialsId: "$SONAR_CREDENTIAL_ID", variable: 'SONAR_TOKEN')]) {
withSonarQubeEnv('sonar') {
sh "mvn sonar:sonar -gs `pwd`/configuration/settings.xml -Dsonar.branch=$BRANCH_NAME -Dsonar.login=$SONAR_TOKEN"
}
}
timeout(time: 1, unit: 'HOURS') {
waitForQualityGate true
}
}
}
}
stage('构建镜像-推送镜像') {
steps {
container('maven') {
sh 'mvn -Dmaven.test.skip=true -gs `pwd`/configuration/settings.xml clean package'
sh 'docker build -f Dockerfile-online -t $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER .'
withCredentials([usernamePassword(passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,credentialsId : "$DOCKER_CREDENTIAL_ID" ,)]) {
sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest '
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:latest '
}
}
}
}
stage('部署到k8s') {
steps {
input(id: "deploy-to-dev-$PROJECT_NAME", message: "是否将 $PROJECT_NAME 部署到集群中?")
kubernetesDeploy(configs: "deploy/**", enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")
}
}
stage('发布版本'){
when{
expression{
return params.PROJECT_VERSION =~ /v.*/
}
}
steps {
container ('maven') {
input(id: 'release-image-with-tag', message: '发布当前版本镜像吗?')
sh 'docker tag $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:SNAPSHOT-$BRANCH_NAME-$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:$PROJECT_VERSION '
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$PROJECT_NAME:$PROJECT_VERSION '
withCredentials([usernamePassword(credentialsId: "$GITEE_CREDENTIAL_ID", passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh 'git config --global user.email "2376306355@qq.com" '
sh 'git config --global user.name "guopengcheng" '
sh 'git tag -a $PROJECT_NAME-$PROJECT_VERSION -m "$PROJECT_VERSION" '
sh 'git push http://$GIT_USERNAME:$GIT_PASSWORD@gitee.com/$GITEE_ACCOUNT/devops-java-sample.git --tags --ipv4'
}
}
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'aliyun-hub-id'
GITEE_CREDENTIAL_ID = 'gitee-ik'
KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
REGISTRY = 'registry.cn-beijing.aliyuncs.com'
DOCKERHUB_NAMESPACE = 'XXX' //阿里云 名称空间
GITEE_ACCOUNT = 'XXX'
PROJECT_NAME = 'devops-java-sample'
SONAR_CREDENTIAL_ID = 'sonar-qube'
BRANCH_NAME = 'master'
}
parameters {
string(name: 'PROJECT_VERSION', defaultValue: 'v0.0Beta', description: '项目版本')
}
}
更多推荐
已为社区贡献3条内容
所有评论(0)