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

如果不考虑意外,

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

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

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

上一篇:docker——三剑客之Docker swarm


下一篇:zabbix--高级篇-监控docker服务(一)