Commit 4b516b77 authored by Clemens Lange's avatar Clemens Lange
Browse files

add pipeline for building docker images

The pipeline contains two different build jobs, building a standalone or a cms/cvmfs version. In addition it contains a job for checking which files have been modified, enabling to trigger the build of the cms/cvmfs version that was changed. Only reason for there to be a separate script `check.sh` and not just a multi line script is because I didn't get the multi line syntax to work with a for loop

```
script:
  - >
```

If you know how to fix it we can transfer the code in `check.sh` to the pipeline!

-----------------------------

The two build jobs:

* A standalone build is triggered when the variables `CMSSW_VERSION`, `SCRAM_ARCH` and `BASE_IMAGE` is passed, could be passed with a webhook.

* A cms/cvmfs version is built when there has been a change in the directory containing the Dockerfile corresponding to the image. Since there is only one job but five directories, before building, the job has to set the IMAGE_NAME variable to indicate which image to build (cc7-cms/cvmfs, slc6-cms/cvmfs, slc5-cms). This is set with the job `check changed files`

The file `check.sh` calls 

```
files=$(git diff-tree --no-commit-id --name-only -r $CI_COMMIT_SHA)
```

which returns a list of all files that has been modified. The directory of the file is checked and the pipeline is triggered again, this time with the variable IMAGE_NAME set to the directory name. (The reason why the job tags docker is because the runner has git installed).

See merge request cms-cloud/cmssw-docker!12
parents b509ad00 72f0feb9
stages:
- check
- build
before_script:
- export DATE=$(date +"%Y-%m-%d")
variables:
COMMIT_HASH: $CI_COMMIT_SHORT_SHA
check changed files:
stage: check
only:
changes: [cc7-cms/*, cc7-cvmfs/*, slc6-cms/*, slc6-cvmfs/*, slc5-cms/*]
except:
- pipelines
tags:
- docker
script:
- ./check.sh
build standalone:
stage: build
only:
variables:
- $CMSSW_VERSION && $SCRAM_ARCH && $BASE_IMAGE
variables:
REPO_NAME: standalone
IMAGE_NAME: cmssw
tags:
- docker-privileged-xl
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- echo "Building standalone image $BASE_IMAGE with $CMSSW_VERSION and $SCRAM_ARCH"
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --context $CI_PROJECT_DIR/$REPO_NAME --dockerfile $CI_PROJECT_DIR/$REPO_NAME/Dockerfile --destination $CI_REGISTRY_IMAGE/$IMAGE_NAME:$CMSSW_VERSION-$DATE-$COMMIT_HASH --build-arg=BUILD_DATE="$DATE" --build-arg=VERSION="$DATE" --build-arg=VCS_URL="$CI_REPOSITORY_URL" --build-arg=VCS_REF="$COMMIT_HASH" --build-arg BASEIMAGE="clelange/$BASE_IMAGE-cms:latest" --build-arg SCRAM_ARCH="$SCRAM_ARCH" --build-arg CMSSW_VERSION="$CMSSW_VERSION"
build cms cvmfs:
stage: build
only:
variables:
- $CI_PIPELINE_SOURCE == "pipeline" && $IMAGE_NAME
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- echo "Building image for $IMAGE_NAME"
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --context $CI_PROJECT_DIR/$IMAGE_NAME --dockerfile $CI_PROJECT_DIR/$IMAGE_NAME/Dockerfile --destination $CI_REGISTRY_IMAGE/$IMAGE_NAME:$DATE-$COMMIT_HASH --destination $CI_REGISTRY_IMAGE/$IMAGE_NAME:latest --build-arg=BUILD_DATE="$DATE" --build-arg=VERSION="$DATE" --build-arg=VCS_URL="$CI_REPOSITORY_URL" --build-arg=VCS_REF="$COMMIT_HASH"
#!/bin/bash
files=$(git diff-tree --no-commit-id --name-only -r $CI_COMMIT_SHA)
for file in $files; do
dir=${file%/*}
curl --request POST \
--form token=$CI_JOB_TOKEN \
--form ref=$CI_DEFAULT_BRANCH \
--form "variables[IMAGE_NAME]=$dir" \
https://gitlab.cern.ch/api/v4/projects/$CI_PROJECT_ID/trigger/pipeline
done
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment