本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 GitHub 動作部署到 Elastic Beanstalk
當您將程式碼變更推送至儲存庫時,GitHub 動作
工作流程範例:Beanstalk 標準環境
下列範例工作流程會在您每次推送至main分支時,將應用程式部署至 Beanstalk 標準環境。在 下的儲存庫中建立.yml檔案.github/workflows/。如果環境不存在, 動作會建立環境。在這種情況下, solution-stack-name(或 platform-arn) 會選取平台,並且option-settings必須提供 中所述的執行個體描述檔和服務角色Elastic Beanstalk Service 角色、執行個體描述檔和使用者政策。當環境已存在時,這些輸入是選用的。
範例 Beanstalk 標準環境的 GitHub 動作工作流程
name: Deploy to Elastic Beanstalk on: push: branches: - main permissions: id-token: write contents: read jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::123456789012:role/my-github-actions-roleaws-region:us-east-1- name: Deploy to Elastic Beanstalk uses: aws-actions/aws-elasticbeanstalk-deploy@v1 with: aws-region:us-east-1application-name:my-applicationenvironment-name:my-application-envsolution-stack-name: '64bit Amazon Linux 2023 v4.10.0 running Go 1' option-settings: | [ { "Namespace": "aws:autoscaling:launchconfiguration", "OptionName": "IamInstanceProfile", "Value": "aws-elasticbeanstalk-ec2-role" }, { "Namespace": "aws:elasticbeanstalk:environment", "OptionName": "ServiceRole", "Value": "aws-elasticbeanstalk-service-role" } ]
此工作流程會檢查您的儲存庫,使用 OpenID Connect (OIDC)
工作流程範例:Beanstalk 叢集環境
Beanstalk 叢集環境會執行容器映像,因此無法自行部署原始碼套件。若要部署至 Beanstalk 叢集環境,請設定下列其中一個輸入,而非解決方案堆疊或平台 ARN:
-
image-uri– 您的工作流程已建立並推送至登錄檔的容器映像的 URI,例如 Amazon Elastic Container Registry (Amazon ECR)。動作會直接從映像建立應用程式版本,而不會將原始碼套件上傳至 Amazon S3。 -
build-configuration– JSON 物件,告知 Elastic Beanstalk 如何從來源建置映像,包括 AWS CodeBuild 擔任CodeBuildServiceRole的 和建置類型。動作會將您的原始碼套件上傳至 Amazon S3,使用建置組態建立應用程式版本,並等待建置完成再部署。如需建置設定,請參閱 為 Beanstalk 叢集環境建置容器映像。
下列工作流程範例會建置映像、將其推送至 Amazon ECR,並將其部署至 Beanstalk 叢集環境。如果環境不存在, 動作會建立環境。在這種情況下, option-settings 必須提供 中所述的叢集、節點和可觀測性角色Beanstalk 叢集的許可,而且必須允許 GitHub Actions 擔任的角色傳遞這些角色。
範例 Beanstalk 叢集環境的 GitHub 動作工作流程
name: Deploy to Elastic Beanstalk on: push: branches: - main permissions: id-token: write contents: read jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::123456789012:role/my-github-actions-roleaws-region:us-east-1- name: Login to Amazon ECR id: ecr uses: aws-actions/amazon-ecr-login@v2 - name: Build and push container image run: | docker build --platform linux/amd64 -t ${{ steps.ecr.outputs.registry }}/my-app:${{ github.sha }} . docker push ${{ steps.ecr.outputs.registry }}/my-app:${{ github.sha }} - name: Deploy to Elastic Beanstalk uses: aws-actions/aws-elasticbeanstalk-deploy@v1 with: aws-region:us-east-1application-name:my-cluster-appenvironment-name:my-cluster-envimage-uri: ${{ steps.ecr.outputs.registry }}/my-app:${{ github.sha }} option-settings: | [ { "Namespace": "aws:elasticbeanstalk:eks", "OptionName": "cluster-role", "Value": "${{ secrets.CLUSTER_ROLE_ARN }}" }, { "Namespace": "aws:elasticbeanstalk:eks", "OptionName": "node-role", "Value": "${{ secrets.NODE_ROLE_ARN }}" }, { "Namespace": "aws:elasticbeanstalk:eks:environment", "OptionName": "observability-role", "Value": "${{ secrets.OBSERVABILITY_ROLE_ARN }}" } ]
設定 build-configuration image-uri或 時,deployment-timeout輸入預設為 2400 秒,而不是 900 秒。一組子網路上的第一個 Beanstalk 叢集環境會佈建 Amazon EKS 叢集,這需要 15-20 分鐘的時間;相同子網路上的稍後環境會重複使用叢集,並在幾分鐘內部署。
如需所需許可、build-configuration範例、輸入的完整清單,以及兩種環境類型的更多組態選項,請參閱 GitHub 上的 Elastic Beanstalk 部署動作 README
其他資源
-
在 GitHub 上部署 Elastic Beanstalk 動作
-
在 GitHub 上設定 AWS 登入資料動作
-
在 Amazon Web Services 中設定 OpenID Connect
(GitHub 文件)