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

问题描述 投票:1回答:1

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

我用这个build.gradle用Java编写了一个简单的微服务

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镜像。

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

java amazon-web-services docker gradle aws-code-deploy
1个回答
2
投票

这是我在一个项目中用来构建并将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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.