Steps to create a Job that pulls a Docker image and tests it with Github Actions

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.

Prerequisites

--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.)

Folder structure


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)

procedure

  1. ghcr.io for image in docker-compose.yml Enter the specified image name.
  2. After creating an image with docker-compose up, push the image with docker-compose push
  3. Pull on the github actions file and do a pytest!

Upload the image to ghcr.io.

Specify 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

About CI files on Github Actions

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

Steps to create a Job that pulls a Docker image and tests it with Github Actions
[Golang] Create docker image with Github Actions
Steps to set up Pipenv, create a CRUD app with Flask, and containerize it with Docker
I want to create a pipfile and reflect it in docker
Steps to create a Twitter bot with python
I have a private Python package but I want to install pipenv on GitHub Actions and build a Docker Image
[Linux] Create a self-signed certificate with Docker and apache
I made a Docker Image that reads RSS and automatically tweets regularly and released it.
A simple system that automatically shoots with object detection and sends it to LINE
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
Steps to quickly create a deep learning environment on Mac with TensorFlow and OpenCV
A script that makes it easy to create rich menus with the LINE Messaging API
Create a deploy script with fabric and cuisine and reuse it
Create a temporary file with django as a zip file and return it
I tried to create Bulls and Cows with a shell program
Create a Docker container image with JRE8 / JDK8 on Amazon Linux
Create a poster with matplotlib to visualize multiplication tables that remember multiplication
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Steps to create a Django project
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
Deploy a Python app on Google App Engine and integrate it with GitHub
Steps to create a Python virtual environment with VS Code on Windows
[Python] Create a linebot to write a name and age on an image
How to make a container name a subdomain and make it accessible in Docker
A python program that resizes a video and turns it into an image
Start the webcam to take a still image and save it locally
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
How to create a clone from Github
Create a dummy image with Python + PIL.
I came up with a way to create a 3D model from a photo Part 02 Image loading and vertex drawing
Security Server Docker Image that allows you to easily try X-Road is now available, so give it a try 1
I made a system that automatically decides whether to run tomorrow with Python and adds it to Google Calendar.
Create a web service with Docker + Flask
Create an API to convert PDF files to TIF images with FastAPI and Docker
A spell that eliminates non-Japanese characters and symbols to create a Japanese plaintext corpus
When I tried to create a virtual environment with Python, it didn't work
I want to write an element to a file with numpy and check it.
Zip-compress any file with the [shell] command to create a file and delete the original file.
Build a data analysis environment that links GitHub authentication and Django with JupyterHub
Create a stacked graph corresponding to both positive and negative directions with matplotlib
Load a photo and make a handwritten sketch. With zoom function. Tried to make it.
Build a Docker image containing the private repository Python library on GitHub Actions
Create a web app that converts PDF to text using Flask and PyPDF2
I tried to make a simple image recognition API with Fast API and Tensorflow
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
A new form of app that works with GitHub: How to make GitHub Apps
I tried to make it on / off by setting "Create a plug-in that highlights double-byte space with Sublime Text 2".
When I tried to create a project using Python on Docker with PyCharm, it didn't work, but it worked with Docker Compose.
Create a page that loads infinitely with python
How to create a multi-platform app with kivy
A Python script that crawls RSS in Azure Status and posts it to Hipchat
Create a discord bot that notifies unilaterally with python (use only requests and json)
How to create a USB that Linux and Win10 installer and winpe can boot UEFI
Create a High Dynamic Range Image (HDR) with OpenCV and Python (Mertens, Robertson, Debevec)
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
I made a web application that maps IT event information with Vue and Flask
From installing Flask on CentOS to making it a service with Nginx and uWSGI
It was a little difficult to do flask with the docker version of nginx-unit
A memo that detects and returns an image acquired from a webcam with Django's OpenCV
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)