|
|
|
|
@ -0,0 +1,48 @@
|
|
|
|
|
# AWS CodeBuild:从 CodeCommit 仓库 pms 拉取代码,仓库根目录即应用根(Dockerfile、backend/、frontend/ 等)
|
|
|
|
|
# 构建 Docker 镜像并推送到 ECR(中国区 cn-northwest-1);无需 cd,工作目录已是应用根
|
|
|
|
|
# 环境变量 IMAGE_REPO_NAME、AWS_ACCOUNT_ID、AWS_DEFAULT_REGION 由 CodeBuild 或 Pipeline 注入
|
|
|
|
|
# PYTHON_BASE_IMAGE 已设为 ECR 中的 python-base,运行阶段基于该镜像,避免重复 apt/pip 安装
|
|
|
|
|
version: 0.2
|
|
|
|
|
|
|
|
|
|
phases:
|
|
|
|
|
pre_build:
|
|
|
|
|
commands:
|
|
|
|
|
- echo Logging in to Amazon ECR...
|
|
|
|
|
- "aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com.cn"
|
|
|
|
|
- "IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)"
|
|
|
|
|
- "IMAGE_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com.cn/$IMAGE_REPO_NAME:$IMAGE_TAG"
|
|
|
|
|
- echo Build image $IMAGE_URI
|
|
|
|
|
- "echo Using BASE_IMAGE for runtime: ${PYTHON_BASE_IMAGE:-python:3.12-slim}"
|
|
|
|
|
build:
|
|
|
|
|
commands:
|
|
|
|
|
- echo Building frontend Vue on host to avoid Docker Hub node image
|
|
|
|
|
- command -v npm >/dev/null || (dnf install -y nodejs 2>/dev/null || yum install -y nodejs 2>/dev/null || true)
|
|
|
|
|
# 需安装 devDependencies(vite、@vitejs/plugin-vue)才能执行 npm run build
|
|
|
|
|
- cd frontend && (npm ci 2>/dev/null || npm install) && npm run build
|
|
|
|
|
- cd ..
|
|
|
|
|
- |
|
|
|
|
|
if [ -n "$PYTHON_BASE_IMAGE" ]; then
|
|
|
|
|
docker build --build-arg BASE_IMAGE=$PYTHON_BASE_IMAGE -t $IMAGE_URI -f Dockerfile .
|
|
|
|
|
else
|
|
|
|
|
docker build -t $IMAGE_URI -f Dockerfile .
|
|
|
|
|
fi
|
|
|
|
|
- docker tag $IMAGE_URI $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com.cn/$IMAGE_REPO_NAME:latest
|
|
|
|
|
post_build:
|
|
|
|
|
commands:
|
|
|
|
|
- docker push $IMAGE_URI
|
|
|
|
|
- docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com.cn/$IMAGE_REPO_NAME:latest
|
|
|
|
|
- echo Build completed on $(date)
|
|
|
|
|
- echo Image $IMAGE_URI
|
|
|
|
|
|
|
|
|
|
artifacts:
|
|
|
|
|
files:
|
|
|
|
|
- build.log
|
|
|
|
|
cache:
|
|
|
|
|
paths:
|
|
|
|
|
- '/root/.npm/**/*'
|
|
|
|
|
- 'frontend/node_modules/**/*'
|
|
|
|
|
|
|
|
|
|
env:
|
|
|
|
|
variables:
|
|
|
|
|
# 使用 ECR 中的 python-base 作为运行阶段基础镜像,避免 Docker Hub 超时及重复 pip 安装
|
|
|
|
|
PYTHON_BASE_IMAGE: "833395216036.dkr.ecr.cn-northwest-1.amazonaws.com.cn/python-base:latest"
|