Building a Simple Web App Part 5 of N : Gitlab Pipelines

The post is going to be about setting up a build / test pipeline that runs automatically when you push new code change. I am doing all of this with gitlab. While most of the world uses github, I decided to use gitlab because you can do more without paying anything :).

So I have all my app code in a gitlab repo. Once you do this you can easily configure build pipelines using a .yml file:

# This is the base image for the environment which all this will run in.
image: python:3.11

# The only service we need to be running is postgres
services:
  - postgres:15-alpine

# we are going to have 2 pipeline stages first we will run tests, then build
# the container if the tests pass
stages:
  - test
  - build

variables:
    POSTGRES_DB: dj_app_name
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
    POSTGRES_HOST_AUTH_METHOD: trust

    # these vars are for django
    SECRET_KEY: ${SECRET_KEY}
    DEBUG: ${DEBUG}
    CSRF_TRUSTED_ORIGINS: "https://curate.northnitch.com"
    ALLOWED_HOSTS: "127.0.0.1,localhost,0.0.0.0,testserver,45.33.45.235,curate.northnitch.com"


# Do a scratch install and the check to see if all the tests pass
test:
  variables:
    SECRET_KEY: 'm1-v(c8%na*fb2($wg5j_7*+68p-pvp-gkj%w@%(av7!m8w@gt'
    DB_CONFIG: "ci_test"
  script:
    - pip install --upgrade pip
    - pip install -r requirements.txt
    - python manage.py makemigrations
    - python manage.py migrate
    - python manage.py test 

# if we are good then lets build a docker container.
build:
  image: docker:20.10.16
  stage: build
  # we are going to run docker to be able to build the container
  services:
    - docker:20.10.16-dind
  script:
    # these $CI_ vars are set as gitlab config vars
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $CI_REGISTRY/bricetebbs/appname/web:latest .
    - docker push $CI_REGISTRY/bricetebbs/appname/web:latest

This will give us a built docker container for the web app in the gitlab container registry. We can pull it down from there to our server using:

docker pull registry.gitlab.com/bricetebbs/appname/web

I was using dockerhub but it got too tedious as they kept making the free version less useful so I’m sticking with this.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *