Skip to main content

Working with Gitlab

Guangzhou, China

Pulling Images from a Private Docker Registry

Start by logging in with your Gitlab account:

docker login -u me@email-address.com gitlab.example.com:34578

And type in your Gitlab password.

Now enter your repository and go to Packages&Registries / Container Registry and copy the Image URL:

Working with Gitlab

Now you are ready to pull the image:

docker pull gitlab.example.com:34578/wiki/wiki-instar-en-docker:latest

Setting a Pipline Tigger

I want to automatically start a Docker build when new build artifacts were generated in a different repository. The Docker pipeline should than take this new code, wrap it into a new container image and push it into the Docker Registry.

The container I am using is a Go Fiber Webserver that should serve the static HTML code generated by a Gatsby.js repository (in form of build artifacts from a CI pipeline). I am using a variation of this Go Fiber boilerplate, build the Go Fiber webserver and load/unzip the artifacts into the public directory:

# Building the binary of the App
FROM golang:1.15 AS build

# Project labels
LABEL maintainer="m.polinowski@instar.com"

# `wiki` should be replaced with your project name
WORKDIR /go/src/wiki

# Copy all the Code and stuff to compile everything
COPY . .
RUN curl -L --header "PRIVATE-TOKEN:MYPRIVATETOKEN" "https://my.gitlab.com/api/v4/projects/wiki%2Fwiki_de/jobs/artifacts/master/download?job=page-render" >> ./temp/artifacts.zip
RUN apt update && apt install -y unzip && unzip -qq ./temp/artifacts.zip -d ./static

# Downloads all the dependencies in advance (could be left out, but it's more clear this way)
RUN go mod download

# Builds the application as a staticly linked one, to allow it to run on alpine
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app .

# Moving the binary to the 'final Image' to make it smaller
FROM alpine:latest

WORKDIR /app

# Create the `public` dir and copy all the assets into it
RUN mkdir ./static
COPY --from=build /go/src/wiki/static ./static

# `wiki` should be replaced here as well
COPY --from=build /go/src/wiki/app .

# Exposes port 8888 because our program listens on that port
EXPOSE 8888

# CMD ["./app"]
RUN chmod +x ./static/run.sh
CMD ["ash", "./static/run.sh"]

The run.sh file just starts the web server for me once the container is run:

#!/bin/ash
./app

To add the trigger enter the repository and go to Settings / CI/CD and click to add a new trigger:

Working with Gitlab

In the following examples, you can see the exact API call you need to make in order to rebuild a specific ref (branch or tag) with a trigger token.

All you need to do is replace the TOKEN and REF_NAME with the trigger token and the branch or tag name respectively.

Use cURL Copy one of the tokens above, set your branch or tag name, and that reference will be rebuilt.

curl -X POST \
-F token=TOKEN \
-F ref=REF_NAME \
https://my.gitlab.com/api/v4/projects/76/trigger/pipeline

The value of TOKEN is the value token that is displayed next to your new generated trigger (see screenshot above 9b7b...). And the REF_NAME is in my case the master branch. Note that the project that you want to trigger is defined by the number 76.

I can now add those lines to my .gitlab-ci.yml file in the Gatsby repository (NOT the repository for the container):

image: node:latest

cache:
paths:
- node_modules/
- .cache/
- public/

stages:
- build
- deploy

page-render:
stage: build
script:
- npm install
- npm run pre-build
- ./node_modules/.bin/gatsby build --prefix-paths
artifacts:
expire_in: 1 days
paths:
- public
only:
- master
tags: [build]

containerize:
stage: deploy
script:
- curl --request POST --form "token=9b7b..." --form ref=master https://my.gitlab.com/api/v4/projects/76/trigger/pipeline

This file will take my Gatsby source code (when committed to the MASTER branch) and run a Gatsby build on it. Once the build is done the deploy stage will be started and trigger the Docker pipeline:

image: docker:19.03.12-dind

variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""

services:
- name: docker:19.03.12-dind
entrypoint: ["env", "-u", "DOCKER_HOST"]
command: ["dockerd-entrypoint.sh"]


stages:
- build
# - test
- release

variables:
TEST_IMAGE: my.gitlab.com:12345/wiki/wiki_de_container:$CI_COMMIT_REF_NAME
RELEASE_IMAGE: my.gitlab.com:12345/wiki/wiki_de_container:latest

before_script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN my.gitlab.com:12345

build:
stage: build
script:
- docker build --pull -t $TEST_IMAGE .
- docker push $TEST_IMAGE

# test:
# stage: test
# - docker pull $TEST_IMAGE
# - docker run $TEST_IMAGE npm test

release:
stage: release
script:
- docker pull $TEST_IMAGE
- docker tag $TEST_IMAGE $RELEASE_IMAGE
- docker push $RELEASE_IMAGE
only:
- master

Once the pipeline finished you will be able to pull your fresh Docker images:

docker pull my.gitlab.com:12345/wiki/wiki_de_container