【问题标题】:SSH through gradle script通过 gradle 脚本进行 SSH
【发布时间】:2015-05-13 19:49:43
【问题描述】:

我正在编写 gradle 脚本,我有一个插件可以自动从 artifactory 下载 zip、jar 和 war 文件,之后我需要对远程服务器执行 ssh 并将该 zip 文件移动到特定位置。

我正在尝试按照以下方式进行操作,但出现错误

// ------ Tell the script to get dependencies from artifactory ------
buildscript {
    repositories {
      maven {
        url "http://cm.thm.com:80/artifactory/repo1-cache"
        url "http://cm.thm.com:80/artifactory/libs-snapshot"
         }
    }

    // ------ Tell the script to get dependencies from artifactory ------
    dependencies {
    classpath 'org.hidetake:gradle-ssh-plugin:0.4.3'
    classpath ([ "com.tr.cm:cmgradleplugin:1.1.118" ])
  }
}

apply plugin: 'com.truven.cm.cmgradleplugin'
apply plugin: 'org.hidetake.ssh'
/**
 * Use the deploy task to download the artifact and then unzip it to a local directory
 * -DjobName specifies the job that produced the artifact
 * -DbuildNumber specifies the build Number for the artifact
 */

 remotes {
  webServer {
    host = '10.2.12.912'
    user = 'cc'
   password = 'tr'
   // identity = file('id_rsa')
  }
}

task deployment(type: SshTask, dependsOn: deploy){
  ssh.run {
    session(remotes.webServer) {
      put(analytics-engine-AnalyticsLib-4.0.0.78.zip, "/applications/analyticsengine")
     execute 'unzip analytics-engine-AnalyticsLib-4.0.0.78.zip'
    }
  }
}

下面是错误

build file '/applications/jenkins/workspace/DeployArtifactToDev/environments/build.gradle': 43: unexpected token: 0.78 @ line 43, column 45.
     analytics-engine-AnalyticsLib-4.0.0.78.zip, 
                                   ^

  1 error

谁能告诉我我的 ssh 代码没问题,或者我需要改变什么吗?

【问题讨论】:

    标签: gradle build.gradle gradlew


    【解决方案1】:

    我可以为 ssh 编写代码

    // ------ Tell the script to get dependencies from artifactory ------
    buildscript {
        repositories {
          maven {
            url "http://c.t.th.com:8/artifactory/repo1-cache"
            url "http://c.t.t.com:8/artifactory/libs-snapshot"
             }
        }
    
        // ------ Tell the script to get dependencies from artifactory ------
        dependencies {
        classpath 'org.hidetake:gradle-ssh-plugin:1.1.1'
        classpath ([ "cm.t.cm:cmgrad:1.1.118" ])
      }
    }
    
    plugins {
        id 'war'
        id 'org.hidetake.ssh' version '1.1.1'
    }
    
    apply plugin: 'com.truven.cm.cmgradleplugin'
    /**
     * Use the deploy task to download the artifact and then unzip it to a local directory
     * -DjobName specifies the job that produced the artifact
     * -DbuildNumber specifies the build Number for the artifact
     */
    
    remotes {
        localhost {
            host = '1.2.12.1'
            user = "$deploy_username"
            password = "$deploy_dev_password"
        }
    }
    task deploymenttohost(dependsOn: deploy) << {
        ssh.run {
            session(remotes.localhost) {
                execute 'mkdir /applications/analyticsengine/temp'
                put from: 'build/artifacts/*.zip', into: '/applications/analyticsengine/temp'
                put from: 'deploymentfiles/*.sh', into: '/applications/analyticsengine/temp'
                execute 'unzip -d /applications/analyticsengine/temp/ /applications/analyticsengine/temp/*.zip'
                execute 'dos2unix /applications/analyticsengine/temp/analyticsEngine.sh'
                execute 'sh /applications/analyticsengine/temp/analyticsEngine.sh'
                execute 'pwd'
                execute 'rm -fr /applications/analyticsengine/temp'
            }
        }
    }
    
    task wrapper(type: Wrapper) {
        gradleVersion = '2.3'
    }
    

    它工作正常

    【讨论】:

      【解决方案2】:

      您正在尝试引用名称为 analytics-engine-AnalyticsLib-4.0.0.78.zip 的对象。那是不存在的。你在这里想要的是你的工件作为字符串的路径。您缺少引号:

      put('analytics-engine-AnalyticsLib-4.0.0.78.zip', "/applications/analyticsengine")
      

      使用硬编码字符串不是很好,因为它包含项目版本,并且路径可能不正确,具体取决于您执行构建的目录。如果您通过公开的属性archivePath 引用您尝试上传的档案的路径,那就更好了。在实践中,这将作为yourZipTask.archivePath.canonicalPath 工作。 yourZipTask 是生成 analytics-engine-AnalyticsLib-4.0.0.78.zip 的 ZIP 任务的实例。

      【讨论】:

      • 你能告诉我一件事,我如何为多个部署主机编写代码。现在我只有一个部署主机,但将来如果我有多个用于开发环境的部署服务器,那么我该如何运行我的脚本在每个主机的循环中。
      • 您将在 remotes 闭包中定义多个主机。然后根据提供的命令行选项-P,在运行时选择要在ssh.run.session 中使用的选项。有关如何使用/解析项目属性的更多信息,请参阅 Gradle 文档。
      猜你喜欢
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 2014-04-02
      相关资源
      最近更新 更多