编写代码上传到gitee

我用fastapi写了一个简单的helloworld接口
代码链接:https://gitee.com/feiminjie/helloworld.git

jenkins拉取代码

这里直接流水线方法:

Jenkins中点击新建任务,输入名称,选择流水线,点击确定

先配置可以根据标签拉取代码。在general中勾选参数化构建过程(This project is parameterized),选择git参数

jenkins 拉取的项目根据项目名称移动到指定的目录 jenkins拉取代码_docker


在代码中添加Jenkinsfile上传到gitee

在流水线中添加git信息,输入一下信息后,其他默认,最后脚本路径写Jenkinsfile,点击应用保存

jenkins 拉取的项目根据项目名称移动到指定的目录 jenkins拉取代码_docker_02

jenkins 拉取的项目根据项目名称移动到指定的目录 jenkins拉取代码_docker_03


再点击流水线语法

jenkins 拉取的项目根据项目名称移动到指定的目录 jenkins拉取代码_docker_04


选择好这个,然后配置好git信息,点击下面生成流水线脚本,然后复制生成的脚本,然后黏贴到git的jenkinsfile文件的步骤中。生成的语句中

jenkins 拉取的项目根据项目名称移动到指定的目录 jenkins拉取代码_docker_05


master表示拉取最新的代码,但是我们希望通过tag拉取,所以改为${tag}。

复制流水线脚本,粘贴到Jenkinsfile中的steps中,文件的结果如下:

pipeline{
	// 制定任务在哪个集群节点中执行
	agent any

	// 声明全局变量,方便后面使用
	environment {
		key = 'value'
	}

	stages {
        	stage('拉取git仓库代码') {
            		steps {
        checkout scmGit(branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: 'ee882b26-32f7-487f-af8b-8ce97ae6d923', url: 'https://gitee.com/feiminjie/helloworld.git']])
            		}
        	}
    }
}

然后build一下,查看日志确认拉取成功

生成docker镜像

创建对应Dockerfile,我创建的如下:

# 基胐镜像
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
# 设置工作目录
WORKDIR /app
# 复制项目女件到容器中
COPY ./requirements.txt /app
# 安装依赖项
RUN pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
COPY . /app
# 暴露端口
EXPOSE 8000
# 运行命令
CMD ["gunicorn", "main:app", "-b", "0.0.0.0:8000", "-w", "4", "-k", "uvicorn.workers.UvicornWorker"]

写入后上传到git上,然后在jenkins任务中流水线语法出选择

jenkins 拉取的项目根据项目名称移动到指定的目录 jenkins拉取代码_docker_06


复制流水线脚本粘贴到Jenkinsfile 中

这时Jenkinsfile内容为

pipeline{
	// 制定任务在哪个集群节点中执行
	agent any

	// 声明全局变量,方便后面使用
	environment {
		key = 'value'
	}

	stages {
        	stage('拉取git仓库代码') {
            		steps {
        checkout scmGit(branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: 'ee882b26-32f7-487f-af8b-8ce97ae6d923', url: 'https://gitee.com/feiminjie/helloworld.git']])
            		}
        	}
        	stage('生成docker镜像') {
            		steps {
            		    sh 'docker build -t hello:$tag .'
            		}
        	}
    }
}

配置jenkins推送到harbor

cd /var/run
# ll命令找到docker.sock,修改用户组和权限
chown root:root docker.sock
chmod o+rw docker.sock    # 注意如果服务器重启了需要重新设置
cd /usr/local/docker/jenkins_docker/
vi docker-compose.yml
# 修改docker-compose.yml 修改后的文件内容为
version: '3.1'
services:
  jenkins:
    image: 'jenkins/jenkins:2.426.3-lts'
    container_name: jenkins
    ports:
      - '8080:8080'
      - '50000:50000'
    volumes:
      - './data/:/var/jenkins_home/'
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker
      - /etc/docker/daemon.json:/etc/docker/daemon.json
 # 修改后执行
 docker-compose up -d

在jenkins容易内部验证docker是否可行

docker exec -it jenkins bash
docker version # 输出版本信息

修改daemon.json,加入insecure-registries ip和端口为harbor的服务的ip和端口, 不然会报一个https的错误

cd /etc/docker
# 如果没有daemon.json则创建该文件,写入下面内容
{
  "registry-mirrors": ["https://registry.cn-hangzhou.aliyuncs.com"],
  "insecure-registries": ["103.39.222.98:80"],
  "experimental": true
}

# 修改后重启docker
systemctl restart docker

然后在jenkins任务中流水线语法出选择

jenkins 拉取的项目根据项目名称移动到指定的目录 jenkins拉取代码_ico_07

复制流水线脚本粘贴到Jenkinsfile 中
这时Jenkinsfile内容为

pipeline{
	// 制定任务在哪个集群节点中执行
	agent any

	// 声明全局变量,方便后面使用
	environment {
		key = 'value'
	}

	stages {
        	stage('拉取git仓库代码') {
            		steps {
        checkout scmGit(branches: [[name: '${tag}']], extensions: [], userRemoteConfigs: [[credentialsId: 'ee882b26-32f7-487f-af8b-8ce97ae6d923', url: 'https://gitee.com/feiminjie/helloworld.git']])
            		}
        	},
        	stage('生成docker镜像') {
            		steps {
            		    sh 'docker build -t hello:$tag .'
            		}
        	},
        	stage('推送harbor') {
            		steps {
            		    sh '''docker login -u admin -p Harbor12345 103.39.222.98:80
docker tag hello:$tag 103.39.222.98:80/repo/hello:$tag
docker push 103.39.222.98:80/repo/hello:$tag'''
            		}
        	}
    }
}

jenkins中build一下该任务,查看执行任务是否成功,进harbor的repo项目中查看是否有镜像,走到这里应该是有的。