首页 > 代码库 > 如果想从jenkins直接生成docker镜像,并推送到harbor中,最简单的脚本如何实现?
如果想从jenkins直接生成docker镜像,并推送到harbor中,最简单的脚本如何实现?
如果不考虑意外,
第一版最简单的构思如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-import getopt, sysimport subprocessimport ossite_name = app_name = dep_version = war_name = ""docker_harbor_ip = "x.x.x.x"docker_login_name = "boss"docker_login_password = "client"#参数用法def usage(): print "./docker.py -s site -a app -d dev --war=war_name" sys.exit()#获取参数def get_opt(): try: opts, args = getopt.getopt(sys.argv[1:], "hs:a:d:", ["help", "war="]) except getopt.GetoptError: print "getopt function has error.." usage() for o, a in opts: if o in ("-h", "--help"): usage() if o in ("-s"): site_name = a if o in ("-a"): app_name = a if o in ("-d"): dep_version = a if o in ("--war"): war_name = a return site_name, app_name, dep_version, war_name#执行shell命令def docker_cmd(cmd): return_code = subprocess.call(cmd, shell=True) if return_code != 0: print "command === %s === error" % (cmd) usage() return return_codedef main(): site_name, app_name, dep_version, war_name = get_opt() if "" in [site_name, app_name, dep_version, war_name]: print "args have empty value..." usage() docker_login = ‘docker login -u %s -p %s http://%s‘ % (docker_login_name, docker_login_password, docker_harbor_ip ) docker_tag = ‘docker build -t %s/%s/%s:%s .‘ % (docker_harbor_ip , site_name.lower(), app_name.lower(), dep_version) docker_push = ‘docker push %s/%s/%s:%s‘ % (docker_harbor_ip , site_name.lower(), app_name.lower(),dep_version) for cmd in [docker_login, docker_tag, docker_push]: docker_cmd(cmd) print "docker cmd is run..." if __name__ ==‘__main__‘: main()
如果想从jenkins直接生成docker镜像,并推送到harbor中,最简单的脚本如何实现?
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。