AWS 使用 CodeBuild 和 GitHub Action workflows 实现自动构建镜像及推送到 ECR
AWS GitHub DevOps About 1,211 words为什么使用 CodeBuild
公司的GitHub Enterprise管理员禁用了GitHub提供的运行机器。
GitHub Actions hosted runners are disabled for this repository. For more information please contact your GitHub Enterprise Administrator.
新建 CodeBuild
Project configuration
选择Runner Project。
Runner
选择GitHub。并且进行GitHub授权,可以选择GitHub App或OAuth等方式。
选择Repository。
复制Runner Configuration Sample(后面拷贝到GitHub workflows的ecr.yaml里)
其他
其他选择默认即可。
IAM
然后到IAM的Role里,找到codebuild-<project-name>-service-role的角色,添加AmazonEC2ContainerRegistryPowerUser权限即可。
ecr.yaml
新建.github文件夹,在.github文件下新建workflows文件夹(注意单词拼写,否则无法触发GitHub Action)
完整路径:.github/workflows/ecr.yaml
name: Build and Push to ECR
on:
push:
branches: [main]
workflow_dispatch:
env:
ECR_REPO: ${{ github.event.repository.name }}
jobs:
build:
runs-on: codebuild-gh-runner-my-app-${{ github.run_id }}-${{ github.run_attempt }}
steps:
- uses: actions/checkout@v4
- id: ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build & Push
env:
REGISTRY: ${{ steps.ecr.outputs.registry }}
run: |
docker build -t $REGISTRY/$ECR_REPO:${{ github.sha }} -t $REGISTRY/$ECR_REPO:latest .
docker push --all-tags $REGISTRY/$ECR_REPO
Views: 9 · Posted: 2026-10-10
———         Thanks for Reading         ———
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...