编写代码上传到gitee

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

jenkins拉取代码

这里直接流水线方法:
Jenkins中点击新建任务,输入名称,选择流水线,点击确定
先配置可以根据标签拉取代码。在general中勾选参数化构建过程(This project is parameterized),选择git参数
在这里插入图片描述
在代码中添加Jenkinsfile上传到gitee
在流水线中添加git信息,输入一下信息后,其他默认,最后脚本路径写Jenkinsfile,点击应用保存在这里插入图片描述 在这里插入图片描述
再点击流水线语法
在这里插入图片描述
选择好这个,然后配置好git信息,点击下面生成流水线脚本,然后复制生成的脚本,然后黏贴到git的jenkinsfile文件的步骤中。生成的语句中
在这里插入图片描述
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任务中流水线语法出选择
在这里插入图片描述
复制流水线脚本粘贴到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任务中流水线语法出选择
在这里插入图片描述

复制流水线脚本粘贴到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项目中查看是否有镜像,走到这里应该是有的。

GitHub 加速计划 / ha / harbor
10
3
下载
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
最近提交(Master分支:6 个月前 )
45659070 Fix integration issue with UI Signed-off-by: stonezdj <stone.zhang@broadcom.com> 13 小时前
add0b600 chore(deps): bump go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp Bumps [go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp](https://github.com/open-telemetry/opentelemetry-go) from 1.31.0 to 1.34.0. - [Release notes](https://github.com/open-telemetry/opentelemetry-go/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-telemetry/opentelemetry-go/compare/v1.31.0...v1.34.0) --- updated-dependencies: - dependency-name: go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐