Skip to content

Commit e4b2ea6

Browse files
Merge pull request #2 from code-dot-org/build-docker
iterate on pr buildspec
2 parents 9306981 + db27916 commit e4b2ea6

File tree

8 files changed

+102
-11
lines changed

8 files changed

+102
-11
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
FROM python:3.11-slim
22

3+
RUN pip install Flask
4+
35
WORKDIR /app
46
COPY requirements.txt .
57

cicd/1-setup/cicd-dependencies.template.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ Resources:
7272
Action: codestar-connections:UseConnection
7373
Resource:
7474
- !Sub arn:aws:codestar-connections:us-east-1:${AWS::AccountId}:connection/*
75+
- PolicyName: CodeBuildSecretsAccess
76+
PolicyDocument:
77+
Statement:
78+
- Effect: Allow
79+
Action:
80+
- "secretsmanager:GetSecretValue"
81+
Resource:
82+
- !Sub arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:cicd/*
7583

7684
Outputs:
7785
AiProxyCodeBuildArtifactBucket:

cicd/1-setup/deploy-cicd-dependencies.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ read -r -p "Would you like to deploy this template to AWS account $ACCOUNT? [y/N
1919
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
2020
then
2121
echo Updating cloudformation stack...
22+
echo "Follow along at https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks?filteringText=aiproxy-cicd-deps&filteringStatus=active&viewNested=true&hideStacks=false"
2223
aws cloudformation deploy \
2324
--stack-name aiproxy-cicd-deps \
2425
--template-file ${TEMPLATE_FILE} \

cicd/3-app/aiproxy/buildspec.yml

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,68 @@
11
version: 0.2
2+
3+
env:
4+
secrets-manager:
5+
DOCKER_HUB_USERNAME: cicd/docker-hub/username
6+
DOCKER_HUB_PAT: cicd/docker-hub/pat
7+
28
phases:
39
install:
410
runtime-versions:
511
python: 3.11
612
commands:
713
- pip install cfn-lint
14+
15+
pre_build:
16+
commands:
17+
- echo Logging in to Docker Hub...
18+
- echo $DOCKER_HUB_PAT | docker login -u $DOCKER_HUB_USERNAME --password-stdin
19+
820
build:
9-
# This should be moved to a shell script if it gets more complicated.
21+
# This will build and push the docker image to ECR and package the
22+
# Cloudformation template to be passed as an Artifact to future pipeline
23+
# steps. It will also run unit tests and linting.
1024
commands:
1125
- set -e
1226
- BRANCH_NAME=${CODEBUILD_WEBHOOK_HEAD_REF#"refs/heads/"}
1327

14-
- echo "This is where I would run python tests, if I had any tests"
28+
- cd $CODEBUILD_SRC_DIR
29+
30+
- echo "Validating Cloudformation Templates..."
31+
- cfn-lint cicd/1-setup/*.template.yml
32+
- cfn-lint cicd/2-cicd/*.template.yml
33+
- cfn-lint cicd/3-app/aiproxy/template.yml
1534

35+
- echo "Building Docker Image..."
36+
- IMAGE_NAME=aiproxy
37+
- IMAGE_TAG=$(git rev-parse --short HEAD)
38+
- docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
39+
40+
- echo "Running Unit Tests..."
41+
- echo "This is where I would run my unit tests"
42+
43+
- echo "Pushing Docker Image to ECR..."
44+
- ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
45+
- AWS_REGION=$(aws configure get region)
46+
- ECR_REGISTRY="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
47+
- ECR_REPO_NAME="aiproxy-${BRANCH_NAME}"
48+
- FULL_IMAGE_NAME="${ECR_REGISTRY}/${ECR_REPO_NAME}:${IMAGE_TAG}"
49+
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
50+
- docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${FULL_IMAGE_NAME}
51+
- docker push ${FULL_IMAGE_NAME}
52+
53+
- echo "Linting cloudformation..."
1654
- cd $CODEBUILD_SRC_DIR
1755
- cp cicd/3-app/aiproxy/template.yml ./template.yml
1856
- cfn-lint template.yml
1957
- cat template.yml
2058

59+
- echo "Creating environment config..."
2160
- cicd/3-app/aiproxy/config/create-environment-config.sh
2261

62+
- echo "Packaging Cloudformation Template..."
2363
- aws cloudformation package --template-file template.yml --s3-bucket $ARTIFACT_STORE --s3-prefix package --output-template-file packaged-app-template.yml
64+
65+
- echo "Running ls command..."
66+
- ls
2467
artifacts:
2568
files: '**/*'

cicd/3-app/aiproxy/integration-test-buildspec.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ phases:
1010
commands:
1111
- set -e
1212

13-
- echo "This is where I would run my integration tests, if I had any tests"
13+
- echo "This is where I would run my tests against the Test environment, if I had any tests"

cicd/3-app/aiproxy/pr-buildspec.yml

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
11
version: 0.2
2+
3+
env:
4+
secrets-manager:
5+
DOCKER_HUB_USERNAME: cicd/docker-hub/username
6+
DOCKER_HUB_PAT: cicd/docker-hub/pat
7+
28
phases:
39
install:
410
runtime-versions:
511
python: 3.11
612
commands:
713
- pip install cfn-lint
814

15+
pre_build:
16+
commands:
17+
- echo Logging in to Docker Hub...
18+
- echo $DOCKER_HUB_PAT | docker login -u $DOCKER_HUB_USERNAME --password-stdin
19+
20+
921
build:
1022
# This should be moved to a shell script if it gets more complicated.
1123
commands:
1224
- set -e
25+
1326
- BRANCH_NAME=${CODEBUILD_WEBHOOK_HEAD_REF#"refs/heads/"}
1427
- ARTIFACT_PATH=branch/$BRANCH_NAME/$CODEBUILD_BUILD_NUMBER
1528

29+
- cd $CODEBUILD_SRC_DIR
30+
31+
- echo "Validating Cloudformation Templates..."
1632
- cfn-lint cicd/1-setup/*.template.yml
1733
- cfn-lint cicd/2-cicd/*.template.yml
34+
- cfn-lint cicd/3-app/aiproxy/template.yml
1835

19-
- echo "This is where I would test my python code, if I had any tests"
36+
- echo "Building Docker Image..."
37+
- IMAGE_NAME=aiproxy
38+
- IMAGE_TAG=$(git rev-parse --short HEAD)
39+
- docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
2040

21-
- cd $CODEBUILD_SRC_DIR
22-
- cp cicd/3-app/aiproxy/template.yml ./template.yml
23-
- cfn-lint template.yml
24-
25-
- aws cloudformation package --template-file template.yml --output-template-file cloudformation-output.yml --s3-bucket $ARTIFACT_STORE --s3-prefix "$ARTIFACT_PATH/cloudformation-package"
26-
- aws s3 cp cloudformation-output.yml "s3://${ARTIFACT_STORE}/${ARTIFACT_PATH}/"
27-
- echo "Artifacts uploaded to S3, view them at https://console.aws.amazon.com/s3/buckets/${ARTIFACT_STORE}?region=us-east-1&prefix=${ARTIFACT_PATH}/"
41+
- echo "Running Unit Tests..."
42+
- docker run --rm ${IMAGE_NAME}:${IMAGE_TAG} python -m unittest test_app.py
43+
44+
- echo "Pushing Docker Image to ECR..."
45+
- echo "This is where I would push the docker image to ECR, when I get around to it"

src/test_app.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from app import app
3+
4+
class TestApp(unittest.TestCase):
5+
6+
def setUp(self):
7+
self.app = app.test_client()
8+
9+
def test_process(self):
10+
response = self.app.get('/process')
11+
self.assertEqual(response.data.decode(), 'hello world')
12+
self.assertEqual(response.status_code, 200)
13+
14+
if __name__ == '__main__':
15+
unittest.main()

test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
docker build -t aiproxy:local .
4+
docker run --rm aiproxy:local python -m unittest test_app.py

0 commit comments

Comments
 (0)