簡體   English   中英

如何在buildspec.yml中正確定義階段和命令?

[英]How to correctly define stages & commands in buildspec.yml?

當前,我期待將自己的項目遷移到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映像。

有沒有類似的經驗,可以幫助我創建文件嗎?

這是我在一個項目中使用的buildspec.yml文件,用於構建Docker映像並將其推送到ECR

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語法

我將aws/codebuild/docker:17.09.0映像用於CodeBuild項目。 我從CodePipeline傳入$REPOSITORY_URI環境變量。 看起來像123456789012.dkr.ecr.us-east-1.amazonaws.com/projectName

imagedefinitions.json則使用在后CodePipeline階段部署圖像Fargate。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM