我尝试使用Code Pipeline自动执行代码部署。它使用Git Hub - >代码构建 - >如wiki中提到的云形成
我设法在this thread
建议的一些更改后运行管道但是每当我使用代码管道时,Lambda测试都会失败,说找不到类。
为了验证,我直接在AWS lambda控制台上传了jar,它工作正常。
我还验证了由“MyAppBuild”文件夹中的aws代码构建构建的jar,它包含zip文件中目标/ app-1.0-SNAPSHOT.jar中的jar文件以及我的SamTemplate.yml。
这是SamTemplate.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs the time
Parameters:
SourceBucket:
Type: String
Description: S3 bucket name for the CodeBuild artifact
SourceArtifact:
Type: String
Description: S3 object key for the CodeBuild artifact
Resources:
TimeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.xxx.Hello::handleRequest
Runtime: java8
CodeUri:
Bucket: !Ref SourceBucket
Key: !Ref SourceArtifact
Events:
MyTimeApi:
Type: Api
Properties:
Path: /TimeResource
Method: GET
这是buildSpec.yaml
version: 0.2
phases:
build:
commands:
- echo Build started on `date`
- mvn test
post_build:
commands:
- echo Build completed on `date`
- mvn package
install:
commands:
- aws cloudformation package --template-file SamTemplate.yaml --s3-bucket codepipeline-us-east-1-xxxx
--output-template-file NewSamTemplate.yaml
artifacts:
type: zip
files:
- SamTemplate.yaml
- target/app-1.0-SNAPSHOT.jar
有任何建议吗? 我用maven。
答案 0 :(得分:0)
最后,经过几次尝试后,我发现了一个可能的解决方案,包括aws代码构建,云形成和lambda。
重点是代码构建会为工件中提到的所有文件创建包装zip :
这是必须赋予aws lambda的相同zip文件。 为了使aws lambda接受一个zip作为有效,类应该是根文件夹,依赖库应该在libs文件夹中。
所以我设法做了我的构建规范。
version: 0.2
phases:
install:
commands:
- aws cloudformation package --template-file SamTemplate.yaml --s3-bucket codepipeline-us-east-1-XXXXXXXX
--output-template-file NewSamTemplate.yaml
build:
commands:
- echo Build started on `date`
- gradle build clean
- gradle test
post_build:
commands:
- echo Build started on `date`
- gradle build
- mkdir -p deploy
- cp -r build/classes/main/* deploy/
- cp NewSamTemplate.yaml deploy/
- cp -r build/libs deploy/
- ls -ltr deploy
- ls -ltr build
- echo Build completed on `date`
- echo Build is complete
artifacts:
type : zip
files:
- '**/*'
base-directory : 'deploy'