Scripts helping towards Monorepo with GitLab CI.
Mostly adapted from workarounds given in gitlab-ce/issues/19232. Hopefully soon to be integrated into GitLab CI!
Add as a submodule
git submodule add https://github.com/awesome-inc/monorepo.gitlab.git .monorepo.gitlab
and update your .gitlab-ci.yml
.
- Add some
variables
and abefore_script
to get the last green commit in Gitlab CI
# needs `curl`, `jq` 1.5
variables:
GIT_SUBMODULE_STRATEGY: recursive
before_script:
- .monorepo.gitlab/last_green_commit.sh
- Build your sub-component
foo
only when there are diffs in./foo
since the last green commit
build-foo:
# before
script: build foo
# after
script: .monorepo.gitlab/build_if_changed.sh foo build foo
Use YAML anchors to keep your jobs DRY.
Say your using docker-compose to orchestrate & build your services.
Your docker-compose.yml
may look something like this
version: '3'
services:
webapp:
image: "${DOCKER_REGISTRY}/${REPO}/${PRODUCT}_webapp:${TAG}"
build:
context: ./webapp
...
And you build and push each service through a script build.sh
which goes something like this
#!/bin/bash -ex
component=$1
docker-compose build ${component}
if [ "$CI_BUILD_REF_NAME" -ne "master" ]; then exit; fi
docker-compose push ${component}
This uses docker-compose
to build the service specified on the command line as a tagged docker image.
If you are on master
it pushes the built image right away to the specified registry.
Then, your jobs in .gitlab-ci.yml
could look something like this
# Use yml anchors, to keep jobs DRY, cf.: https://docs.gitlab.com/ee/ci/yaml/#anchors
.build_template: &build_definition
tags:
- linux
- docker
stage: build
script: .monorepo.gitlab/build_if_changed.sh ${CI_JOB_NAME} ./build.sh ${CI_JOB_NAME}
webapp:
<<: *build_definition
With awesomeinc/docker.gitlab.monorepo it is easy to use GitLab's docker-in-docker executor. Just add this to your .gitlab-ci.yml
variables:
# cf.: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
services:
- docker:dind
image:
name: awesomeinc/docker.gitlab.monorepo:0.1.0
entrypoint: [""] # force an empty entrypoint, cf.: https://gitlab.com/gitlab-org/gitlab-runner/issues/2692#workaround
Some example usages are given in