jenkins构建项目配置及推送阿里云镜像仓库

使用阿里云的k8s,更新一个项目需要如下步骤:

1.先更新代码

2.再将代码打包生成一个docker镜像,推送到阿里云镜像仓库(私有的)

3.在阿里云上使用新的镜像新启一个docker,并把老的docker删除(阿里云k8s可以配置钩子,镜像更新自动重启docker)

使用jenkins构建,就方便很多了。(先要阿里云k8s镜像更新自动重启docker配置好)

在此只使用jenkins运行一个sh文件。

只需要在jenkins配置下执行命令即可:

 sh文件就按照项目发布步骤来写(按照自己项目内的构建逻辑来写,

此处的代码结构:admin项目内有如下几个文件夹

docker [主要用到里面的Dockerfile配置文件]

conf.d [内部存放nginx配置文件]

config [项目配置文件,代码仓库内忽略了配置文件]

git [git内的文件是从代码仓库拉取下来的最新的代码]

www [项目代码]

】具体逻辑不在此赘述):

示例代码:

path="/var/www/"  #项目路径
folder="admin" #项目文件夹名
gitUrl="git@git.com:test/test.git" #项目git地址
gitBranch="admin" #要拉取的git分支
projectEnv="production" #环境标识
dockerTestRegistry="registry.cn-shanghai.aliyuncs.com/test/test:prod" #阿里云镜像仓库地址
BUILD_ENV="production"
buildVersionFile="test/build-version.html" #构建版本时间记录

echo "======== start to publish $folder:$projectEnv (branch:$gitBranch)"
cd $path || (echo "no such dictionary: $path" && exit 1)

if [ ! -d $folder ];then
  mkdir $folder
fi
cd $folder || (echo "fail to create folder: $path/$folder" && exit 1)

echo '======== start to fetch git'

if [ ! -d git ];then
  git clone -b $gitBranch $gitUrl git  
fi

cd git || (echo 'fail to create git folder' && exit 1)
git checkout -f $gitBranch
git pull

echo '======== start to prepare docker file'
cd ..

echo '======== start to copy www files'
if [ ! -d www];then
  mkdir www
else
  rm -rf .www/*
fi

cp -r ./git/www/* ./www
echo '======== copy src files success'

echo '======== start to copy config files'
cp -r ./config/* ./www/common/config
echo '======== copy config files success'

echo '======== start to write build time file'
rm -rf $buildVersionFile
touch $buildVersionFile
echo `date +"%Y-%m-%d %H:%M:%S"`>>$buildVersionFile
cat $buildVersionFile
echo '======== write build time file success'

echo '======== prepare docker file finished'

echo '======== start to run docker file'
cd docker || (echo "no docker dictionary!" && exit 1)

echo '======== start to build admin'
rm -rf conf.d/*
cp -r ../conf.d/* conf.d

docker build -t $dockerAdminRegistry -f ./Dockerfile ../ --build-arg BUILD_ENV=$BUILD_ENV
docker push $dockerAdminRegistry

echo '======== build admin success'

echo '======== all finished!'
exit 0

总结:项目发布的所有的步骤都在sh内写好,Jenkins的作用只是执行此sh文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要确保已经安装了 Jenkins,并且在你的服务器上已经配置好了阿里云镜像地址。 以下是配置 Jenkins 使用阿里云镜像的步骤: 1. 打开 Jenkins 的管理界面,点击左侧菜单栏的 "Manage Jenkins"。 2. 在 "Manage Jenkins" 页面,点击 "Manage Plugins"。 3. 在 "Manage Plugins" 页面,切换到 "Available" 选项卡,并在搜索框中输入 "Config File Provider"。找到对应的插件,勾选并点击 "Install without restart" 安装该插件。 4. 安装完成后,返回到 Jenkins 的管理界面,再次点击 "Manage Jenkins"。 5. 在 "Manage Jenkins" 页面,点击 "Managed files"。 6. 在 "Managed files" 页面,点击 "Add a new Config"。 7. 在 "Config file type" 下拉框中选择 "Custom file",点击 "OK"。 8. 在 "Name" 输入框中填写一个名称(比如 "aliyun-mirror")。 9. 在 "Content" 输入框中粘贴以下内容: ```bash # 修改 Docker 镜像源为阿里云镜像 FROM ubuntu:latest RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse' > /etc/apt/sources.list \ && echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse' >> /etc/apt/sources.list \ && echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse' >> /etc/apt/sources.list \ && echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse' >> /etc/apt/sources.list \ && echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse' >> /etc/apt/sources.list \ && echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse' >> /etc/apt/sources.list \ && echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse' >> /etc/apt/sources.list \ && echo 'deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse' >> /etc/apt/sources.list ``` 10. 点击 "Save" 保存配置文件。 配置完成后,你可以在 Jenkins 的 Job 中使用该配置文件。在你的 Job 中选择 "配置",然后在 "Build Environment" 部分勾选 "Provide Configuration files",并选择 "Custom files"。在 "Files to copy" 中选择 "aliyun-mirror" 配置文件。 这样,Jenkins 在每次构建时都会使用该配置文件,从而将 Docker 镜像源修改为阿里云镜像。请确保你的服务器能够访问到阿里云镜像服务。 希望对你有帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值