testfork1/buildspec.yml

49 lines
2.3 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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)
# 需安装 devDependenciesvite、@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"