

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 實作與 Elastic Beanstalk 環境的 CI/CD 整合
<a name="deployments.cicd"></a>

Elastic Beanstalk 與許多 CI/CD 工具整合，以自動化您的應用程式開發工作流程。CI/CD 實務可讓您以最少的手動介入，自動建置、測試和部署您的應用程式。持續交付/部署 (CD) 透過自動化部署程序來擴展持續整合 (CI)。您可以使用 CodePipeline 等 AWS 服務或第三方工具，例如 Jenkins 和 GitLab 來建立簡化的部署管道，以確保對 Elastic Beanstalk 環境進行一致且可靠的部署。

**Topics**
+ [AWS 要開始使用的來源](#deployments.cicd.aws-sites)
+ [其他資源](#deployments.cicd.aws-services.third-party)
+ [使用 GitHub 動作部署到 Elastic Beanstalk](deploying-github-actions.md)

## AWS 要開始使用的來源
<a name="deployments.cicd.aws-sites"></a>

下列清單重點介紹 CI/CD 工具和對應的 AWS 資源，這些資源提供step-by-step指引：
+ **AWS CodePipeline** – 本[AWS 入門資源中心](https://aws.amazon.com/getting-started/hands-on/continuous-deployment-pipeline/)教學課程說明如何從 GitHub 、S3 或 設定 Elastic Beanstalk 的持續部署管道 AWS CodeCommit。
+ **GitHub 動作** – 請參閱 以[使用 GitHub 動作部署到 Elastic Beanstalk](deploying-github-actions.md)了解如何設定 YAML 型工作流程，直接從 GitHub 設定 Elastic Beanstalk 的連續部署管道。
+ **GitLab** – 此 [AWS DevOps 開發人員生產力部落格](https://aws.amazon.com/blogs/devops/deploy-a-docker-application-on-aws-elastic-beanstalk-with-gitlab/)文章示範如何設定 GitLab 連續管道，將 Node.js 應用程式部署至 Elastic Beanstalk Docker 環境。
+ **Azure DevOps** – 此[AWS 部落格上的 .NET](https://aws.amazon.com/blogs/dotnet/deploy-to-elastic-beanstalk-with-azure-devops/) 文章會引導您使用 Azure Pipelines 將連續部署管道從 Azure DevOps Git 儲存庫實作至 Elastic Beanstalk。

## 其他資源
<a name="deployments.cicd.aws-services.third-party"></a>

下列第三方工具和資源可協助您將自動化部署管道實作到 Elastic Beanstalk 環境：
+ **Jenkins** – [AWS EBDeployment Jenkins 外掛程式](https://plugins.jenkins.io/awseb-deployment-plugin/)可讓您從 Jenkins 任務組態頁面直接部署到 Elastic Beanstalk 環境。
+ **圓形 CI：** – [Elastic Beanstalk 的 Orbs](https://circleci.com/developer/orbs/orb/circleci/aws-elastic-beanstalk) 提供可重複使用的組態套件，可將應用程式部署和擴展至 Elastic Beanstalk。
+ **Bitbucket Pipelines** – 文章[使用 Bitbucket Pipelines 部署 Elastic Beanstalk 應用程式](https://avishayil.medium.com/deploy-to-elastic-beanstalk-using-bitbucket-pipelines-189eb75cf052)提供使用 Elastic Beanstalk 實作 Bitbucket Pipelines 的基本組態範例。

# 使用 GitHub 動作部署到 Elastic Beanstalk
<a name="deploying-github-actions"></a>

當您將程式碼變更推送至儲存庫時，[GitHub 動作](https://docs.github.com/en/actions)可以自動將應用程式部署至 Elastic Beanstalk。[Elastic Beanstalk 部署](https://github.com/aws-actions/aws-elasticbeanstalk-deploy)動作提供簡單的 YAML 介面，可處理建立應用程式版本、將原始碼套件上傳至 Amazon S3，以及部署到您的 Elastic Beanstalk 環境。

## 範例工作流程
<a name="deploying-github-actions-example"></a>

下列範例工作流程會在每次您推送至`main`分支時，將應用程式部署至 Elastic Beanstalk 環境。在 `.github/workflows/` 下的儲存庫中建立`.yml`檔案。

**Example Elastic 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-role
          aws-region: us-east-1

      - name: Deploy to Elastic Beanstalk
        uses: aws-actions/aws-elasticbeanstalk-deploy@v1.0.0
        with:
          aws-region: us-east-1
          application-name: my-application
          environment-name: my-application-env
```

此工作流程會檢查您的儲存庫、使用 [OpenID Connect (OIDC)](https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services) AWS 透過[設定 AWS 登入](https://github.com/aws-actions/configure-aws-credentials)資料動作向 進行驗證，然後將您的應用程式部署到 Elastic Beanstalk。部署動作會封裝您的儲存庫內容、將原始碼套件上傳至 Amazon S3、建立新的應用程式版本，以及建立或更新您的環境。根據預設，它會等待部署完成，並等待環境回到運作狀態。

如需更多組態選項和進階範例，請參閱 GitHub 上的 [Elastic Beanstalk 部署動作 README](https://github.com/aws-actions/aws-elasticbeanstalk-deploy#readme)。

## 其他資源
<a name="deploying-github-actions-resources"></a>
+ 在 GitHub 上[部署 Elastic Beanstalk 動作](https://github.com/aws-actions/aws-elasticbeanstalk-deploy) 
+ 在 GitHub [上設定 AWS 登入資料動作](https://github.com/aws-actions/configure-aws-credentials) 
+ 在 [Amazon Web Services 中設定 OpenID Connect](https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services) (GitHub 文件）