Search code examples
pythondockerplaywrightgitlab-ci-runner

Installing Python on Node/Playwright docker image?


I have been using Playwrights node docker image, defined here: https://playwright.dev/docs/docker

This includes all the browser dependencies/etc.. for the node version of docker. This has worked fine however i'm going to be integrating the testrail CLI to help upload playwright report results to testrail.

Unfortunately this requires python which is not installed (from what I can tell) on the playwright default docker file.

What's my best/easiest course of action here? Obviously I am not going to switch all my test cases, would it make more sense to use a self-created docker file? Is there some way I can install python in the docker file in gitlab? (Which is the ci system we are using now).

I'm not super familiar with docker outside of the basics. But I imagine installing python after the fact makes more sense?

What is the best game plan here? Thanks!


Solution

  • The gitlab pipeline is defined in the .gitlab-ci.yml file in the root of your repository. If you open that file in a text editor, you'll see the various jobs the pipeline runs.

    It will look something like this:

    <some_job_name>:
        image: mcr.microsoft.com/playwright:v1.45.1-jammy
        script:
            - <some command>
    

    What that means is that the job will run on the image provided. In the script section you can add a call to install python ( apt-get install python3 for example)

    More info here: https://docs.gitlab.com/ee/ci/jobs/

    This will install python3 every time the job runs, so a better approach might be to build your own custom image. This way python will be installed once and available in the image thus speeding up your pipeline. The downside is it takes some setting up.

    More info on running CI/CD in containers: https://docs.gitlab.com/ee/ci/docker/using_docker_images.html

    The general steps are as follows:

    1. You can create your custom image by creating a custom Dockerfile:
    FROM mcr.microsoft.com/playwright:v1.45.1-jammy
    RUN apt-get install python3
    
    1. You run docker build and create the image
    2. You publish the image to a registry (https://docs.docker.com/reference/cli/docker/image/push/)
    3. Then you modify the CI to use your custom image for the job. (see job example above)
    4. Finally you tell gitlab how to access the registry that has the custom image (see the "Access an image from a private container registry" section of the Gitlab document on using docker images). Obviously you don't need to do this step if you publish to image to dockerhub

    I hope this helps.