如何从 GitHub 操作中的步骤访问本地主机?

问题描述 投票:0回答:1

我有 2 个存储库,一个用于我的 NextJS 应用程序,我们将其称为存储库 A。另一个用于我的使用 Cucumber 和 playwright 的测试套件,我们将其称为存储库 B。

每当有对存储库 A 的主分支的拉取请求时,我想运行 Github Actions 工作流程。在工作流程中,应该发生以下情况:

  1. 从仓库 A 中查看 PR 分支负责人的代码。
  2. 安装repo A的所有依赖项。
  3. 构建应用程序并启动应用程序。
  4. 查看存储库 B 中的测试套件代码。
  5. 安装所有依赖项。
  6. 针对从步骤 3) 运行的应用程序运行测试

到目前为止,我已经尝试了很多不同的方法来实现预期的行为。以下是我当前的工作流程 YAML 文件。

name: Automated Testing workflow
on:
  pull_request: 
    types: 
      - opened
    branches: 
      - 'github-actions/Test'
  workflow_dispatch:
  
jobs:
  build-and-test:
    name: Build and test app
    runs-on: ubuntu-latest
    env:
      APP_URL: ${{ secrets.APP_URL }}
      USERNAME: ${{ secrets.USERNAME }}
      PASSWORD: ${{ secrets.PASSWORD }}
      ORGANISATION_NAME: ${{ secrets.ORGANISATION_NAME }}
    steps:
      - name: Checkout Git repository A
        uses: actions/checkout@v4
        with:
          path: 'app'

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
        
      - name: Install Node.js dependencies
        working-directory: ${{ github.workspace }}/app
        run: npm ci

      - name: Run application
        working-directory: ${{ github.workspace }}/app
        run: npm run start &

      - name: Checkout Automated Testing suite repo
        uses: actions/checkout@v4
        with:
          repository: orgname/testing-repo-B
          token: ${{ secrets.TOKEN }}
          path: 'tests'
      
      - name: Install dependencies
        working-directory: ${{ github.workspace }}/tests
        run: npm ci

      - name: Install playwright browsers
        working-directory: ${{ github.workspace }}/tests
        run: npx playwright install --with-deps

      - name: Run Testing Suite
        run: export APP_URL=$APP_URL &&
          export USERNAME=$USERNAME &&
          export PASSWORD=$PASSWORD &&
          export ORGANISATION_NAME=$ORGANISATION_NAME &&
          npm test
        working-directory: ${{ github.workspace }}/tests

我可以看到存储库 A 中的应用程序正在按预期构建并运行。在日志中确实表明应用程序正在

http://localhost:3000
上运行。 但是,在我运行测试的步骤中,我收到以下错误:

Error: a BeforeAll hook errored, process exiting: file:/home/runner/work/******/******/tests/hooks/hooks.js:36
    at Runtime.runTestRunHooks (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/runtime/run_test_run_hooks.js:22:23)
    at async Runtime.start (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/runtime/index.js:62:9)
    at async runCucumber (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/api/run_cucumber.js:110:21)
    at async Cli.run (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/cli/index.js:56:29)
    at async Object.run [as default] (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/cli/run.js:29:18) {
  [cause]: page.goto: net::ERR_CONNECTION_REFUSED at http://***
  Call log:
    - navigating to "***", waiting until "load"
  
      at Object.<anonymous> (/home/runner/work/******/******/tests/hooks/hooks.js:54:16)
      at async wrapPromiseWithTimeout (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/time.js:57:12)
      at async Object.run (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/user_code_runner.js:64:22)
      at async Runtime.runTestRunHooks (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/runtime/run_test_run_hooks.js:14:31)
      at async Runtime.start (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/runtime/index.js:62:9)
      at async runCucumber (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/api/run_cucumber.js:110:21)
      at async Cli.run (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/cli/index.js:56:29)
      at async Object.run (/home/runner/work/******/******/tests/node_modules/@cucumber/cucumber/lib/cli/run.js:29:18) {
    name: 'Error'
  }
}

如果我们看一下错误消息

page.goto: net::ERR_CONNECTION_REFUSED at http://***
,对我来说似乎存在一些网络问题。我可以看到应用程序在 GH UI 中运行,并且在日志中我还可以看到应用程序的 URL(如前所述)。但不知何故,在最后一步中,当尝试运行测试以及自动化测试尝试访问本地主机时,我收到了
ERR_CONNECTION_REFUSED
错误。由于它将 URL 打印为
 at http://***
我假设环境变量已从 github 秘密中正确设置。

有人可以帮我解决这个问题吗?

TLDR;无法访问在 localhost 上运行的应用程序以在 Github 操作中进行测试。

我尝试在本地运行应用程序并针对它运行测试套件。测试在本地环境运行顺利。

我尝试将 APP_URL 从 http://localhost:3000 更改为

127.0.0.1
但没有成功。

我尝试了多种不同的工作流程配置。我还尝试在服务中运行该应用程序。以下是我尝试过的工作流程的不同 YAML 文件。

name: Automated Testing workflow
on:
  pull_request: 
    types: 
      - opened
    branches: 
      - 'github-actions/Test'
  workflow_dispatch:
  
jobs:
  build-and-test:
    name: Build and test app
    runs-on: ubuntu-latest
    env:
      APP_URL: ${{ secrets.APP_URL}}
      USERNAME: ${{ secrets.USERNAME}}
      PASSWORD: ${{ secrets.PASSWORD}}
      ORGANISATION_NAME: ${{ secrets.ORGANISATION_NAME}}
    container: ubuntu
    services:
      appA:
        image: node:20-alpine
        ports: 
          - 3000:3000
    steps:
      - name: Checkout Git repository
        uses: actions/checkout@v4
        with:
          path: 'app'

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
        
      - name: Install Node.js dependencies
        working-directory: ${{ github.workspace }}/app
        run: npm ci

      - name: Run application
        working-directory: ${{ github.workspace }}/app
        run: npm run start:prod &

      - name: Checkout Automated Testing suite repo
        uses: actions/checkout@v4
        with:
          repository: orgname/testing-repo-B
          token: ${{ secrets.TOKEN }}
          path: 'tests'
      
      - name: Install dependencies
        working-directory: ${{ github.workspace }}/tests
        run: npm ci

      - name: Install playwright browsers
        working-directory: ${{ github.workspace }}/tests
        run: export APP_URL=$APP_URL &&
          export USERNAME=$USERNAME &&
          export PASSWORD=$PASSWORD &&
          export ORGANISATION_NAME=$ORGANISATION_NAME &&
          npm test && npm test
        working-directory: ${{ github.workspace }}/tests
continuous-integration automated-tests github-actions playwright cucumberjs
1个回答
0
投票

首先,我认为在Github操作中使用

&
以这种方式启动应用程序无法使其保持运行

因此,为了跟踪此工作流程并验证这一点,我可以建议一些编辑:

  1. 启动应用程序后,您可以运行类似
    curl -i localhost:3000
    的命令,这样您就可以确保它仍在运行
- name: Run application
        working-directory: ${{ github.workspace }}/app
        run: |
          npm run start &
          curl -i localhost:3000
  1. 添加另一个步骤,这只是另一个
    bash
    步骤,执行相同的操作以确保应用程序不会退出
- name: test app is still running
  run: curl -i localhost:3000

这应该有助于跟踪应用程序是否因以下步骤而退出


除此之外,我认为最好使用其他方式来启动应用程序,例如

pm2
,我认为它已经包含在上述工作流程中,否则,您可以添加一个步骤来安装它,但您可以先测试如果有的话。

它的用法应类似于

pm2 start npm -- start
,它将取代命令
npm run start &

- name: Run application
        working-directory: ${{ github.workspace }}/app
        run: |
          pm2 start npm -- start
          curl -i localhost:3000

这些编辑主要是针对第一个工作流程的

© www.soinside.com 2019 - 2024. All rights reserved.