温馨提示×

Postman在Ubuntu中如何集成CI/CD

小樊
73
2025-04-09 09:01:34
栏目: 智能运维

在Ubuntu中集成Postman到CI/CD流程可以通过多种方式实现,以下是使用GitHub Actions的一个详细步骤指南:

在Ubuntu中集成Postman到CI/CD的步骤

  1. 创建GitHub仓库并添加API密钥
  • 在GitHub上创建一个新的仓库。
  • 将你的Postman API密钥添加到仓库的Secrets中,路径为 Settings > Secrets and variables > Actions > New repository secrets
  1. 创建GitHub Actions工作流
  • 在仓库的 .github/workflows 目录下创建一个新的YAML文件(例如 run-postman-tests.yml)。
  • 编辑该文件以定义工作流。
  1. 编写工作流文件: 以下是一个示例工作流文件的内容:
name: Run Multiple Postman Collections and Save Reports

on:
  push:
    branches:
      - main

jobs:
  postman-cli-tests:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        config:
          - { collection_id: "40443175-0e230280-1eea-45ad-b7e4-2c6316a91b62", environment_id: "40443175-1c302d5e-5930-4579-880f-ce404a2c4b29" }
          - { collection_id: "40443175-85560488-42ec-4d0e-b866-fda8ca231b15", environment_id: "40443175-55f8a2ab-0dc1-48a9-8279-59b370c19d78" }

    steps:
      # 代码检出
      - name: Checkout code
        uses: actions/checkout@v2

      # 安装Postman CLI
      - name: Install Postman CLI
        run: |
          curl -o- "https://dl-cli.pstmn.io/install/linux64.sh" | sh

      # 登录到Postman CLI
      - name: Login to Postman CLI
        run: |
          postman login --with-api-key ${{ secrets.POSTMAN_API_KEY }}

      # 运行Postman集合并生成报告
      - name: Run Collection and Generate Report
        run: |
          postman collection run ${{ matrix.config.collection_id }} \
            --environment ${{ matrix.config.environment_id }}

解释工作流文件的各个部分

  • on: 定义工作流触发条件,这里设置为在 main 分支上推送更改时触发。
  • jobs: 定义工作流中的任务。
    • postman-cli-tests: 定义一个名为 postman-cli-tests 的任务。
      • runs-on: 指定运行工作流的操作系统为Ubuntu最新版。
      • strategy: 使用矩阵策略来并行运行多个配置。
      • matrix.config: 定义多个配置,每个配置包含一个Postman集合ID和环境ID。
    • steps: 定义工作流的各个步骤。
      • Checkout code: 检出代码。
      • Install Postman CLI: 使用curl命令安装Postman CLI。
      • Login to Postman CLI: 使用API密钥登录到Postman CLI。
      • Run Collection and Generate Report: 运行Postman集合并生成报告。

提交并推送工作流文件

将编辑好的工作流文件提交并推送到GitHub仓库:

git add run-postman-tests.yml
git commit -m "Add GitHub Actions workflow for running Postman tests"
git push origin main

验证工作流

推送更改后,GitHub Actions将自动触发工作流,并在Actions标签页中显示运行结果。你可以通过这些结果来验证Postman测试是否成功执行。

通过这种方式,你可以在Ubuntu中通过GitHub Actions自动化Postman测试的集成到CI/CD流程中。

0