此存储库包含一个CDK堆栈,该堆栈在您的AWS帐户中为GitHub中的CI/CD集成设置Codeguru评论者GitHub Action。它为使用Codeguru审稿人设置了正确的权限,并创建了一个保存代码并建立codeguru Reivewer分析的S3存储桶。
如果您没有安装打字稿的CDK,请在此处按照说明进行操作,并确保正确设置凭据,以便可以使用CDK部署。这些步骤包括:
npm install typescript aws-cdk
一旦正确设置了所有内容,请获取依赖项并编译:
git clone https://github.com/aws-samples/aws-codeguru-reviewer-cicd-cdk-sample.git
cd aws-codeguru-reviewer-cicd-cdk-sample
npm install
npm run build
在文件./cdk.json中,添加所有GitHub存储库,应允许在allowedGithubRepos列表中使用Codeguru Reviewer。例如:
"allowedGithubRepos": ["aws-samples/*", "awslabs/smithy"]
允许组织中的所有存储库aws-sample和存储库awslabs/smithy使用Codeguru审稿人。
更新了allowedGithubRepos GithubRepos后,您需要Bootstrap CDK并部署堆栈。
运行命令:
npx cdk bootstrap --profile {PROFILE_NAME} "aws://unknown-account/unknown-region"
~/.aws/config文件中的命名配置文件之一替换PROFILE_NAME 。"aws://unknown-account/unknown-region" 。运行命令:
npx cdk deploy --profile {PROFILE_NAME}
~/.aws/config文件中的命名配置文件之一替换PROFILE_NAME 。 部署完成后,您将收到与此类似的输出:
✅ GuruCdkSetupStack
Outputs:
GuruCdkSetupStack.Role = arn:aws:iam::123456789012:role/GitHubActionRole
GuruCdkSetupStack.Region = us-east-1
GuruCdkSetupStack.Bucket = codeguru-reviewer-build-artifacts-123456789012-us-east-1
您将在GitHub工作流中需要此信息:
configure-aws-credentials操作时,您将使用角色ARN和区域。codeguru-reviewer Action时,您将使用存储牌名称。 您可以将以下模板之一用于工作流程:
name: Analyze with CodeGuru Reviewer
on:
- push
- workflow_dispatch # This allows manual triggering of the action through the GitHub UI.
permissions:
id-token: write
contents: read
security-events: write
jobs:
analyze:
name: Analyze with CodeGuru Reviewer
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
id: iam-role
continue-on-error: true
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: {ROLE_ARN}
aws-region: {REGION}
- uses: actions/checkout@v2
if: steps.iam-role.outcome == 'success'
with:
fetch-depth: 0
- name: Set up JDK 1.8
if: steps.iam-role.outcome == 'success'
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build project
if: steps.iam-role.outcome == 'success'
run: ./gradlew jar -x test
- name: CodeGuru Reviewer
uses: aws-actions/[email protected]
if: steps.iam-role.outcome == 'success'
continue-on-error: false
with:
s3_bucket: {BUCKET_NAME}
build_path: ./target/classes
- name: Upload review result
if: steps.iam-role.outcome == 'success'
uses: github/codeql-action/upload-sarif@v1
with:
sarif_file: codeguru-results.sarif.json
将字符串{ROLE_ARN} , {REGION}和{BUCKET_NAME}替换为从CDK输出收到的值。
这些示例使用GitHub的代码扫描功能来显示建议。如果您使用的是私人存储库而无需支付代码扫描,则将失败。在使用此功能之前,您需要为您的存储库或组织启用GitHub代码扫描(请参阅文档)。如果您不打算使用此功能,请省略Upload review result部分。
name: Analyze with CodeGuru Reviewer
on:
- push
- workflow_dispatch # This allows manual triggering of the action through the GitHub UI.
permissions:
id-token: write
contents: read
jobs:
analyze:
name: Analyze with CodeGuru Reviewer
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
id: iam-role
continue-on-error: true
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: {ROLE_ARN}
aws-region: {REGION}
- uses: actions/checkout@v2
if: steps.iam-role.outcome == 'success'
with:
fetch-depth: 0
- name: CodeGuru Reviewer
uses: aws-actions/[email protected]
if: steps.iam-role.outcome == 'success'
continue-on-error: false
with:
s3_bucket: {BUCKET_NAME}
- name: Store SARIF file
if: steps.iam-role.outcome == 'success'
uses: actions/upload-artifact@v2
with:
name: SARIF_recommendations
path: ./codeguru-results.sarif.json
将字符串{ROLE_ARN} , {REGION}和{BUCKET_NAME}替换为从CDK输出收到的值。
在这里,我们没有将工件上传到Githubs安全标签上,而是将其存储为CICD运行的Artifacs。这样可以访问存储库的任何人以SARIF格式下载建议。
您还可以在您的AWS控制台中查看所有建议。
有关更多信息,请参阅《 Codeguru评论者文档》。
请注意,只有允许上市的组织和存储库可以承担IAM角色来运行Codeguru审稿人。因此,我们建议您仅在push事件上运行Codeguru审稿人的操作。如果pull_request起源的存储库也是允许列表的一部分,则该操作将仅在pull_requests上成功。
此外,为了避免针对该存储库的用户的操作失败,我们将角色假设步骤标记为id: iam-role和守卫所有其他工作流程步骤:
if: steps.iam-role.outcome == 'success'
因此,如果不允许叉子担任角色,则不会被执行。
有关更多信息,请参见贡献。
该图书馆已获得MIT-0许可证的许可。请参阅许可证文件。