queirozsc
8/13/2017 - 6:03 AM

[codebuild] O AWS CodeBuild é um serviço de compilação totalmente gerenciado que compila código-fonte, executa testes e gera pacotes de soft

[codebuild] O AWS CodeBuild é um serviço de compilação totalmente gerenciado que compila código-fonte, executa testes e gera pacotes de software prontos para implantação #aws #codebuild

#repository file: test.js
var assert = require('assert');
describe('String Tests', function() {
  describe('Comparison', function() {
    it('Should be equal when strings are the same.', function() {
      assert.equal("No mercy for the misguided.", "No mercy for the misguided.");
    });
  });
});

#repository file: buildspec.yaml
version: 0.1
phases:
  install:
    commands:
      - echo Installing Express...
      - npm install express
      - echo Installing Mocha...
      - npm install -g mocha
  pre_build:
    commands:
      - echo Installing source NPM dependencies...
  build:
    commands:
      - echo Build started on `date`
      - echo Compiling the Node.js code
      - echo Running tests...
      - mocha test.js
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - '**/*'
    
#Running a build of project
aws codebuild start-build --project-name [ProjectName]
#Granting permissions for your user account to use AWS CodeBuild
aws iam attach-user-policy --user-name sergio.queiroz --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
aws iam attach-user-policy --user-name sergio.queiroz --policy-arn arn:aws:iam::aws:policy/AWSCodeBuildAdminAccess
	#local file: create-role.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codebuild.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
#local file: put-role-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "CloudWatchLogsPolicy",
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Sid": "CodeCommitPolicy",
      "Effect": "Allow",
      "Action": [
        "codecommit:GitPull"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Sid": "S3GetObjectPolicy",
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:GetObjectVersion"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Sid": "S3PutObjectPolicy",
      "Effect": "Allow",
      "Action": [
        "s3:PutObject"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}
#Creating a service role for the AWS CodeBuild service:
aws iam create-role --role-name CodeBuildServiceRole --assume-role-policy-document file://create-role.json
aws iam put-role-policy --role-name CodeBuildServiceRole --policy-name CodeBuildServiceRolePolicy --policy-document file://put-role-policy.json
#Creating your build project in AWS CodeBuild
aws codebuild create-project --name [ProjectName] --description "[ProjectDescription]" --source type="CODECOMMIT",location="[CloneUrlHttp]" --artifacts type="S3",location="[BucketName]",name="BuildOutput.zip",packaging="ZIP" --environment type="LINUX_CONTAINER",computeType="BUILD_GENERAL1_SMALL",image="aws/codebuild/nodejs:7.0.0" --service-role "[ServiceRoleARN]"