码迷,mamicode.com
首页 > 其他好文 > 详细

如果想从jenkins直接生成docker镜像,并推送到harbor中,最简单的脚本如何实现?

时间:2017-06-20 14:46:34      阅读:1599      评论:0      收藏:0      [点我收藏+]

标签:erro   png   执行   ima   com   bsp   rbo   imp   cti   

如果不考虑意外,

第一版最简单的构思如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import getopt, sys
import subprocess
import os


site_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_code

def 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中,最简单的脚本如何实现?

标签:erro   png   执行   ima   com   bsp   rbo   imp   cti   

原文地址:http://www.cnblogs.com/aguncn/p/7053907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!