

亚马逊 CodeCatalyst 不再向新买家开放。现有客户可以继续正常使用该服务。有关更多信息，请参阅 [如何从中迁移 CodeCatalyst](migration.md)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 教程：使用动作的 Lint 代码 GitHub
<a name="integrations-github-action-tutorial"></a>

在本教程中，您将将 [Super-Linter GitHub 操作](https://github.com/marketplace/actions/super-linter)添加到亚马逊 CodeCatalyst 工作流程中。Super-Linter 操作检查代码，查找代码存在错误、格式问题和可疑结构的区域，然后将结果输出到控制台）。 CodeCatalyst将 linter 添加到工作流后，您可以运行工作流来对示例 Node.js 应用程序（`app.js`）进行 lint 检查。之后，您可以修复报告的问题，并再次运行工作流以查看是否已成功修复问题。

**提示**  
考虑使用 Super-Linter 来对 YAML 文件（例如 [CloudFormation 模板](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-guide.html)）进行 lint 检查。

**Topics**
+ [先决条件](#integrations-github-action-tutorial-prereqs)
+ [步骤 1：创建源存储库](#integrations-github-action-tutorial-create-source-repo)
+ [步骤 2：添加 app.js 文件](#integrations-github-action-tutorial-add-appjs)
+ [步骤 3：创建运行 Super-Linter 操作的工作流](#integrations-github-action-tutorial-create-workflow)
+ [步骤 4：修复 Super-Linter 发现的问题](#integrations-github-action-tutorial-fix-probs)
+ [清理](#integrations-github-action-tutorial-cleanup)

## 先决条件
<a name="integrations-github-action-tutorial-prereqs"></a>

在开始之前，您需要：
+ 一个连接的 CodeCatalyst **空间** AWS 账户。有关更多信息，请参阅 [创建空间](spaces-create.md)。
+ 您的 CodeCatalyst 空间中有一个名为的空项目`codecatalyst-linter-project`。选择**从头开始**选项来创建此项目。

  ```
  ```

  有关更多信息，请参阅 [在 Amazon 中创建一个空项目 CodeCatalyst](projects-create.md#projects-create-empty)。

## 步骤 1：创建源存储库
<a name="integrations-github-action-tutorial-create-source-repo"></a>

在此步骤中，您将在中创建源存储库 CodeCatalyst。您将使用此存储库来存储示例应用程序源文件（本教程中为 `app.js`）。

有关源存储库的更多信息，请参阅[创建源存储库](source-repositories-create.md)。

**创建源存储库**

1. 打开 CodeCatalyst 控制台，[网址为 https://codecatalyst.aws/](https://codecatalyst.aws/)。

1. 导航到您的项目 `codecatalyst-linter-project`。

1. 在导航窗格中，选择**代码**，然后选择**源存储库**。

1. 选择**添加存储库**，然后选择**创建存储库**。

1. 在**存储库名称**中，输入：

   ```
   codecatalyst-linter-source-repository
   ```

1. 选择**创建**。

## 步骤 2：添加 app.js 文件
<a name="integrations-github-action-tutorial-add-appjs"></a>

在此步骤中，您将 `app.js` 文件添加到源存储库。`app.js` 包含的函数代码存在几处错误，linter 将发现这些错误。

**添加 app.js 文件**

1. 在 CodeCatalyst 控制台中，选择您的项目`codecatalyst-linter-project`。

1. 在导航窗格中，选择**代码**，然后选择**源存储库**。

1. 从源存储库列表中，选择您的存储库 `codecatalyst-linter-source-repository`。

1. 在**文件**中，选择**创建文件**。

1. 在文本框中，输入以下代码：

   ```
   // const axios = require('axios')
   // const url = 'http://checkip.amazonaws.com/';
   let response;
   /**
    *
    * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
    * @param {Object} event - API Gateway Lambda Proxy Input Format
    *
    * Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html 
    * @param {Object} context
    *
    * Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
    * @returns {Object} object - API Gateway Lambda Proxy Output Format
    *
    */
   exports.lambdaHandler = async (event, context) => {
     try {
       // const ret = await axios(url);
       response = {
         statusCode: 200,
         'body': JSON.stringify({
           message: 'hello world'
           // location: ret.data.trim()
         })
       }
     } catch (err) {
       console.log(err)
       return err
     }
   
       return response
   }
   ```

1. 对于**文件名**，输入 `app.js`。保留其他默认选项。

1. 选择**提交**。

   现在，您已创建一个名为 `app.js` 的文件。

## 步骤 3：创建运行 Super-Linter 操作的工作流
<a name="integrations-github-action-tutorial-create-workflow"></a>

在此步骤中，您将创建一个工作流，当您将代码推送到源存储库时，该工作流将运行 Super-Linter 操作。该工作流包含您在 YAML 文件中定义的以下构建基块：
+ **触发器** – 当您将更改推送到源存储库时，此触发器会自动启动工作流运行。有关触发器的更多信息，请参阅[使用触发器自动启动工作流运行](workflows-add-trigger.md)。
+ **“GitHub 操作” 操作** — 触发后，“**GitHub 操作**” 操作会运行 Super-Linter 操作，该操作反过来会检查源存储库中的所有文件。如果 linter 发现问题，则工作流操作将失败。

**创建运行 Super-Linter 操作的工作流**

1. 在 CodeCatalyst 控制台中，选择您的项目`codecatalyst-linter-project`。

1. 在导航窗格中，选择 **CI/CD**，然后选择**工作流**。

1. 选择**创建工作流**。

1. 对于**源存储库**，选择 `codecatalyst-linter-source-repository`。

1. 对于**分支**，选择 `main`。

1. 选择**创建**。

1. 删除 YAML 示例代码。

1. 添加以下 YAML：

   ```
   Name: codecatalyst-linter-workflow
   SchemaVersion: "1.0"
   Triggers:
     - Type: PUSH
       Branches:
         - main
   Actions:
     SuperLinterAction:
       Identifier: aws/github-actions-runner@v1
       Configuration:
         Steps:
           github-action-code
   ```

   在前面的代码中，按照本*github-action-code*过程的以下步骤中的说明替换为 Super-Linter 操作代码。

1. 前往 Market [place GitHub 中的 Super-Linter 页面](https://github.com/marketplace/actions/super-linter)。

1. 在`steps:`（小写）下，找到代码并将其粘贴到`Steps:`（大写）下 CodeCatalyst的工作流程中。

   调整 GitHub 操作代码以符合 CodeCatalyst 标准，如以下代码所示。

   现在，您的 CodeCatalyst 工作流程如下所示：

   ```
   Name: codecatalyst-linter-workflow
   SchemaVersion: "1.0"
   Triggers:
     - Type: PUSH
       Branches:
         - main
   Actions:
     SuperLinterAction:
       Identifier: aws/github-actions-runner@v1
       Configuration:
         Steps:
           - name: Lint Code Base
             uses: github/super-linter@v4
             env:
               VALIDATE_ALL_CODEBASE: "true"
               DEFAULT_BRANCH: main
   ```

1. （可选）选择**验证**，确保 YAML 代码在提交之前有效。

1. 选择**提交**，输入**提交消息**，选择您的 `codecatalyst-linter-source-repository` **存储库**，然后再次选择**提交**。

   现在，您已创建工作流。由于在工作流顶部定义了触发器，因此工作流运行会自动启动。

**查看正在运行的工作流**

1. 在导航窗格中，选择 **CI/CD**，然后选择**工作流**。

1. 选择您刚刚创建的工作流：`codecatalyst-linter-workflow`。

1. 在工作流程图中，选择**SuperLinterAction**。

1. 等待操作失败。预计会失败，因为 linter 在代码中发现了问题。

1. 让 CodeCatalyst 控制台保持打开状态并转至[步骤 4：修复 Super-Linter 发现的问题](#integrations-github-action-tutorial-fix-probs)。

## 步骤 4：修复 Super-Linter 发现的问题
<a name="integrations-github-action-tutorial-fix-probs"></a>

Super-Linter 应已在 `app.js` 代码以及源代码库中包含的 `README.md` 文件中发现了问题。

**解决 linter 发现的问题**

1. 在 CodeCatalyst 控制台中，选择 “**日志**” 选项卡，然后选择 **Lint Code Base**。

   这将显示 Super-Linter 操作生成的日志。

1. 在 Super-Linter 日志中，向下滚动到第 90 行左右，您可以从该行开始查看问题。其内容与以下内容类似：

   ```
   /github/workspace/hello-world/app.js:3:13: Extra semicolon.
   /github/workspace/hello-world/app.js:9:92: Trailing spaces not allowed.
   /github/workspace/hello-world/app.js:21:7: Unnecessarily quoted property 'body' found.
   /github/workspace/hello-world/app.js:31:1: Expected indentation of 2 spaces but found 4.
   /github/workspace/hello-world/app.js:32:2: Newline required at end of file but not found.
   ```

1. 修复源存储库中的 `app.js` 和 `README.md`，然后提交您的更改。
**提示**  
要修复 `README.md`，请在代码块中添加 `markdown`，如下所示：  

   ```
   ```markdown
   Setup examples:
   ...
   ```
   ```

   您的更改将自动启动另一个工作流。等待该工作流完成。如果您已修复所有问题，则该工作流将成功。

## 清理
<a name="integrations-github-action-tutorial-cleanup"></a>

进行清理 CodeCatalyst 以从您的环境中删除本教程的痕迹。

**要清理干净 CodeCatalyst**

1. 打开 CodeCatalyst 控制台，[网址为 https://codecatalyst.aws/](https://codecatalyst.aws/)。

1. 删除 `codecatalyst-linter-source-repository`。

1. 删除 `codecatalyst-linter-workflow`。

在本教程中，你学习了如何将 Super-Linter GitHub 操作添加到 CodeCatalyst 工作流程中以整理一些代码。