如何在多种环境中使用 github 操作部署到 AWS elastic beanstalk?(开发、暂存、生产)

如何在多种环境中使用 github 操作部署到 AWS elastic beanstalk?(开发、暂存、生产)

我正在使用此 github 操作从 Node 应用程序构建 docker 映像,然后通过 ECR 将其部署到 elastic beanstalk。
这对于应用程序的单一环境来说很好,但是添加暂存和生产呢?由于应用程序版本对于应用程序的所有环境都是相同的,我应该用and还是什么来
标记版本?stagingprod

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 中的设置方式瞄准正确的环境/应用程序即可。

相关内容