在 GitHub Actions 中使用 k6
设置 k6 测试,使其作为 GitHub Actions 工作流的一部分自动运行
在 GitHub Actions 中使用 k6 是 CoddyKit 上的免费 Load Testing & Performance Benchmarking (JMeter & k6) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Load Testing & Performance Benchmarking (JMeter & k6) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Load Testing & Performance Benchmarking (JMeter & k6) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Automating Performance Testing
Welcome to integrating performance testing with CI/CD! Continuous Integration (CI) and Continuous Delivery (CD) pipelines automate software development steps.
Bringing performance tests into this flow ensures that performance issues are caught early, before they reach users.
Why k6 in GitHub Actions?
GitHub Actions provides a powerful way to automate tasks directly within your GitHub repository. Here's why it's great for k6 performance tests:
- Automation: Tests run automatically on code changes.
- Consistency: The test environment is standardized.
- Early Feedback: Catch performance regressions quickly.
- Version Control: Your test scripts and workflows live with your code.
GitHub Actions Workflow Basics
A GitHub Actions workflow is defined by a YAML file in the .github/workflows/ directory of your repository.
It specifies when to run, what to run (jobs and steps), and on which operating system.
Our Simple k6 Test Script
Before creating the workflow, let's have a basic k6 script. This script will perform a simple HTTP GET request to a test endpoint. Save this as test.js in your repository.
import http from 'k6/http';
import { check, sleep } from 'k6';
export default function () {
const res = http.get('https://test.k6.io');
check(res, {
'status is 200': (r) => r.status === 200,
});
sleep(1);
}Creating the Workflow File
First, create a new directory .github/workflows/ in the root of your repository. Inside this, create a file named, for example, k6-perf-test.yml.
This file will define our automation steps.
Workflow Structure: Name & Trigger
Every workflow needs a name and a trigger (on). We'll trigger our test on every push to the main branch.
Here's the start of our k6-perf-test.yml file:
name: k6 Performance Test
on:
push:
branches:
- main
jobs:Defining the Test Job
Next, we define a job. A job is a set of steps that execute on the same runner. We'll call ours run-k6-test and use an ubuntu-latest runner.
name: k6 Performance Test
on:
push:
branches:
- main
jobs:
run-k6-test:
runs-on: ubuntu-latest
steps:Checkout Code & Install k6
Inside our job's steps, we first need to check out our repository code, then install k6. We'll use a pre-built GitHub Action to easily install k6.
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install k6
uses: k6io/action@v2.0.0Executing the k6 Test
Finally, we add a step to run our test.js k6 script. We can specify options like the number of virtual users (-u) and duration (-d) directly.
- name: Install k6
uses: k6io/action@v2.0.0
- name: Run k6 test
run: k6 run -u 10 -d 30s test.jsViewing Test Results
After committing your .yml file and test.js script to your GitHub repository, navigate to the Actions tab.
You'll see your workflow running. Click on the workflow run, then the run-k6-test job to view the detailed logs, including k6's output and results.
Workflow Essentials
Which of the following is the correct path and file extension for a GitHub Actions workflow file?
Recap & Next Steps
You've learned how to integrate k6 performance tests into GitHub Actions! This enables automated, consistent, and early performance feedback.
- We explored the workflow structure.
- Set up a k6 script and the YAML workflow file.
- Configured steps for checking out code, installing k6, and running the test.
- Understood how to view results in GitHub Actions.
Next, explore more advanced k6 features in your workflows, like reporting and thresholds!
常见问题解答
「在 GitHub Actions 中使用 k6」课时是免费的吗?
是的 — 「在 GitHub Actions 中使用 k6」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Load Testing & Performance Benchmarking (JMeter & k6) 课程的其余内容,请升级到 CoddyKit PRO。 Load Testing & Performance Benchmarking (JMeter & k6) 课程共包含 4 节课。
「在 GitHub Actions 中使用 k6」这节课中我会学到什么?
设置 k6 测试,使其作为 GitHub Actions 工作流的一部分自动运行 你通过在浏览器中直接运行的动手代码来练习 Load Testing & Performance Benchmarking (JMeter & k6),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Load Testing & Performance Benchmarking (JMeter & k6) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Load Testing & Performance Benchmarking (JMeter & k6) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「在 GitHub Actions 中使用 k6」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Load Testing & Performance Benchmarking (JMeter & k6) 课中编写并运行代码吗?
能。每节 Load Testing & Performance Benchmarking (JMeter & k6) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 将 JMeter 集成到 Jenkins
- 在 GitHub Actions 中使用 k6
- 性能门禁与 SLO
- CI 中的趋势分析与基线建立