GitHub Actions 是 GitHub 提供的一项持续集成/持续部署服务,可帮助您自动化软件开发工作流程。结合 AWS 服务,您可以在 GitHub Actions 工作流程中访问和管理 AWS 资源,从而实现更高效的开发和部署流程。

[参考](https://docs.github.com/zh/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services)

步骤

1. 创建 IAM 用户

在 AWS 控制台中创建一个 IAM 用户,用于 GitHub Actions 访问 AWS 资源。为该用户分配最小权限,只授予工作流程所需的权限。

2. 生成访问密钥

为 IAM 用户生成访问密钥(Access Key ID 和 Secret Access Key)。这些凭据将用于 GitHub Actions 与 AWS 进行通信。

3. 存储凭据

将 IAM 用户的访问密钥添加到 GitHub 存储库的 Secrets 中。您可以通过访问 GitHub 存储库的 Settings > Secrets 菜单完成此操作。

4. 配置 GitHub Actions 工作流程

在 GitHub 存储库中创建或编辑工作流程文件(例如 .github/workflows/main.yml),并定义需要访问 AWS 资源的工作流程步骤。在这些步骤中,您可以使用存储在 GitHub Secrets 中的 AWS 凭据。

5. 测试和部署

测试您的 GitHub Actions 工作流程,确保它可以顺利地访问和使用 AWS 资源。一旦测试通过,您就可以将工作流程部署到 GitHub 存储库中,使其自动化执行。

示例工作流程

以下是一个示例 GitHub Actions 工作流程,展示了如何使用 AWS 凭据执行一些操作:

name: 'test AWS Access'

# The workflow should only trigger on pull requests to the main branch
on: 
  pull_request:
    branches:
      - main

jobs:
  deploy:
    name: test AWS Access
    runs-on: ubuntu-latest
    steps:
      - 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: ${{ env.AWS_DEFAULT_REGION }}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

在这个示例中:

  • on: pull_request 定义了工作流程的触发条件,这里配置为仅在主分支上的 pull request 触发。
  • aws-actions/configure-aws-credentials@v1 是一个 GitHub Action,用于配置 AWS 凭据。
  • ${{ secrets.AWS_ACCESS_KEY_ID }} 和 ${{ secrets.AWS_SECRET_ACCESS_KEY }} 是存储在 GitHub Secrets 中的 AWS 凭据。
  • ${{ env.AWS_DEFAULT_REGION }} 是环境变量,存储了 AWS 的默认区域。

添加 GitHub 操作作为身份提供者

为了将 GitHub 操作作为身份提供者添加到 AWS,您需要执行以下步骤:

  1. 对于提供商类型,选择 OpenID Connect。
  2. 对于 Provider URL,输入 https://token.actions.githubusercontent.com
  3. 单击“获取指纹”以获取提供者的指纹。
  4. 对于受众,请输入 sts.amazonaws.com

创建并配置角色

使用 github 操作所需的权限和信任提供者的信任策略创建一个新角色,例如:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Federated": "arn:aws:iam::0123456789:oidc-provider/token.actions.githubusercontent.com"
      },
      "Action": "sts:AssumeRoleWithWebIdentity",
      "Condition": {
        "StringEquals": {
          "token.actions.githubusercontent.com:sub": [
            "repo:ORG_OR_USER_NAME/REPOSITORY:pull_request",
            "repo:ORG_OR_USER_NAME/REPOSITORY:ref:refs/heads/main"
          ],
          "token.actions.githubusercontent.com:aud": "sts.amazonaws.com"
        }
      }
    }
  ]
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

在这个策略中,指定了仅使用特定触发器对组织存储库中的分支进行授权。

使用 GitHub 操作配置 AWS 凭证

最后,在工作流程中使用 GitHub 操作来配置要使用的 AWS 凭据:

name: 'test AWS Access'

# The workflow should only trigger on pull requests to the main branch
on:
  pull_request:
    branches:
      - main

# Required to get the ID Token that will be used for OIDC
permissions:
  id-token: write
  contents: read # needed for private repos to checkout

jobs:
  aws:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: eu-west-1
          role-to-assume: ${{ secrets.READ_ROLE }}
          role-session-name: OIDCSession

      - run: aws sts get-caller-identity
        shell: bash
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

在这个工作流程中,GitHub 操作将使用 AWS 凭据执行一些 AWS 命令。

通过这个整体流程,您可以成功授权 GitHub Actions 并与 AWS 服务集成,实现自动化的软件开发和部署流程。