19

I wanted to run django test cases inside container. I am able to pull private image from docker hub. but when I ran command to test, It is failed to run.

Anyone tried running test cases inside the container.

jobs:
test:
container:
  image: abcd
  credentials:
    username: "<username>"
    password: "<password>"

steps:
  - uses: actions/checkout@v2
  - name: Display Python version
    run: |
      python -m pip install --upgrade pip
      pip install -r requirements/dev.txt
  - name: run test
    run: |
      python3 manage.py test
   
DannyB
  • 12,810
  • 5
  • 55
  • 65
Manoj Jadhav
  • 1,270
  • 1
  • 15
  • 23

1 Answers1

39

In my experience, I found out that using GitHub's container instruction causes more confusion than simply running whatever you want on the runner itself, as if you are running it on your own machine.

A big majority of the tests I am running on GitHub actions are running in containers, and some require private DockerHub images.

I always do this:

  1. Create a docker-compose.yml for development use, so I can test things locally.
  2. Usually in CI, you might want slightly different things in your docker-compose (for example, no volume mappings) - if this is the case, I am creating another docker-compose.yml in a .ci subfolder.
  3. My docker-compose.yml contains a test service, that runs whatever test (or test suite) I want.

Here is a sample GitHub actions file I am using:

name: Test
on:
  pull_request:
  push: { branches: master }

jobs:
  test:
    name: Run test suite
    runs-on: ubuntu-latest
    env:
      COMPOSE_FILE: .ci/docker-compose.yml
      DOCKER_USER: ${{ secrets.DOCKER_USER }}
      DOCKER_PASS: ${{ secrets.DOCKER_PASS }}

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Login to DockerHub
      run: docker login -u $DOCKER_USER -p $DOCKER_PASS

    - name: Build docker images
      run: docker-compose build

    - name: Run tests
      run: docker-compose run test

Of course, this entails setting up the two mentioned secrets, but other than that, I found this method to be:

  • Reliable
  • Portable (I switched from Travis CI with the same approach easily)
  • Compatible with dev environment
  • Easy to understand and reproduce both locally and in CI
DannyB
  • 12,810
  • 5
  • 55
  • 65
  • I agree, the container instruction is really difficult to use. Better use your approach or just docker run containerimage cmd – Lincoln Dec 17 '20 at 23:03
  • 4
    So how does github know that the tests have failed or passed (as they are run inside a Docker container)? – d56 Feb 26 '21 at 12:20
  • 3
    Running a command that executes tests from the host, using docker or docker-compose will also exit with non zero code if the command fails in the container. – DannyB Feb 26 '21 at 18:07
  • @DannyB do you have a sample codebase with this workflow? – dondrzzy Jul 14 '21 at 13:10
  • 4
    @dondrzzy - I am using this workflow mainly for private complex apps, but I have a similar one in at least two public repos: [here](https://github.com/DannyBen/alf/blob/master/.github/workflows/test.yml) and [here](https://github.com/DannyBen/rush-cli/blob/master/.github/workflows/test.yml). – DannyB Jul 14 '21 at 13:36
  • @DannyB do you have a repo on GitHub that uses this approach? – Michele Locati Sep 30 '21 at 10:21
  • Yes, one comment above :) – DannyB Sep 30 '21 at 12:02