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

时间:2019-03-11 12:41:55

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

当前,我期待将自己的项目迁移到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映像。

有人有任何类似的经验,可以帮助我创建文件吗?

1 个答案:

答案 0 :(得分:2)

这是我在一个项目中使用的buildspec.yml文件,用于构建Docker映像并将其推送到ECR

import tkinter as tk
from collections import OrderedDict

class Toplevel(tk.Toplevel):
    def __init__(self, parent, label_dict):
        super().__init__(parent)

        for key, text in label_dict.items():
            label = tk.Label(self, text=text)
            label.key = key
            label.pack()

    def update_labels(self, data):
        children = self.children
        for c in children:
            label = children[c]
            label.configure(text=data[label.key])

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.menubar = tk.Menu()
        self.menubar.add_command(label='Update', command=self.update)

        self.labels = OrderedDict({'label_1':'Label 1', 'label_2':'Label 2', 'label_3':'Label 3'})
        self.top = Toplevel(self, self.labels)


    def update(self):
        for key, text in self.labels.items():
            self.labels[key] = 'Updated {}'.format(text)

        self.top.update_labels(self.labels)

if __name__ == "__main__":
    App().mainloop()

我希望它可以帮助您入门。您显然需要根据需要对其进行修改。如果要添加更多阶段,请检查buildspec syntax

我将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 图像用于CodeBuild项目。我从CodePipeline输入aws/codebuild/docker:17.09.0环境变量。看起来像$REPOSITORY_URI

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