Jenkins将Harbor镜像拉取到目标服务器

将此文章写给我最心爱的女孩
目标服务器准备脚本文件

  • 流程
    在这里插入图片描述
    jenkins需要把 harbor地址、harbor仓库、镜像名、镜像版本、主机端口、容器端口告诉目标服务器,因此需要6个参数给脚本

1.创建shell脚本并授予可执行权限

touch deploy.sh 
chmod a+x deploy.sh 

2.编写脚本

#!/bin/bash
harbor_addr=$1
harbor_repo=$2
project=$3
version=$4
container_port=$5
host_port=$6

imageName=$harbor_addr/$harbor_repo/$project:$version

echo $imageName

containerId=`docker ps -a | grep ${project} | awk '{print $1}'`

echo $containerId

if [ "$containerId" != "" ];then
        docker stop $containerId
        docker rm $containerId
fi

tag=`docker images | grep ${project} | awk '{print $2}' `
echo $tag
 
if [ "$tag" =~ "$version" ];then
        docker rmi -f $imageName
fi          
            
docker login -u admin -p Harbor12345 $harbor_addr
           
docker pull $imageName
              
docker run -d -p $container_port:$host_port --name $project $imageName

echo "SUCCESS"                           

运行脚本

 ./deploy.sh 192.168.85.139 npy lover_story v3.0.0 8088 8082

3.jenkins配置

参数化构建:
在这里插入图片描述
构建后操作:
在这里插入图片描述
当构建之后,就会执行目标服务器的脚本文件去下载部署镜像了。

会出现的错误

DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            Install the buildx component to build images with BuildKit:
            https://docs.docker.com/go/buildx/

invalid argument "lover_story:origin/master" for "-t, --tag" flag: invalid reference format
See 'docker build --help'.
Build step 'Execute shell' marked build as failure
SSH: Current build result is [FAILURE], not going to run.
Finished: FAILURE

一定要把脚本文件移动到 /usr/bin目录下!

GitHub 加速计划 / ha / harbor
23.24 K
4.67 K
下载
Harbor 是一个开源的容器镜像仓库,用于存储和管理 Docker 镜像和其他容器镜像。 * 容器镜像仓库、存储和管理 Docker 镜像和其他容器镜像 * 有什么特点:支持多种镜像格式、易于使用、安全性和访问控制
最近提交(Master分支:1 个月前 )
c5d26723 chore(deps): bump github.com/go-openapi/runtime in /src Bumps [github.com/go-openapi/runtime](https://github.com/go-openapi/runtime) from 0.26.2 to 0.28.0. - [Release notes](https://github.com/go-openapi/runtime/releases) - [Commits](https://github.com/go-openapi/runtime/compare/v0.26.2...v0.28.0) --- updated-dependencies: - dependency-name: github.com/go-openapi/runtime dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: miner <yminer@vmware.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Shengwen YU <yshengwen@vmware.com> Co-authored-by: miner <yminer@vmware.com> 14 天前
76624373 chore(deps): bump github.com/coreos/go-oidc/v3 in /src Bumps [github.com/coreos/go-oidc/v3](https://github.com/coreos/go-oidc) from 3.10.0 to 3.11.0. - [Release notes](https://github.com/coreos/go-oidc/releases) - [Commits](https://github.com/coreos/go-oidc/compare/v3.10.0...v3.11.0) --- updated-dependencies: - dependency-name: github.com/coreos/go-oidc/v3 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> Co-authored-by: Shengwen YU <yshengwen@vmware.com> Co-authored-by: miner <yminer@vmware.com> 14 天前
Logo

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

更多推荐