aahnik
aahnik

Reputation: 1861

How to test my Dockerfile for my python project using GitHub actions?

Suppose, I have a python script that can take the screenshot of any web page (whose URL is supplied).

I want to automate the task of building docker images and running tests in the container.

In the local machine, I can run docker build -t myproj . to build.

and for testing, I can run docker run -it myproj pytest (if i forget to add the RUN that installs chromium and chromedriver, then my pytest will fail inside container)

I hope I am able to explain my purpose.

Normally,in github actions,the python source code can be run on ubuntu, mac, windows etc. Besides the different os, I also want to build and test my dockerfile.

Upvotes: 9

Views: 4450

Answers (1)

aahnik
aahnik

Reputation: 1861

After a little more experimenting and research I found out that the solution to my problem is simple.

Inside the .github/workflows folder create a docker-build-test.yml file.

 
name: Docker build and test

on:
  workflow_dispatch 
# you can trigger on anything you want

jobs:
  build:
   runs-on: ubuntu-latest
  
   steps:
     - uses: actions/checkout@v2
     - name: Build Docker image 
       run: docker build -t samplepy .
     - name: Run tests inside the container
       run: docker run samplepy poetry run pytest

Its very simple, because github's ubuntu-latest VMs already have docker installed and configured. You just run the docker command and everything just works.

And, I tested the above workflow with a dummy python project also.

Screenshot from 2021-04-17 14-02-29

Upvotes: 15

Related Questions