Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for additional codebuild resources #17

Merged
merged 5 commits into from
Oct 4, 2023
Merged

Conversation

gsoria
Copy link

@gsoria gsoria commented Oct 3, 2023

This PR includes new modules to handle Codebuild builds, build batches, report groups, and source credentials. Adding support for webhooks and reports was not needed as these resources depend on the project and project's builds respectively.

Testing

After creating the codebuild resources using the script mentioned below, run aws-nuke specifying the following resources:

  • CodeBuildBuild
  • CodeBuildProject
  • CodeBuildSourceCredential
  • CodeBuildBuildBatch
  • CodeBuildReportGroup

Once aws-nuke finishes, verify that there are no resources left with the following commands:

echo "Listing projects"
aws codebuild list-projects 
echo "Listing report groups"
aws codebuild list-report-groups 
echo "Listing source credentials"
aws codebuild list-source-credentials
echo "Listing builds"
aws codebuild list-builds
echo "Listing build batches"
aws codebuild list-build-batches

Setup

#!/bin/bash

# Get AWS account ID
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
echo "AWS Account ID: $AWS_ACCOUNT_ID"

# Generate a random number
randomNum=$(cat /dev/urandom | LANG=c tr -dc '0-9' | head -c 12)
echo "Random number: $randomNum"

# create a service role for codebuild
aws iam create-role --role-name CodeBuildServiceRole --assume-role-policy-document '{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "codebuild.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}'

# attach policies to the codebuild service role
aws iam attach-role-policy --role-name CodeBuildServiceRole --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
aws iam attach-role-policy --role-name CodeBuildServiceRole --policy-arn arn:aws:iam::aws:policy/AmazonEC2FullAccess

# create source code 
mkdir -p src/main/java
mkdir -p src/test/java

cat <<EOF >./src/main/java/MessageUtil.java
public class MessageUtil {
  private String message;

  public MessageUtil(String message) {
    this.message = message;
  }

  public String printMessage() {
    System.out.println(message);
    return message;
  }

  public String salutationMessage() {
    message = "Hi!" + message;
    System.out.println(message);
    return message;
  }
}
EOF

cat <<EOF >./src/test/java/TestMessageUtil.java
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class TestMessageUtil {

  String message = "Robert";    
  MessageUtil messageUtil = new MessageUtil(message);
   
  @Test
  public void testPrintMessage() {      
    System.out.println("Inside testPrintMessage()");     
    assertEquals(message,messageUtil.printMessage());
  }

  @Test
  public void testSalutationMessage() {
    System.out.println("Inside testSalutationMessage()");
    message = "Hi!" + "Robert";
    assertEquals(message,messageUtil.salutationMessage());
  }
}
EOF

cat <<EOF >pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>messageUtil</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>
  <name>Message Utility Java Sample App</name>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>	
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
      </plugin>
    </plugins>
  </build>
</project>
EOF

# create the buildspec file
cat <<EOF >buildspec.yml
version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto11
  pre_build:
    commands:
      - echo Nothing to do in the pre_build phase...
  build:
    commands:
      - echo Build started on `date`
      - mvn install
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - target/messageUtil-1.0.jar
EOF

# create a codebuild project spec
cat <<EOF >create-project.json
{
  "name": "codebuild-demo-project",
  "source": {
    "type": "S3",
    "location": "codebuild-$randomNum-input/MessageUtil.zip"
  },
  "artifacts": {
    "type": "S3",
    "location": "codebuild-$randomNum-output"
  },
  "environment": {
    "type": "LINUX_CONTAINER",
    "image": "aws/codebuild/standard:5.0",
    "computeType": "BUILD_GENERAL1_SMALL"
  },
  "serviceRole": "arn:aws:iam::$AWS_ACCOUNT_ID:role/CodeBuildServiceRole",
  "buildBatchConfig": {
    "serviceRole": "arn:aws:iam::$AWS_ACCOUNT_ID:role/CodeBuildServiceRole",
    "combineArtifacts": false,
    "restrictions": {
        "maximumBuildsAllowed": 2,
        "computeTypesAllowed": ["BUILD_GENERAL1_SMALL"]
    },
    "timeoutInMins": 10
  }
}
EOF

# create two S3 buckets
aws s3api create-bucket --bucket codebuild-$randomNum-input --no-cli-pager
aws s3api create-bucket --bucket codebuild-$randomNum-output --no-cli-pager

# Create a zip file of the source code
zip -r  MessageUtil.zip ./src/* pom.xml buildspec.yml

# Upload the source code to the S3 bucket
aws s3 cp MessageUtil.zip s3://codebuild-$randomNum-input

aws codebuild create-project --cli-input-json file://create-project.json --no-cli-pager

# create a codebuild report group
cat <<EOF >create-report-group-source.json
{
    "name": "cli-created-report-group",
    "type": "TEST",
    "exportConfig": {
        "exportConfigType": "S3",
        "s3Destination": {
            "bucket": "codebuild-$randomNum-output",
            "path": "",
            "packaging": "ZIP",
            "encryptionDisabled": true
        }
    }
}
EOF
aws codebuild create-report-group \
    --cli-input-json file://create-report-group-source.json \
    --no-cli-pager

# import source credentials
aws codebuild import-source-credentials --server-type BITBUCKET --auth-type BASIC_AUTH --token my-Bitbucket-password --username my-Bitbucket-username

# start a codebuild build
aws codebuild start-build --project-name codebuild-demo-project

# start a codebuild build batch
aws codebuild start-build-batch --project-name codebuild-demo-project

Copy link
Member

@sstoops sstoops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These modules worked for me! I ran the test script in conjunction with the companions PRs and saw resources created and removed. I left a small comment in line about something that stuck out to me, but nothing big.

Comment on lines 31 to 33
if resp == nil {
return nil, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this follow a different pattern than the modules above? This is the only one where I see if resp == nil. Reading the SDK docs doesn't clarify to me when the response would ever be nil.

I also notice a lack of pagination, but that appears correct as this endpoint does not seem to be paginated if I'm reading the docs correctly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a previous version of this module I had an error saying resp could be null. I double checked, and it looks to me that it is actually not needed. Removed it in 6c8489e Thanks for catching this @sstoops

Regarding the pagination, you are right, this endpoint is not paginated, SourceCredentialsInfo doesn't have a NextToken field.

Signed-off-by: Gabriela S. Soria <[email protected]>
@gsoria gsoria merged commit 076892d into oreilly-main Oct 4, 2023
1 check passed
corybekk pushed a commit that referenced this pull request Nov 6, 2024
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants