.gitlab-ci.yml
file to the repository. But what if in a hundred repositories is a file with the same content? Even if you decompose it into repositories once, how then to change it? But what if one .gitlab-ci.yml
not enough for building - Dockerfile
or Dappfile
, different scripts and structure of YAML files for Helm are needed? How to update them?
.gitlab-ci.yml
from another repository, or link .gitlab-ci.yml
from files in other repositories ...
.gitlab-ci.yml
interests the community. The decision to add sections from a file in another repository seems very simple: it is based on many years of programming practice and will be clear to anyone. However, include how the concept works well in the case of the source tree, but in the case of several Git repositories in this solution, you can see the following disadvantages:
.gitlab-ci.yml
, store it and reassemble it already on the basis of it..gitlab-ci.yml
file, then in fact the .gitlab-ci.yml
project of the project that is going to change, but the change history will not be visible..gitlab-ci.yml
remains..gitlab-ci.yml
and other files needed for building;.gitlab-ci.yml
file common to hundreds of other projects. When this file is changed from the user gitlab-ci-distributor, commits are sent to other projects.
.gitlab-ci.yml
projects, add them to the build task of the common-ci-config project.
.gitlab-ci.yml
and who changed it. The problem of storing the effective .gitlab-ci.yml
, because Each project always shows the full version without include..gitlab-ci.yml
by invoking a script. That is, if it is necessary that .gitlab-ci.yml
always uses the latest version of the test configuration when building, then the testing is placed in a script in the common-ci-config project. $ curl -s --header "PRIVATE-TOKEN: $TOKEN" https://gitlab.example.com/api/v4/groups/group-of-alike-projects/projects?simple=true | \ jq -r '.[] | "\(.path_with_namespace)\t\(.id)"' group-of-alike-projects/project-pasiphae 7 group-of-alike-projects/project-megaclite 6 group-of-alike-projects/project-helike 5 group-of-alike-projects/project-erinome 4 group-of-alike-projects/project-callisto 3 group-of-alike-projects/project-aitne 2 group-of-alike-projects/project-adrastea 1
.gitlab-ci.yml
added to the common-ci-config .gitlab-ci.yml
:
stages: - distribute distribute: stage: distribute script: - ./distribute.sh
distribute.sh
script, which so far displays information about the commit and projects from the selected group:
#!/usr/bin/env bash curl -s --header "PRIVATE-TOKEN: $DISTRIBUTOR_TOKEN" https://gitlab.example.com/api/v4/projects/infra%2Fcommon-ci-config/repository/commits/$CI_COMMIT_SHA | jq '.' curl -s --header "PRIVATE-TOKEN: $DISTRIBUTOR_TOKEN" https://gitlab.example.com/api/v4/groups/group-of-alike-projects/projects?simple=true | \ jq -r '.[] | "\(.path_with_namespace)\t\(.id)"'
Running with gitlab-runner 10.1.0 (c1ecf97f) on gitlab (d82a6d8f) Using Shell executor... Running on gitlab... Fetching changes... HEAD is now at 08dcc92 Initial .gitlab-ci.yml and distribute.sh Checking out 08dcc92a as master... Skipping Git submodules setup $ ./distribute.sh { "id": "08dcc92abf0d951194ad1ffcc23deeb875855320", "short_id": "08dcc92a", "title": "Initial .gitlab-ci.yml and distribute.sh", "created_at": "2017-10-25T16:35:15.000+03:00", "parent_ids": [ "d9bdea91d081025c2af658209f23f684c96b5cee" ], "message": "Initial .gitlab-ci.yml and distribute.sh\n", "author_name": "root root", "author_email": "root.root@gitlab.example.com", "authored_date": "2017-10-25T16:35:15.000+03:00", "committer_name": "root root", "committer_email": "root.root@gitlab.example.com", "committed_date": "2017-10-25T16:35:15.000+03:00", "stats": { "additions": 0, "deletions": 0, "total": 0 }, "status": "running", "last_pipeline": { "id": 2, "sha": "08dcc92abf0d951194ad1ffcc23deeb875855320", "ref": "master", "status": "running" } } group-of-alike-projects/project-pasiphae 7 group-of-alike-projects/project-megaclite 6 group-of-alike-projects/project-helike 5 group-of-alike-projects/project-erinome 4 group-of-alike-projects/project-callisto 3 group-of-alike-projects/project-aitne 2 group-of-alike-projects/project-adrastea 1 Job succeeded
.gitalb-ci.yml
. In order not to confuse it with the .gitlab-ci.yml
project of the common-ci-config project, the file is located in the common
directory. The file describes a simple automatic task:
# common/.gitlab-ci.yml stages: - build build: stage: build script: - echo Building project $CI_PROJECT_PATH
distribute.sh
script already has information about the commit and a list of projects. In order to get a beautiful commit into the projects, you need to select the author’s name and mail and the full message of the commit. You also need to add a cycle on the received projects and for each project, call the method that creates the commit.
distribute.sh
:
#!/usr/bin/env bash COMMIT_INFO=$(curl -s --header "PRIVATE-TOKEN: $DISTRIBUTOR_TOKEN" https://gitlab.example.com/api/v4/projects/infra%2Fcommon-ci-config/repository/commits/$CI_COMMIT_SHA) # , jq -r MESSAGE=$(echo "$COMMIT_INFO" | jq '.message') AUTHOR_NAME=$(echo "$COMMIT_INFO" | jq -r '.author_name') AUTHOR_EMAIL=$(echo "$COMMIT_INFO" | jq -r '.author_email') CONTENT=$(base64 -w0 common/.gitlab-ci.yml) PAYLOAD=$(cat <<- JSON { "branch": "master", "commit_message": $MESSAGE, "author_name": "$AUTHOR_NAME", "author_email": "$AUTHOR_EMAIL", "actions": [ { "action": "update", "file_path": ".gitlab-ci.yml", "content": "$CONTENT", "encoding": "base64" } ] } JSON ) echo "$PAYLOAD" curl -s --header "PRIVATE-TOKEN: $DISTRIBUTOR_TOKEN" https://gitlab.example.com/api/v4/groups/group-of-alike-projects/projects?simple=true | \ jq -r '.[] | "\(.path_with_namespace)\t\(.id)"' | \ while read project do name=`echo $project | awk '{print $1}'` id=`echo $project | awk '{print $2}'` echo Update project $name curl -s --request POST --header "PRIVATE-TOKEN: $DISTRIBUTOR_TOKEN" \ --header "Content-Type: application/json" \ --data "$PAYLOAD" https://gitlab.example.com/api/v4/projects/$id/repository/commits done echo Stop
distribute.sh
script adds commits to several projects at once. This leads to the creation of a new pipeline and the simultaneous launch of assembly orders. This effect is not always needed. To prevent a commit that updates .gitlab-ci.yml
from running the build, you can first set up a task with a warning message:
script: - 'if [ "x$GITLAB_USER_NAME" == "xgitlab-ci-distributor" ] ; then echo -e "\033[0;31m\n\n .gitlab-ci.yml .\n\n\033[0m"; exit 1; fi'
GITLAB_USER_NAME
appeared in GitLab 10.0 ( release of September 22, 2017) . In earlier versions there is only GITLAB_USER_ID
and for the condition you will have to use the user ID. This ID can be found, for example, by running the task with script: [export]
or with such an API request:
curl -s --header "PRIVATE-TOKEN: $DISTRIBUTOR_TOKEN" https://gitlab.example.com/api/v4/users?username=gitlab-ci-distributor | jq '.[] | .id'
Vagrantfile
: change the base image to ubuntu/xenial64
and increase the memory of vb.memory = "3072"
. And after launching add gitlab-runner according to the instructions
Source: https://habr.com/ru/post/340996/