Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub Actions: How to run test inside container

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
   
like image 964
Manoj Jadhav Avatar asked Oct 15 '20 04:10

Manoj Jadhav


People also ask

Do GitHub Actions run in a container?

GitHub Actions has a relatively little known feature where you can run jobs in a container, specifically a Docker container. I liken it to delegating the entire job to the container, so every step that would run in the job will instead run in the container, including the clone/checkout of the repository.

How do you run inside a container?

To run a command as a different user inside your container, add the --user flag: docker exec --user guest container-name whoami.


1 Answers

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
like image 198
DannyB Avatar answered Oct 19 '22 10:10

DannyB