本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
教程:使用 GitHub Action 对代码进行 lint 检查
在本教程中,您将 Super-Linter GitHub Actionapp.js
)进行 lint 检查。之后,您可以修复报告的问题,并再次运行工作流以查看是否已成功修复问题。
提示
考虑使用 Super-Linter 来对 YAML 文件(例如 AWS CloudFormation 模板)进行 lint 检查。
先决条件
在开始之前,您需要:
-
一个带已连接的 AWS 账户的 CodeCatalyst 空间。有关更多信息,请参阅创建空间。
-
您的 CodeCatalyst 空间中的一个名为
codecatalyst-linter-project
的空项目。选择从头开始选项来创建此项目。有关更多信息,请参阅在 Amazon 中创建一个空项目 CodeCatalyst。
步骤 1:创建源存储库
在此步骤中,您将在 CodeCatalyst 中创建源存储库。您将使用此存储库来存储示例应用程序源文件(本教程中为 app.js
)。
有关源存储库的更多信息,请参阅创建源存储库。
创建源存储库
通过访问 https://codecatalyst.aws/
打开 CodeCatalyst 控制台。 -
导航到您的项目
codecatalyst-linter-project
。 -
在导航窗格中,选择代码,然后选择源存储库。
-
选择添加存储库,然后选择创建存储库。
-
在存储库名称中,输入:
codecatalyst-linter-source-repository
-
选择创建。
步骤 2:添加 app.js 文件
在此步骤中,您将 app.js
文件添加到源存储库。app.js
包含的函数代码存在几处错误,linter 将发现这些错误。
添加 app.js 文件
-
在 CodeCatalyst 控制台中,选择您的项目
codecatalyst-linter-project
。 -
在导航窗格中,选择代码,然后选择源存储库。
-
从源存储库列表中,选择您的存储库
codecatalyst-linter-source-repository
。 -
在文件中,选择创建文件。
-
在文本框中,输入以下代码:
// 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 }
-
对于文件名,输入
app.js
。保留其他默认选项。 -
选择提交。
现在,您已创建一个名为
app.js
的文件。
步骤 3:创建运行 Super-Linter 操作的工作流
在此步骤中,您将创建一个工作流,当您将代码推送到源存储库时,该工作流将运行 Super-Linter 操作。该工作流包含您在 YAML 文件中定义的以下构建基块:
-
触发器 – 当您将更改推送到源存储库时,此触发器会自动启动工作流运行。有关触发器的更多信息,请参阅使用触发器自动启动工作流运行。
-
“GitHub Actions”操作 – 触发后,GitHub Actions 操作将运行 Super-Linter 操作,后者反过来会检查源存储库中的所有文件。如果 linter 发现问题,则工作流操作将失败。
创建运行 Super-Linter 操作的工作流
-
在 CodeCatalyst 控制台中,选择您的项目
codecatalyst-linter-project
。 -
在导航窗格中,选择 CI/CD,然后选择工作流。
-
选择创建工作流。
-
对于源存储库,选择
codecatalyst-linter-source-repository
。 -
对于分支,选择
main
。 -
选择创建。
删除 YAML 示例代码。
-
添加以下 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 操作代码,如本过程的以下步骤中所示。 -
转到 GitHub Marketplace 中的 Super-Linter 页面
。 -
在
steps:
(小写形式)下,找到代码并将其粘贴到 CodeCatalyst 工作流中的Steps:
(大写形式)下。调整 GitHub Action 代码以符合 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
-
(可选)选择验证,确保 YAML 代码在提交之前有效。
-
选择提交,输入提交消息,选择您的
codecatalyst-linter-source-repository
存储库,然后再次选择提交。现在,您已创建工作流。由于在工作流顶部定义了触发器,因此工作流运行会自动启动。
查看正在运行的工作流
-
在导航窗格中,选择 CI/CD,然后选择工作流。
-
选择您刚刚创建的工作流:
codecatalyst-linter-workflow
。 -
在工作流图中,选择 SuperLinterAction。
-
等待操作失败。预计会失败,因为 linter 在代码中发现了问题。
-
将 CodeCatalyst 控制台保持打开状态并转至步骤 4:修复 Super-Linter 发现的问题。
步骤 4:修复 Super-Linter 发现的问题
Super-Linter 应已在 app.js
代码以及源代码库中包含的 README.md
文件中发现了问题。
解决 linter 发现的问题
-
在 CodeCatalyst 控制台中,选择日志选项卡,然后选择 Lint 代码库。
这将显示 Super-Linter 操作生成的日志。
-
在 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.
-
修复源存储库中的
app.js
和README.md
,然后提交您的更改。提示
要修复
README.md
,请在代码块中添加markdown
,如下所示:```markdown Setup examples: ... ```
您的更改将自动启动另一个工作流。等待该工作流完成。如果您已修复所有问题,则该工作流将成功。
清理
在 CodeCatalyst 中进行清理以从环境中移除本教程的跟踪。
在 CodeCatalyst 中进行清理
-
通过访问 https://codecatalyst.aws/
打开 CodeCatalyst 控制台。 -
删除
codecatalyst-linter-source-repository
。 -
删除
codecatalyst-linter-workflow
。
在本教程中,您已了解如何将 Super-Linter GitHub Action 添加到 CodeCatalyst 工作流中以便对一些代码进行 lint 检查。