![如何在多种环境中使用 github 操作部署到 AWS elastic beanstalk?(开发、暂存、生产)](https://linux22.com/image/1635394/%E5%A6%82%E4%BD%95%E5%9C%A8%E5%A4%9A%E7%A7%8D%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8%20github%20%E6%93%8D%E4%BD%9C%E9%83%A8%E7%BD%B2%E5%88%B0%20AWS%20elastic%20beanstalk%EF%BC%9F%EF%BC%88%E5%BC%80%E5%8F%91%E3%80%81%E6%9A%82%E5%AD%98%E3%80%81%E7%94%9F%E4%BA%A7%EF%BC%89.png)
我正在使用此 github 操作从 Node 应用程序构建 docker 映像,然后通过 ECR 将其部署到 elastic beanstalk。
这对于应用程序的单一环境来说很好,但是添加暂存和生产呢?由于应用程序版本对于应用程序的所有环境都是相同的,我应该用and还是什么来
标记版本?staging
prod
name: Deploy Docker Image to Amazon ElasticBeanstalk - Development
on:
push:
branches:
- development
jobs:
deploy:
name: Build And Deploy to AWS
runs-on: ubuntu-latest
steps:
- uses: chrislennon/[email protected]
- name: Checkout
uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Get App Version
id: get-version
run: |
versionValue=$(cat package.json | jq '.version')
appVersion="${versionValue%\"}"
appVersion="${appVersion#\"}"
echo "::set-output name=app-version::$appVersion"
- name: Get Branch name
id: get-branch
run: echo ::set-output name=branch-name::${GITHUB_REF#refs/*/}
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: vennly-api
IMAGE_TAG: v${{ steps.get-version.outputs.app-version }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Generate Deploy File
env:
IMAGE_NAME: ${{ steps.build-image.outputs.image }}
ZIP_FILE: v${{ steps.get-version.outputs.app-version }}
run: |
envsubst < Dockerrun.template.json > Dockerrun.aws.json
zip -r $ZIP_FILE.zip Dockerrun.aws.json
- name: Deploy File
env:
ZIP_FILE: v${{ steps.get-version.outputs.app-version }}.zip
APP_VERSION: v${{ steps.get-version.outputs.app-version }}
run: |
aws s3 cp $ZIP_FILE s3://elasticbeanstalk-vennly-api-versions
aws elasticbeanstalk create-application-version --application-name vennly-api \
--version-label $APP_VERSION \
--source-bundle S3Bucket=elasticbeanstalk-vennly-api-versions,S3Key=$ZIP_FILE \
--description "${{ github.event.head_commit.message }}"
aws elasticbeanstalk update-environment --application-name vennly-api \
--environment-name vennly-api-${{ steps.get-branch.outputs.branch-name }} \
--solution-stack-name "64bit Amazon Linux 2 v3.1.0 running Docker" \
--version-label $APP_VERSION
- name: Logout of Amazon ECR
if: always()
run: docker logout ${{ steps.login-ecr.outputs.registry }}
答案1
您可以创建多个工作流程并调整触发器以匹配分支:
on:
push:
branches:
- staging
on:
push:
branches:
- prod
只需确保根据您在 Elastic Beanstalk 中的设置方式瞄准正确的环境/应用程序即可。