Skip to content

Create GitLab Pipeline

GitLab

Creating GitLab Pipeline

Create a Dockerfile

In your IDE lets start writing our new Dockerfile

ARG NGINX_VER
FROM nginx:${NGINX_VER}-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]

Here is our Dockerfile explained:

  1. ARG: Accept a build argument at build time.
  2. FROM: Set the base image to use, with the above ARG
  3. RUN: Run a command (remove the file default.conf)
  4. COPY: Copy a file from the context (our project source) to the image.
  5. EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
  6. CMD: Which command gets executed when running the container.

Now lets create our nginx.conf file, so we can copy it from the repository during docker build.

Create the file: nginx.conf at the same directory of the Dockerfile (root of your project)

user  nginx;
worker_processes  1;

error_log  /dev/stdout info;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    access_log /dev/stdout;
    sendfile        on;
    keepalive_timeout  60;
    include /etc/nginx/conf.d/*.conf;
}

Create the build stage

Create .gitlab-ci.yml

image: docker:stable

stages:
- build

We ask gitlab to start a runner with a docker daemon running.

This allows us to run Docker commands.

We declare on one stage, and named it: build

Now, set the Base image tag you would like to use:

variables:
NGINX_VER: "1.17.8"

In the build stage pass it as a build argument with the build option --build-arg:

build:
stage: build
script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build --build-arg NGINX_VER=${NGINX_VER} -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE

This is our full .gitlab-ci.yml file so far:

image: docker:git

stages:
- build

variables:
NGINX_VER: "1.17.8"

build:
stage: build
script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build --build-arg NGINX_VER=${NGINX_VER} -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE

Wait... what about $CI_JOB_TOKEN & $CI_REGISTRY, how were those created ?

These variables are created automatically at every runner, when the pipeline starts.

That way, you don't have to worry about authenticating the registry when running a pipeline.

For example the $CI_JOB_TOKEN value will be created according to the GitLab User that called the pipeline.

GitLab has a vast amount of predefined variables that you can use during pipelines.

You can read about them Here:

Environment variables

Commit and push

Now you can git commit and push your changes

If everything worked correctly, You should now see a pipeline running with a single stage.

CI/CD --> Pipelines

so now we have a pipeline that will run each time we push to the repository.

In the next page we will see how to create a release.

We will also tag the images using the release version number.

Comments