java - 如何在 buildspec.yml 中正确定义阶段和命令?

标签 java amazon-web-services docker gradle aws-code-deploy

目前,我正在期待将自己的项目迁移到 AWS 中,并且我已经将其上传到 CodeCommit 中。下一步是利用 CodePipeline 和 CodeBuild 进行构建和测试。

我有一个用 Java 编写的简单微服务,使用 build.gradle

buildscript {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.1.RELEASE'
        classpath 'com.bmuschko:gradle-docker-plugin:4.2.0'
    }
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.bmuschko.docker-spring-boot-application'

compileJava {
    sourceCompatibility = 11
    targetCompatibility = 11
}

group 'com.polubentcev.messenger'
version '1.0'

docker {
    springBootApplication {
        baseImage = 'openjdk:11-jre-slim'
    }
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
}

def messengerVersion = '1.0'
def springBootVersion = '2.1.2.RELEASE'

dependencies {
    compile 'com.polubentcev.messenger:messenger-util-model:'+messengerVersion
    compile 'org.springframework.boot:spring-boot-starter-web:'+springBootVersion
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.1.0.RELEASE'
    compile 'org.springframework.cloud:spring-cloud-starter-config:2.1.0.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:'+springBootVersion
    compile 'org.springframework.boot:spring-boot-starter-security:'+springBootVersion
    compile 'org.springframework.kafka:spring-kafka:2.2.3.RELEASE'
    compile 'org.springframework.cloud:spring-cloud-starter-oauth2:2.1.0.RELEASE'
    compile 'org.springframework.security.oauth:spring-security-oauth2:2.3.4.RELEASE'
    compile 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.2.RELEASE'
    compile 'org.postgresql:postgresql:42.2.5'
    compileOnly 'org.projectlombok:lombok:1.18.4'
    annotationProcessor 'org.projectlombok:lombok:1.18.4'
    testCompile 'org.springframework.boot:spring-boot-starter-test:'+springBootVersion
}

我想要一个用于 CodeDeploy 的 buildspec.yml 文件,以便运行单元测试并从中构建 docker 镜像。

有没有人有类似的经验可以帮我创建文件?

最佳答案

这是我在一个项目中用来构建 Docker 镜像并将其推送到 ECR 的 buildspec.yml 文件:

version: 0.2

phases:
  pre_build:
    commands:
      - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
      - COMMIT_HASH="$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)"
      - IMAGE_TAG="${COMMIT_HASH:=latest}"
      - printenv

  build:
    commands:
      - docker build -f infrastructure/Dockerfile -t $REPOSITORY_URI:latest .
      - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG

  post_build:
    commands:
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - export IMAGE_NAME='projectName'
      - export IMAGE_URI=$REPOSITORY_URI:$IMAGE_TAG
      - "printf '[{\"name\":\"%s\",\"imageUri\":\"%s\"}]' \"$IMAGE_NAME\" \"$IMAGE_URI\" > imagedefinitions.json"


artifacts:
  files:
    - imagedefinitions.json

希望它能帮助您入门。您显然需要根据需要修改它。如果您想添加更多阶段,请检查 buildspec syntax

我为 CodeBuild 项目使用 aws/codebuild/docker:17.09.0 图像。我从我的 CodePipeline 传入了 $REPOSITORY_URI 环境变量。它看起来像 123456789012.dkr.ecr.us-east-1.amazonaws.com/projectName

imagedefinitions.json 然后在后面的 CodePipeline 阶段用于将图像部署到 Fargate。

关于java - 如何在 buildspec.yml 中正确定义阶段和命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55102073/

相关文章:

json - CloudFormation 存储桶策略 - 缺少必填字段 "Effect"

java - 无法从jetty访问index.html

java - 调整许多 JTextField 的高度

java - 组合 JPA、EJB 和 JSF 托管 bean 的首选设计模式是什么?

symfony - 如何使用 docker 将 Symfony4 连接到 mysql

node.js - 后端容器无法连接到数据库 - Sequelize、Node、Postgres、Docker

postgresql - 将 postgres 参数传递到 Kubernetes 部署中

java - 如何检查 double 变量在 Java 中是否有两位以上的小数?

mysql - 通过 AWS 上托管的 servlet 从 mySQL DB 下载文件

amazon-web-services - 为什么 Elastic Beanstalk 负载均衡器拒绝建立 SSL 连接?