This time, I practiced to do  pytest on the image of  docker-compose with  Github Actions for the repository of the Python project, so I will write the memo.
--Create a token for packages on Github.
--Docker login.
--Register a token number that allows you to package  Secrets in any repository on Github. 
 (Registered as  GHCR_IO_TOKEN in my environment.)
repository/             #Repository directory
 ├ .github/
 │ ├ workflows/
 │ │ └ sample.yml     #Github actions file
 ├ project/
 │ ├ sample/           #(Explanation omitted)
 │ │ ├ __init__.py
 │ │ └ add.py
 │ └ test/             #(Explanation omitted)
 │   ├ __init__.py   
 │   └ test_add.py
 ├ docker-compose.yml   #Detailed description later
 ├ Dockerfile           #Appropriate Python image (explanation omitted)
 └ requirements.txt     #Describe the appropriate package (only pytest is described this time)
 ghcr.io for image in docker-compose.yml Enter the specified image name. docker-compose up, push the image with  docker-compose pushSpecify the image as follows.
docker-compose.yml
 services:
    python:
        build: .
        #image: ghcr.io/Github username/Repository name/Image name:Tag name
        image: ghcr.io/n-jun-k2/sample-actions/python:v1
Create an image locally and push it.
#For the time being, create an image as usual
docker-compose up -d 
#Push image
docker-compose push
Please change ... appropriately.
I'm checking out, logging in, pulling and doing a pytest.
yml:.github/workflows/sample.yml
name: ...
on:
  ...
jobs:
  ...:
    name: ...
    runs-on: ubuntu-20.04
    steps:
        - name: Checkout                                         #Check out
          uses: actions/checkout@v2
        - name: Login to Docker                                  #docker login
          uses: docker/login-action@v1
          with:
            registry: ghcr.io
            username: ${{ github.repository_owner }}
            password: ${{ secrets.GHCR_IO_TOKEN }}
        - name: Set up Docker                                    #Pull to launch the image
          run: |
            docker pull ghcr.io/n-jun-k2/sample-actions/python:v1
            docker-compose up -d
        - name: Run test                                         #Do a pytest.
          run: docker-compose exec -T python pytest
This CI file will pytest on the docker-compose image.
Note that don't forget to include the -T option below! I'll get an error
docker-compose exec -T python pytest
        Recommended Posts