-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildspec.yaml
More file actions
80 lines (74 loc) · 3.38 KB
/
buildspec.yaml
File metadata and controls
80 lines (74 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
version: 0.2
phases:
install:
runtime-versions:
python: 3.x
commands:
- pip install awscli
- pip install --upgrade pip
pre_build:
commands:
- set -e
- echo Logging in to Docker Hub...
- echo $DOCKERHUB_PASSWORD | docker login -u $DOCKERHUB_USERNAME --password-stdin
- echo Setting image tags...
- export IMAGE_TAG="$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)-${CODEBUILD_BUILD_NUMBER}"
- export IMAGE_REPO_NAME="$DOCKERHUB_USERNAME/fastapi-sample"
- echo Installing dependencies...
- python -m venv .venv
- |
if [ -f .venv/bin/activate ]; then
source .venv/bin/activate
elif [ -f .venv/Scripts/activate ]; then
source .venv/Scripts/activate
fi
python --version
pip --version
pip install --upgrade pip setuptools wheel
pip install pytest pytest-cov pytest-html
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- mkdir -p test-results
- |
if [ -f .venv/bin/activate ]; then
source .venv/bin/activate
elif [ -f .venv/Scripts/activate ]; then
source .venv/Scripts/activate
fi
- pytest --junitxml=test-results/pytest-report.xml \
--cov=app --cov-report=xml:test-results/coverage.xml \
--cov-report=html:test-results/coverage-html \
--cov-fail-under=60 \
--html=test-results/pytest-report.html --self-contained-html --tb=short || true
build:
commands:
- echo Building the Docker image...
- docker build -t "$IMAGE_REPO_NAME:$IMAGE_TAG" .
- echo Build successful
post_build:
commands:
- echo Pushing Docker image to Docker Hub...
- docker push "$IMAGE_REPO_NAME:$IMAGE_TAG"
- echo Writing image definitions file...
- printf '{"ImageURI":"%s"}' "$IMAGE_REPO_NAME:$IMAGE_TAG" > image_definitions.json
- |
if [ -z "${S3_BUCKET}" ]; then echo "S3_BUCKET is not set. Please set it in the CodeBuild project environment."; exit 1; fi
- echo "Uploading test and coverage files to s3://${S3_BUCKET}/${CODEBUILD_SOURCE_VERSION}/${CODEBUILD_BUILD_ID}/pytest/ ..."
- aws s3 cp "test-results/pytest-report.xml" "s3://${S3_BUCKET}/${CODEBUILD_SOURCE_VERSION}/${CODEBUILD_BUILD_ID}/pytest/pytest-report.xml" --content-type "application/xml"
- aws s3 cp "test-results/coverage.xml" "s3://${S3_BUCKET}/${CODEBUILD_SOURCE_VERSION}/${CODEBUILD_BUILD_ID}/pytest/coverage.xml" --content-type "application/xml"
- aws s3 cp "test-results/pytest-report.html" "s3://${S3_BUCKET}/${CODEBUILD_SOURCE_VERSION}/${CODEBUILD_BUILD_ID}/pytest/pytest-report.html" --content-type "text/html"
- |
if [ -d "test-results/coverage-html/" ]; then
aws s3 sync "test-results/coverage-html/" "s3://${S3_BUCKET}/${CODEBUILD_SOURCE_VERSION}/${CODEBUILD_BUILD_ID}/pytest/coverage-html/"
fi
- aws s3 sync "test-results/" "s3://${S3_BUCKET}/${CODEBUILD_SOURCE_VERSION}/${CODEBUILD_BUILD_ID}/pytest/" --exclude "pytest-report.html" --exclude "coverage.html" --exclude "coverage-html/*"
artifacts:
files:
- image_definitions.json
- test-results/*
- test-results/coverage-html/**
discard-paths: no
reports:
pytest-reports:
files:
- test-results/pytest-report.xml
file-format: JUNITXML