# Hacking Cloudbuild

## CodeBuild

Codebuild is a fully managed continious integration service that compiles code, run tests and produces packages to be used for deployment.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FsW4Yhf5ZD0GSBBc7XOes%2Fimage.png?alt=media&#x26;token=716d99e2-382e-466c-9839-ff3aed60bdce" alt=""><figcaption></figcaption></figure>

## Components

### Source

Source in CodeBuild acts as source provider for application code which is used for building it. Source can be anything from S3 bucket to OpenSource Repository hosting platforms like Github/Bitbucket and even AWS's own CodeCommit.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FfG0LZUAeMzjQgJy6VgGB%2Fimage.png?alt=media&#x26;token=b132f676-2def-4dc1-b326-736920384e42" alt=""><figcaption></figcaption></figure>

In this case, source is S3 bucket and inside S3 bucket called `securitylabs-articles`, file called `code.zip` contains all the code for application.

This means that once the build is started, it would fetch code.zip file present in securitylabs-articles bucket.

### Environment

Environment defines the Operating System and its preferences where the building would take place. This controls things like OS,architecture and instance specifications like RAM and vCPU to be used for building application. You can also define fields like Environment variables and VPC that can be used for building the application.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FhorNAbncgWXuyw3DjnWg%2Fimage.png?alt=media&#x26;token=5c6dabb4-ae40-4906-90aa-a2b77c565978" alt=""><figcaption></figcaption></figure>

### Buildspec

Buildspec defines the rules i.e steps that needs to be taken to create the application from code. This is the heart of CodeBuild which create applications ready for deployment.

The below buildspec.yml file indicates the build steps to be taken.

```
version: 0.2
env:
  variables:
      Argument: "Value"
phases:
  pre_build:
    commands:
       - aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin 584358494719.dkr.ecr.us-east-2.amazonaws.com
  build:
    commands:
       - docker build -t securitylabs-article .
       - docker tag securitylabs-article:latest 584358494719.dkr.ecr.us-east-2.amazonaws.com/securitylabs-article:latest
       - docker push 584358494719.dkr.ecr.us-east-2.amazonaws.com/securitylabs-article:latest
  post_build:
    commands:
       - echo "Build ready!"
artifacts:
  files:
     - "artifact.txt"
  name: $(date +%Y-%m-%d)
  discard-paths: yes
  base-directory: "/uploads"
```

### Artifacts

Artifacts define the upload location for artifcats that are generated during build. For instance, here we inform CodeBuild that artifcats generated during build would be uploaded to S3 bucket called `securitylabs-article`

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FgDf2pnt3dF5UxZihBKg6%2Fimage.png?alt=media&#x26;token=91505cee-0b90-425a-91b2-150bae70a287" alt=""><figcaption></figcaption></figure>

You can also configure the path and the filename of the file containing the artifacts that would be uploaded. In the below screenshot, we see that artifacts will be uploaded at `path/artifact.zip` in the S3 bucket. Artifact package indicates that artifacts need to be ziped before uploading.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FGPdUTY3Z3TZ3pIywGDIn%2Fimage.png?alt=media&#x26;token=7fc9b76a-550e-4c9d-8271-6cb32e7dca8f" alt=""><figcaption></figcaption></figure>

### Build triggers

Build Triggers acts as cron job for scheduling execution of CodeBuild projects. In the below case, we are scheduling once in a week build.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FXmefbNfxTgrtsqwCXF7h%2Fimage.png?alt=media&#x26;token=74639ff4-aadd-4ce9-a3ce-89c5aebf93b3" alt=""><figcaption></figcaption></figure>

## Running Build

To start the build process, simply click on the `Start Build` button to initiate the building process.

<figure><img src="https://1804885456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIo3S6x9y21ea77Yw303B%2Fuploads%2FFCIbiO845qIUt2qDNPcN%2Fimage.png?alt=media&#x26;token=8f560b0a-18de-40d0-91d0-d192275766b2" alt=""><figcaption></figcaption></figure>

