This afternoon, I have successfully implemented my first Gitlab CI pipeline!

The application I have done is made from python flask which I followed thanks to this tutorial. I connected this app to an external PostgreSQL database running on a separate KVM instance in my network. Here’s the preview:

python-shopping-list-app

Yeah, I know that’s not the prettiest to look at. But my priority is to make a functioning application first. I will modify it with CSS later (using my pipeline, of course!)

Once I have done this, I turned it into a docker container. Here’s the Dockerfile:

FROM python:3.8-alpine

WORKDIR /flask-postgresql

COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m", "flask", "run", "--host=0.0.0.0"]

Then I went and tested it locally before pushing it into my DockerHub repo:

docker build . -t [docker_username]/python-gitlab-ci:0.1
docker run --name app1 -p 5000:5000 [docker_username]/python-gitlab-ci:0.1

# When confirmed working, it's time to push it into my dockerhub repo

docker push [docker_username]/python-gitlab-ci:0.1

I can then establish my CI pipeline by creating a .gitlab-ci.yml file inside my repo root directory:

variables:
  IMAGE_NAME: [docker_username]/python-gitlab-ci
  IMAGE_VERSION: 0.1

build_image:
  image: docker:26.1.4
  services:
    - docker:26.1.4-dind
  variables:
    IMAGE_TAG: python-app
    DOCKER_TLS_CERTDIR: "/certs"
  before_script:
    - docker login -u $REGISTRY_USER -p $REGISTRY_PASS
  script:
    - docker build . -t $IMAGE_NAME:$IMAGE_TAG-$IMAGE_VERSION
    - docker push $IMAGE_NAME:$IMAGE_TAG-$IMAGE_VERSION

Now everytime I push some changes in my local Gitlab repo, the CI pipeline will trigger and push the new container image to my dockerhub:

gitlab-ci-img

I will use this pipeline as I modify my python-flask application’s CSS style.

However, this pipeline is still incomplete. I need to add a testing stage to test my Python code before building the image. Additionally, I require a deployment stage to deploy the container to my Kubernetes cluster.