我的build.gradle设置如下(为简洁起见,仅列出插件)

plugins {
  id 'java'
  id 'maven-publish'
  id 'signing'
  id 'org.springframework.boot' version '2.2.2.RELEASE'
  id 'io.spring.dependency-management' version '1.0.8.RELEASE'
  id 'com.limark.gitflowsemver' version '0.3.1'
  id 'com.google.cloud.tools.jib' version '1.8.0'
}
...
group = 'com.app.my'
// The below line remains commented
// version = '0.1.0'
...

jib {
  from {
    image = 'azul/zulu-openjdk-alpine:11-jre'
  }
  to {
    image = 'aws_account_id.dkr.ecr.region.amazonaws.com/my-app'
    tags = [version]
  }
  container {
    format = 'OCI'
  }
}

publishing {
  repositories {
    maven {
      def releasesRepoUrl = "http://localhost:8081/repository/maven-releases/"
      def snapshotsRepoUrl = "http://localhost:8081/repository/maven-snapshots/"
      url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
      credentials {
        username nexusUsername
        password nexusPassword
      }
    }
  }
  publications {
    mavenJava(MavenPublication) {
      artifactId = 'my-app'
      from components.java
      pom {
        name = 'My App'
        description = 'My App'
        url = 'https://my-app.com'
        developers {
          developer {
            id = 'john'
            name = 'John Doe'
            email = '[email protected]'
          }
        }
        scm {
          connection = 'scm:git:ssh://[email protected]:acme/my-app.git'
          developerConnection = 'scm:git:ssh://[email protected]:acme/my-app.git'
          url = 'https://bitbucket.org/acme/my-app'
        }
      }
    }
  }
}

signing {
  sign publishing.publications.mavenJava
}
gitflowsemver plugin基于GitFlow分支策略更新project.versionpublish任务能够访问gitflowsemver插件更新的版本,但是当我尝试使用jib构建docker镜像时,它不会使用更新的版本标记该镜像。它始终标记为unspecified。但是,如果我取消注释version = '0.1.0'行,则jib插件可以获取版本。我不明白为什么。非常感谢您的帮助。

最佳答案

更新:Jib 2.6.0 现在支持jib.to.imagejib.to.tags的后期评估。您可以使用project.provider配置它们,并且仅在使用它们时对其进行评估。

jib {
  ...
  to {
    image = 'rishabh9/jib-demo'
    tags = project.provider{[version]}
  }
  ...
}
对于旧版本的短臂,您可以尝试使用以前的答案:
来自关于gitter.im/google/jib的解决方案
查看gitsemver插件的代码,您可以看到它在评估项目后会更新版本:https://github.com/OpenLimark/GitFlowSemVerPlugin/blob/develop/src/main/groovy/com/limark/open/gradle/plugins/gitflowsemver/GitFlowSemVerPlugin.groovy#L51
因此,您可以做的就是在afterEvaluate块中设置标签版本,然后正确选择版本:
jib {
  ...
  to {
    image = 'rishabh9/jib-demo'
    project.afterEvaluate { // <-- so we evaluate version after it has been set
      tags = [version]
    }
  }
  ...
}

07-28 07:53