Search code examples
pythongithub-actionsimap-tools

Getting ConnectionRefusedError on Github Actions but locally runs


I have an application that reads an email box.

Locally I can run without problems but on GitHub Actions I'm getting the error below:

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.

I used Gmail as an IMAP server and noticed that the errors started after I switched to Yahoo (I had to do this due to some new restrictions coming to Gmail).

My main.yml file looks like this:

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

Solution

  • In the traceback you have getenv('IMAP') but the environment variable is not defined in your workflow.

    Hope that helps.