Report this

What is the reason for this report?

github action docker database migration fail

Posted on February 16, 2021

I am trying to build CI/CD environment using github action.

Below script helps me to build the docker image, but returning an error

django.db.utils.OperationalError: could not connect to server: Operation timed out Is the server running on host “db-postgresql-nyc1.b.db.ondigitalocean.com” (157.230.224.47) and accepting TCP/IP connections on port 23052?

localhost can successfully migrate the database without any errors. However, when I use github action, it fails.

Maybe I should try manipulating firewalls from digital ocean or github? If you have any suggestion, please let me know.

name: Python application

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: docker login
      env:
        DOCKER_ID: ${{ secrets.DOCKER_USER }}
        DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} 
      run: |
        docker login -u $DOCKER_ID -p $DOCKER_PASSWORD
    - name: docker build
      run: |
        docker rmi --force "dummy/dummy-community:latest"
        docker build -t polls . 
    - name: docker migrate
      run: |
        docker run --env-file env polls sh -c "python manage.py makemigrations && python manage.py migrate"
    - name: docker tag image
      run: |
        docker tag polls:latest dummy/dummy-community:latest
    - name: docker push
      run: |
        docker push dummy/dummy-community:latest
    - name: kubectl deploy
      run: |
        kubectl delete pods polls-app
        kubectl get deploy polls-app


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This comment has been deleted

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

The error message implies that your GitHub action cannot establish a connection to your PostgreSQL database. The issue could indeed be related to the firewall rules. Here are some steps you should confirm:

1. Check your PostgreSQL database configuration: Make sure your PostgreSQL database is set to allow connections from Github’s IP addresses.

2. Check your DigitalOcean Networking Firewall rules: Make sure that your Droplet’s incoming rules have ports for your PostgreSQL open and ensure the GitHub’s IP range is allowed.

Remember, you should only allow trusted IPs to access your PostgreSQL instance to maintain security.

If you are unsure as to how to configure your Cloud Firewalls, you can head over to this DigitalOcean link for more details: https://www.digitalocean.com/docs/networking/firewalls/how-to/configure-rules/

Another option would be to create a VPN connection from your GitHub action to your DigitalOcean droplet.

However, always remember, to expose your services carefully, to reduce the attack surface. Another option is to use some kind of secret management to avoid storing secrets in repositories or in environment variables of your CI/CD pipelines.

For more options on connecting your application with your DigitalOcean PostgreSQL, follow the link below: https://www.digitalocean.com/docs/databases/postgresql/how-to/connect/

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.