How to use Play with custom modules and continuous integration

前端 未结 2 1270
隐瞒了意图╮
隐瞒了意图╮ 2021-02-15 12:24

How can I set up builds of Play apps and (custom) Play modules in a CI system so that when a module\'s build is good, the build installs the module artifacts in a local reposito

2条回答
  •  春和景丽
    2021-02-15 13:12

    I wrote this little Play! command that basically it does the same thing, but integrates nicely into Play!

    http://www.playframework.org/community/snippets/25

    from play.utils import *
    from poster.encode import multipart_encode
    from poster.streaminghttp import register_openers
    import urllib
    import urllib2
    import yaml
    
    COMMANDS = ['nexus-commit',]
    
    HELP = {
        'nexus-commit': 'push built module to nexus'
    }
    
    def execute(**kargs):
        app = kargs.get("app")
        args = kargs.get("args")
        play_env = kargs.get("env")
    
        print '~ '
        print '~ Commiting to nexus server'
        print '~ '
    
        app.check()
        nexus = app.readConf('nexus.server.url')
        user = app.readConf('nexus.server.user')
        password = app.readConf('nexus.server.password')
    
        if nexus:
            print '~ Found nexus repository : %s' % nexus
        else:
            print '~ No nexus server configured'
            print '~ Set up the following on your application.conf:'
            print '~    nexus.server.url'
            print '~    nexus.server.user'
            print '~    nexus.server.password'
            sys.exit(0)
    
        #Getting module version from dependencies file
        deps_file = os.path.join(app.path, 'conf', 'dependencies.yml')
        if os.path.exists(deps_file):
            f = open(deps_file)
            deps = yaml.load(f.read())
            #Is this a Play~ module?
            if "self" in deps:
                d = deps["self"].split(" ")
                module_version = d.pop()
                app_name = d.pop()
            else:
                app_name = app.name()
                print '~ This is not a Play module'
                module_version = app.readConf('application.version')
                if not module_version:
                    print '~ '
                    print '~ No application.version found in application.conf file'
                    print '~ '
                    module_version = raw_input('~ Provide version number to be pushed to Nexus:')
            f.close
    
        if module_version:
            print '~ Module version : %s' % module_version
            print '~ '
        else:
            print '~ No module version configured.'
            print '~ Configure your dependencies file properly'
            sys.exit(0)
    
        dist_dir = os.path.join(app.path, 'dist')
    
        #Only interested on .zip files
        for root, dirs, files in os.walk(dist_dir):
            files = [ fi for fi in files if fi.endswith(".zip") ]
    
        #Loop over all found files
        if len(files) >0:
            for file in files:
                if "-a" in args:
                    #We won't ask the user if he wants to commit
                    resp = "Y"
                else:
                    resp = raw_input('~ Do you want to post %s to nexus? (Y/N) ' % file)
                if resp == 'Y':
                    url = '%s/%s/%s-SNAPSHOT/%s-%s-SNAPSHOT.zip' % (nexus, app_name, module_version, app_name, module_version)
                    print '~ '
                    print '~ Sending %s to %s' % (file, url)
    
                    try:
                        data = open(os.path.join(dist_dir, file), 'rb')
                    except:
                        print '~ Error: could not open file %s for reading' % file
                        continue 
    
                    openers = register_openers()
                    #post data to Nexus using configured credentials
                    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
                    passman.add_password(None, url, user, password)
                    authhandler = urllib2.HTTPBasicAuthHandler(passman)
                    openers.add_handler(authhandler)
                    openers.add_handler(urllib2.HTTPHandler(debuglevel=1))
                    datagen, headers = multipart_encode({"file": data})
                    request = urllib2.Request(url, datagen, headers)
    
                    try:
                        print urllib2.urlopen(request).read()
                        print '~ File correctly sent'
                    except urllib2.HTTPError, err:
                        print '~ Error: Nexus replied with -- %s' % err
    
                else:
                    print '~ '
                    print '~ Skiping %s' % file
        else:
            print '~ '
            print '~ No module build found.'
            print '~ Try "play build-module" command first'
            print '~ '
    

提交回复
热议问题