Close

使用 GitHub 部署 ImageLabeller

Warren Marusiak 头像
Warren Marusiak

高级技术传播者

为了演示如何使用 Jira Software 和各种互联工具开发、部署和管理应用,我们的团队创建了 ImageLabeller,这是一款基于 AWS 构建的简单演示应用,它使用机器学习将标签应用于图像。

本页面介绍如何使用 GitHub 部署 ImageLabeller。在您开始之前,我们建议阅读 ImageLabeller 架构AWS SageMaker 设置页面,以了解背景信息。

先决条件

如果您还没有 GitHub 组织,请按照本 GitHub 指南中的步骤从头开始创建一个。

使用 ImageLabeller 代码的面向公众的 GitHub 存储库

https://github.com/AtlassianOpenDevOpsGuides

Jira GitHub 集成演示视频

集成 Jira 和 GitHub

从 Jira 中,单击“看板”,再单击“应用”,然后单击“GitHub”。

JSW 应用下拉菜单

单击“立即获取”。

Jsw github 应用

单击“应用”,再单击“管理应用”,然后展开 GitHub。

JSW 管理应用图像

单击“开始使用”。

单击“开始使用”。

github 配置图像

在 GitHub 中安装 Jira 插件

单击“安装 Jira”,将 Jira 插件安装到 GitHub 中。如果 GitHub 中尚未安装 Jira 插件,则需要此步骤。

将 github 组织连接到 jira 图像

Jira 将开始从 GitHub 同步数据。

同步完成后,同步状态变更为“完成”。

同步状态变更屏幕截图

设置 SSH 访问权限

按照此处的说明为您的帐户设置 GitHub SSH 密钥。此处显示其他参考资料。

设置个人访问令牌

按照本指南设置个人访问令牌。在集成测试步骤中,PAT 用于克隆 SystemTest 代码存储库。授予 PAT 代码存储库工作流程访问权限。

为 AWS S3 基础架构创建存储库

标准的开发人员循环通常是让开发人员从 Jira 中接取任务,将其移至正在进行的工作中,然后进行开发工作。Jira 事务 ID 是将开发工作与 Jira 事务联系起来的关键。它是两个系统之间的核心集成组件。

转到 Jira,创建一个新事务,将 AWS S3 基础架构存储库添加到 GitHub。记下事务 ID。本例中为 IM-9。

AWS S3 基础架构

转到 GitHub 并单击“新建”。为“负责人”选择合适的组织。单击“创建存储库”以继续。

添加 AWS 访问密钥存储库变量

单击“设置”,然后单击“密钥”。单击“新存储库密钥”,然后添加 AWS 访问密钥 ID 和 AWS 秘密访问密钥。向与 AWS 访问密钥关联的 IAM 用户授予管理员访问权限。您可以通过选择个别 AWS 访问策略来选择使用更精细的访问控制,但详情留给读者。

在您的终端中转到您的 s3_infra 存储库,并运行以下命令以推送到 GitHub。

git add --all
git commit -m "IM-9 add s3_infra repository to github"
git remote add origin git@github.com:PmmQuickStartGuides01/s3_infra.git
git branch -m mainline
git push -u origin mainline

用于部署到 AWS 的 GitHub 操作

在您的终端中转到 s3_infra 存储库,创建一个以您的 Jira 事务 ID 命名的分支,并创建一个 .github/workflows 目录。

git checkout -b IM-9
mkdir -p .github/workflows && cd .github/workflows

在新的 .github/workflows 中使用以下 yaml 创建 deploy-test-staging.yml 目录。这为您的测试和暂存环境定义了部署工作流程,该工作流程在推送到主线以外的分支时运行。

name: deploy-s3-infra-test-staging
on:
  push:
    branches:
      - '*'
      - '!mainline'

jobs:
  deploy-us-west-1:
    runs-on: ubuntu-latest
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-west-1"
      - name: Deploy to AWS CloudFormation
        uses: aws-actions/aws-cloudformation-github-deploy@v1
        with:
          name: OpenDevOpsS3Infra
          template: template.yml
          no-fail-on-empty-changeset: "1"

  deploy-us-east-2:
    runs-on: ubuntu-latest
    needs: deploy-us-west-1
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-2"
      - name: Deploy to AWS CloudFormation
        uses: aws-actions/aws-cloudformation-github-deploy@v1
        with:
          name: OpenDevOpsS3Infra
          template: template.yml
          no-fail-on-empty-changeset: "1"

然后使用以下 yaml 创建 deploy-prod.yml。这为您的生产环境定义了部署工作流程,该工作流程在拉取请求将变更合并到主线中时运行。

name: deploy-s3-infra-prod
on:
  pull_request:
    branches:
      - mainline

jobs:
  deploy-us-west-2:
    runs-on: ubuntu-latest
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-west-2"
      - name: Deploy to AWS CloudFormation
        uses: aws-actions/aws-cloudformation-github-deploy@v1
        with:
          name: OpenDevOpsS3Infra
          template: template.yml
          no-fail-on-empty-changeset: "1"

  deploy-ca-central-1:
    runs-on: ubuntu-latest
    needs: deploy-us-west-2
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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: "ca-central-1"
      - name: Deploy to AWS CloudFormation
        uses: aws-actions/aws-cloudformation-github-deploy@v1
        with:
          name: OpenDevOpsS3Infra
          template: template.yml
          no-fail-on-empty-changeset: "1"

  deploy-us-east-1:
    runs-on: ubuntu-latest
    needs: deploy-ca-central-1
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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: Deploy to AWS CloudFormation
        uses: aws-actions/aws-cloudformation-github-deploy@v1
        with:
          name: OpenDevOpsS3Infra
          template: template.yml
          no-fail-on-empty-changeset: "1"

了解 GitHub 操作

触发

在这些文件的顶部声明触发工作流程的事件。在 deploy-test-staging.yml 中,该事件被推送到除主线以外的所有分支。

name: deploy-s3-infra-test-staging
on:
  push:
    branches:
      - '*'
      - '!mainline'

许多事件可以触发工作流程。更多信息,请参阅此处的文档。

工作

工作流程包含许多在触发事件发生时运行的作业。每个作业都有一组在执行作业时运行的步骤。在此示例中,有一个步骤用于签出存储库代码,一个步骤用于配置 AWS 凭证,以及一个步骤用于使用 AWS CloudFormation 部署到 AWS。

jobs:
  deploy-us-west-1:
    runs-on: ubuntu-latest
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-west-1"
      - name: Deploy to AWS CloudFormation
        uses: aws-actions/aws-cloudformation-github-deploy@v1
        with:
          name: OpenDevOpsS3Infra
          template: template.yml
          no-fail-on-empty-changeset: "1"

此处阅读有关作业的更多信息,在此处阅读有关步骤的更多信息。

使用需求 (needs) 强制执行顺序

您可以使用需求为作业指定顺序。默认,GitHub 会并行运行所有步骤。使用 需求让一个步骤依赖于另一个步骤。

deploy-us-east-1:
    runs-on: ubuntu-latest
    needs: deploy-ca-central-1
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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: Deploy to AWS CloudFormation
        uses: aws-actions/aws-cloudformation-github-deploy@v1
        with:
          name: OpenDevOpsS3Infra
          template: template.yml
          no-fail-on-empty-changeset: "1"

推送到功能分支

从命令行运行以下命令,将代码推送到您的 s3_infra 存储库的 IM-9 分支。

git add --all
git commit -m "IM-9 add GitHub actions to s3_infra"
git push -u origin IM-9

单击“操作”以查看正在运行的工作流程。

正在运行的工作流程图像

创建拉取请求

单击“拉取请求”,然后单击“创建拉取请求”。

比较变更图像

选择您的功能分支作为源分支,然后单击“创建拉取请求”。

创建拉取请求图像

审查代码变更,然后单击“合并拉取请求”,以接受变更。

单击“操作”以查看生产部署是否已开始。GitHub 在 deploy-prod.yml 中运行作业,因为分支是主线。

为 SubmitImage AWS Lambda 创建存储库

转到 Jira 并创建一个 Jira 事务,将 SubmitImage AWS Lambda 存储库添加到 GitHub。记下 Jira 事务 ID。本例中为 IM-8。

IM 看板图像

转到 GitHub 并单击“新建”。为“负责人”选择合适的组织。单击“创建存储库”以继续。

单击“设置”,然后单击“密钥”。将您的个人访问令牌添加为 ACCESS_KEY,将您的 AWS 访问密钥 ID 添加为 AWS_ACCESS_KEY_ID,将您的 AWS 秘密访问密钥添加为 AWS_SECRET_ACCESS_KEY,将您的 AWS 帐户 ID 添加为 AWS_ACCUNT_ID。

在您的终端中转到您的 SubmitImage 存储库,并运行以下命令以将您的代码推送到 GitHub。

git add --all
git commit -m "IM-8 add SubmitImage to github"
git remote add origin git@github.com:PmmQuickStartGuides01/submitImage.git
git branch -m mainline
git push -u origin mainline
用于部署到 AWS 的 GitHub 操作

在您的终端中转到 SubmitImage 存储库,创建一个以您的 Jira 事务 ID 命名的分支,并创建一个 .github/workflows 目录。

git checkout -b IM-8
mkdir -p .github/workflows && cd .github/workflows

在新的 .github/workflows 中使用以下 yaml 创建 deploy-test-staging.yml 目录。这为您的测试和暂存环境定义了部署工作流程,该工作流程在推送到主线以外的分支时运行。您必须更新 git clone 行才能让 SystemTests 成为您的 SystemTests 存储库。

name: deploy-submit-image-test-staging
on:
  push:
    branches:
      - '*'
      - '!mainline'

env:
  aws_account_id: ${{secrets.AWS_ACCOUNT_ID}}

jobs:
  run-unit-tests:
    runs-on: ubuntu-latest
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Test
        run: |
          cd ${{ github.workspace }}/submitImage
          ls
          go test ./opendevopslambda...

  deploy-us-west-1:
    runs-on: ubuntu-latest
    needs: run-unit-tests
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-west-1"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsSubmitImage" \
            --s3-bucket "open-devops-code-us-west-1-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-us-west-1:
            #    runs-on: ubuntu-latest
            #    needs: deploy-us-west-1
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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-west-1"
            #      - name: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=us-west-1

  deploy-us-east-2:
    runs-on: ubuntu-latest
    needs: deploy-us-west-1
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-2"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsSubmitImage" \
            --s3-bucket "open-devops-code-us-east-2-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-us-east-2:
            #    runs-on: ubuntu-latest
            #    needs: deploy-us-east-2
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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-2"
            #      - name: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=us-east-2

然后使用以下 yaml 创建 deploy-prod.yml。这为您的生产环境定义了部署工作流程,该工作流程在拉取请求将变更合并到主线中时运行。

name: deploy-submit-image-prod
on:
  pull_request:
    branches:
      - mainline

env:
  aws_account_id: ${{secrets.AWS_ACCOUNT_ID}}

jobs:
  deploy-us-west-2:
    runs-on: ubuntu-latest
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-west-2"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsSubmitImage" \
            --s3-bucket "open-devops-code-us-west-2-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-us-west-2:
            #    runs-on: ubuntu-latest
            #    needs: deploy-us-west-2
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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-west-2"
            #      - name: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=us-west-2

  deploy-us-east-1:
    runs-on: ubuntu-latest
    needs: deploy-us-west-2
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsSubmitImage" \
            --s3-bucket "open-devops-code-us-east-1-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-us-east-1:
            #    runs-on: ubuntu-latest
            #    needs: deploy-us-east-1
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=us-east-1

  deploy-ca-central-1:
    runs-on: ubuntu-latest
    needs: deploy-us-east-1
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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: "ca-central-1"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true

        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsSubmitImage" \
            --s3-bucket "open-devops-code-ca-central-1-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-ca-central-1:
            #    runs-on: ubuntu-latest
            #    needs: deploy-ca-central-1
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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: "ca-central-1"
            #      - name: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=ca-central-1

集成测试的执行目前已被注释掉。只有在部署整个应用时,系统测试才会通过。取消对存储库中的集成测试步骤的注释,并在部署 ImageLabeller 的所有组件后再次推送以运行部署管道。更新“需求”部分以按顺序运行这些步骤。您必须更新 git clone 行才能让 SystemTests 成为您的 SystemTests 存储库。

needs: deploy-us-east-1
了解 GitHub 操作

此作业使用 AWS SAM 将您的 SubmitImage AWS Lambda 部署到 us-west-2。

deploy-us-west-2:
    runs-on: ubuntu-latest
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-west-2"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsSubmitImage" \
            --s3-bucket "open-devops-code-us-west-2-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

此作业克隆 SystemTests 存储库,并在 us-west-2 中运行集成测试。

integration-test-us-west-2:
    runs-on: ubuntu-latest
    needs: deploy-us-west-2
    steps:
      - name: Pull systemTests repo
        uses: actions/checkout@master
        with:
          repository: PmmQuickStartGuides01/systemTests
          token: ${{ secrets.ACCESS_KEY }}
      - name: Configure AWS credentials
        id: creds
        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-west-2"
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - name: Run integration tests
        run: go test -v ./... -aws_region=us-west-2

此步骤使用个人访问令牌来克隆 SystemTests 存储库。

    - name: Pull systemTests repo
        uses: actions/checkout@master
        with:
          repository: PmmQuickStartGuides01/systemTests
          token: ${{ secrets.ACCESS_KEY }}
推送到功能分支

从命令行运行以下命令,将您的变更推送到 SubmitImage 存储库的 IM-8 分支。在提交消息中包含 Jira 事务 ID 和分支名称,以使 Jira GitHub 集成能够跟踪您的项目中正在发生的情况。

git add --all
git commit -m "IM-8 add github actions to SubmitImage"
git push -u origin IM-8

单击“操作”以查看正在运行的工作流程。

请注意,单元测试作业已通过,并已开始部署到测试 us-west-1。

前面定义的系统测试作为 integration-test-us-west-1 和 integration-test-us-east-2 作业的一部分运行。

创建拉取请求

如需创建拉取请求,请单击“拉取请求”,然后单击“新建拉取请求”。

从您的功能分支中选择合并。

单击 Create Pull Request(创建拉取请求)

合并拉取请求,然后删除功能分支。单击“操作”以监视生产部署。

为 InvokeLabeller AWS Lambda 创建存储库

转到 Jira 并创建一个 Jira 事务,将 InvokeLabeller AWS Lambda 存储库添加到 GitHub。记下 Jira 事务 ID。本例中为 IM-11。

转到 GitHub 并单击“新建”。为“负责人”选择合适的组织。单击“创建存储库”以继续。

单击“设置”,然后单击“密钥”。将您的个人访问令牌添加为 ACCESS_KEY,将您的 AWS 访问密钥 ID 添加为 AWS_ACCESS_KEY_ID,将您的 AWS 秘密访问密钥添加为 AWS_SECRET_ACCESS_KEY,将您的 AWS 帐户 ID 添加为 AWS_ACCUNT_ID。

在您的终端中转到您的 InvokeLabeller 存储库,并运行以下命令以将您的代码推送到 GitHub。

git add --all
git commit -m "IM-11 add InvokeLabeller to github"
git remote add origin git@github.com:PmmQuickStartGuides01/InvokeLabeller.git
git branch -m mainline
git push -u origin mainline
用于部署到 AWS 的 GitHub 操作

在您的终端中转到 InvokeLabeller 存储库,创建一个以您的 Jira 事务 ID 命名的分支,并创建一个 .github/workflows 目录。

git checkout -b IM-11
mkdir -p .github/workflows && cd .github/workflows

在新的 .github/workflows 中使用以下 yaml 创建 deploy-test-staging.yml 目录。这为您的测试和暂存环境定义了部署工作流程,该工作流程在推送到主线以外的分支时运行。您必须更新 git clone 行才能让 SystemTests 成为您的 SystemTests 存储库。

name: deploy-invoke-labeller-test-staging
on:
  push:
    branches:
      - '*'
      - '!mainline'

env:
  aws_account_id: ${{secrets.AWS_ACCOUNT_ID}}

jobs:
  run-unit-tests:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      - name: Test
        run: |
          pip3 install pytest
          pip3 install moto
          pip3 install -r tst/requirements.txt --user
          python3 -m pytest -v tst/unit --junitxml=test-reports/report.xml

  deploy-us-west-1:
    runs-on: ubuntu-latest
    needs: run-unit-tests
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-west-1"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsImageLabeller" \
            --s3-bucket "open-devops-code-us-west-1-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-us-west-1:
            #    runs-on: ubuntu-latest
            #    needs: deploy-us-west-1
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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-west-1"
            #      - name: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=us-west-1

  deploy-us-east-2:
    runs-on: ubuntu-latest
    needs: deploy-us-west-1
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-2"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsImageLabeller" \
            --s3-bucket "open-devops-code-us-east-2-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-us-east-2:
            #    runs-on: ubuntu-latest
            #    needs: deploy-us-east-2
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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-2"
            #      - name: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=us-east-2

然后使用以下 yaml 创建 deploy-prod.yml。这为您的生产环境定义了部署工作流程,该工作流程在拉取请求将变更合并到主线中时运行。

name: deploy-invoke-labeller-prod
on:
  pull_request:
    branches:
      - mainline

env:
  aws_account_id: ${{secrets.AWS_ACCOUNT_ID}}

jobs:
  deploy-us-west-2:
    runs-on: ubuntu-latest
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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-west-2"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsImageLabeller" \
            --s3-bucket "open-devops-code-us-west-2-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-us-west-2:
            #    runs-on: ubuntu-latest
            #    needs: deploy-us-west-2
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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-west-2"
            #      - name: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=us-west-2

  deploy-us-east-1:
    runs-on: ubuntu-latest
    needs: deploy-us-west-2
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true
        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsImageLabeller" \
            --s3-bucket "open-devops-code-us-east-1-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-us-east-1:
            #    runs-on: ubuntu-latest
            #    needs: deploy-us-east-1
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=us-east-1

  deploy-ca-central-1:
    runs-on: ubuntu-latest
    needs: deploy-us-east-1
    outputs:
      env-name: ${{ steps.env-name.outputs.environment }}
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.16.x
      - uses: actions/checkout@v2
      - name: Configure AWS credentials
        id: creds
        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: "ca-central-1"
      - name: SAM Validate
        run: |
          sam --version
          sam validate
      - name: SAM Build
        run: |
          sam build
      - name: SAM Deploy
        continue-on-error: true

        run: |
          sam deploy --template-file .aws-sam/build/template.yaml \
            --stack-name "OpenDevOpsImageLabeller" \
            --s3-bucket "open-devops-code-ca-central-1-${aws_account_id}" \
            --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM

            #  integration-test-ca-central-1:
            #    runs-on: ubuntu-latest
            #    needs: deploy-ca-central-1
            #    steps:
            #      - name: Pull systemTests repo
            #        uses: actions/checkout@master
            #        with:
            #          repository: PmmQuickStartGuides01/systemTests
            #          token: ${{ secrets.ACCESS_KEY }}
            #      - name: Configure AWS credentials
            #        id: creds
            #        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: "ca-central-1"
            #      - name: Install Go
            #        uses: actions/setup-go@v2
            #        with:
            #          go-version: 1.16.x
            #      - name: Run integration tests
            #        run: go test -v ./... -aws_region=ca-central-1

集成测试的执行目前已被注释掉。只有在部署整个应用时,系统测试才会通过。取消对存储库中的集成测试步骤的注释,并在部署 ImageLabeller 的所有组件后再次推送以运行部署管道。更新“需求”部分以按顺序运行这些步骤。您必须更新 git clone 行才能让 SystemTests 成为您的 SystemTests 存储库。

needs: deploy-us-east-1
使用 AWS SageMaker 端点更新 src/app.py

打开 InvokeLabeller 的 src/app.py 文件,并查找 query_endpoint。变更 endpoint_name 和客户端 region_name 以匹配您的 AWS SageMaker 记事本。

def query_endpoint(img):
  endpoint_name = 'jumpstart-dft-image-labeller-endpoint'
  client = boto3.client(service_name='runtime.sagemaker', region_name='us-west-1')
  response = client.invoke_endpoint(EndpointName=endpoint_name, ContentType='application/x-image', Body=img)
  model_predictions = json.loads(response['Body'].read())['predictions'][0]
  return model_predictions
推送到功能分支

从命令行运行以下命令,将您的变更推送到 InvokeLabeller 存储库的 IM-11 分支。在提交消息中包含 Jira 事务 ID 和分支名称,以使 Jira GitHub 集成能够跟踪您的项目中正在发生的情况。

git add --all
git commit -m "IM-11 add github actions to InvokeLabeller"
git push -u origin IM-11

单击“操作”以查看正在运行的工作流程。前面定义的系统测试作为 integration-test-us-west-1 和 integration-test-us-east-2 作业的一部分运行。

创建拉取请求

如需创建拉取请求,请单击“拉取请求”,然后单击“新建拉取请求”。从您的功能分支中选择合并。

单击“操作”以监视生产部署。

为 SystemTests 创建存储库

转到 Jira 并创建一个 Jira 事务,将 SystemTests 存储库添加到 GitHub。记下 Jira 事务 ID。本例中为 IM-7。

转到 GitHub 并单击“新建”。为“负责人”选择合适的组织。单击“创建存储库”以继续。

在您的终端中转到您的 SystemTests 存储库,并运行以下命令以将您的代码推送到 GitHub。

git add --all
git commit -m "IM-7 add SystemTests repository to GitHub"
git remote add origin git@github.com:PmmQuickStartGuides01/systemTests.git
git branch -M mainline
git push -u origin mainline

SystemTests 存储库不需要 GitHub 操作。它没有自己的管道,因为它为其他管道提供测试使其运行。部署了 ImageLabeller 应用的所有组件后,可以取消注释、提交和推送 CI/CD 工作流程文件的集成测试步骤。只有应用的所有组件都正常运行时,测试才会通过。

记下您的 SystemTest 的远程 URL。SubmitImage、GetImageLabel 和 InvokeLabeller CI/CD 管道将在测试步骤中克隆 SystemTests 存储库。您需要使用正确的 URL 更新后续存储库的 gitlab-ci.yml。

如果您已经做到这一步,恭喜!您刚刚已部署 ImageLabeller。下一步是设置使用 Opsgenie 监控 ImageLabeller。

Warren Marusiak
Warren Marusiak

Warren is a Canadian developer from Vancouver, BC with over 10 years of experience. He came to Atlassian from AWS in January of 2021.


分享这篇文章

推荐阅读

将这些资源加入书签,以了解 DevOps 团队的类型,或获取 Atlassian 关于 DevOps 的持续更新。

Devops 示意图

DevOps 社区

Devops 示意图

DevOps 学习路径

地图插图

免费试用

注册以获取我们的 DevOps 新闻资讯

Thank you for signing up