在 Github 操作上获取 ConnectionRefusedError 但在本地运行

Getting ConnectionRefusedError on Github Actions but locally runs

我有一个读取电子邮箱的应用程序。

在本地我可以 运行 没有问题但是在 GitHub 操作上我收到以下错误:

Traceback (most recent call last):
  File "/home/runner/work/Bot-Deschamps-Newsletter/Bot-Deschamps-Newsletter/app.py", line 11, in <module>
    main()
  File "/home/runner/work/Bot-Deschamps-Newsletter/Bot-Deschamps-Newsletter/app.py", line 7, in main
    check_mail()
  File "/home/runner/work/Bot-Deschamps-Newsletter/Bot-Deschamps-Newsletter/read_email.py", line 10, in check_mail
    with MailBox(getenv('IMAP')).login(getenv('MAIL'), getenv('PASS')) as mailbox:
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/site-packages/imap_tools/mailbox.py", line 297, in __init__
    super().__init__()
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/site-packages/imap_tools/mailbox.py", line 35, in __init__
    self.client = self._get_mailbox_client()
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/site-packages/imap_tools/mailbox.py", line 303, in _get_mailbox_client
    return imaplib.IMAP4_SSL(self._host, self._port, self._keyfile, self._certfile, self._ssl_context,
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/imaplib.py", line 1323, in __init__
    IMAP4.__init__(self, host, port, timeout)
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/imaplib.py", line 202, in __init__
    self.open(host, port, timeout)
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/imaplib.py", line 1336, in open
    IMAP4.open(self, host, port, timeout)
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/imaplib.py", line 312, in open
    self.sock = self._create_socket(timeout)
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/imaplib.py", line 1326, in _create_socket
    sock = IMAP4._create_socket(self, timeout)
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/imaplib.py", line 302, in _create_socket
    return socket.create_connection(address)
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/socket.py", line 845, in create_connection
    raise err
  File "/opt/hostedtoolcache/Python/3.10.2/x64/lib/python3.10/socket.py", line 833, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
Error: Process completed with exit code 1.

我将 Gmail 用作 IMAP 服务器,并注意到在我切换到 Yahoo 后开始出现错误(由于 Gmail 的一些新限制,我不得不这样做)。

我的 main.yml 文件如下所示:

name: Python Script Workflow

on:
  # Triggers the workflow at 14:00 GMT on every day-of-week from Monday through Friday.
  schedule:
    - cron: "0 14 * * 1-5"

  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    environment:
      name: production

    steps:
      - name: Checkout repository content
        uses: actions/checkout@v3 # Checkout the repository content to GitHub runner.

      - name: Setup Python Version
        uses: actions/setup-python@v3
        with:
          python-version: 3.10.2

      - name: Install Python dependencies
        uses: py-actions/py-dependency-install@v3
        with:
          path: "requirements.txt"

      - name: Execute Python script # Run the app.py
        env:
          MAIL: ${{ secrets.MAIL }}
          PASS: ${{ secrets.PASS }}
          API_KEY: ${{ secrets.API_KEY }}
          API_KEY_SECRET: ${{ secrets.API_KEY_SECRET }}
          ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
          ACCESS_TOKEN_SECRET: ${{ secrets.ACCESS_TOKEN_SECRET }}
          BEARER_TOKEN: ${{ secrets.BEARER_TOKEN }}

        run: python app.py

在回溯中你有 getenv('IMAP') 但你的工作流程中没有定义环境变量。

希望对您有所帮助。