项目作者: ueberdosis

项目描述 :
🦊 Docker Image with tools for GitLab CI
高级语言: Shell
项目地址: git://github.com/ueberdosis/gitlab-ci-build-tools.git
创建时间: 2019-06-17T12:51:21Z
项目社区:https://github.com/ueberdosis/gitlab-ci-build-tools

开源协议:GNU General Public License v3.0

下载


Docker Image 🐳 for GitLab CI 🦊

ARCHIVED in favor of https://github.com/ueberdosis/build-tools

This image contains a couple of useful tools and helpers and is meant to be used within GitLab CI by the CI runner.

Tools included

Getting started

Include the image via the image keyword in your .gitlab-ci.yml:

  1. image: ueberdosis/gitlab-ci-build-tools

Run the init command of the included ci helper script in the before_script section:

  1. before_script:
  2. - eval $(ci init)

The CI helper script

ci init

The init command connects to a registry and defines which compose file will be used for future docker-compose commands in the current pipeline.

Default usage:

  1. before_script:
  2. - eval $(ci init)

By default the script uses the GitLab container registry of the current project. You can specify your own registry with environment variables in the .gitlab-ci.yml:

  1. variables:
  2. REGISTRY: registry.example.com

For security reasons put the actual credentials in Settings → CI/CD → Variables:

  1. REGISTRY_USER: username
  2. REGISTRY_PASSWORD: super-secret-password-1234

By default the script uses the docker-compose.build.yml throughout the pipeline. To use another compose file (e.g. docker-compose.awesome.yml) pass the name (in this example awesome) as an argument:

  1. before_script:
  2. - eval $(ci init awesome)

To use a different compose file for certain jobs (e.g. testing stages), just run the init command again for this job:

  1. unit_tests:
  2. stage: tests
  3. before_script:
  4. - eval $(ci init testing)
  5. script:
  6. - echo "Test something!"

ci run

The run command starts all services from the previously selected compose file and executes the code from stdin.

It also prefixes the service names so concurrent jobs on the same Docker host are not interfering with each other and it can copy files and folder from a running container after the given script is finished. Of course it takes care of automatically stopping and removing containers, networks and volumes as well after your script finished.

Default usage:

  1. unit_tests:
  2. stage: tests
  3. script:
  4. - |
  5. ci run << CODE
  6. docker-compose exec -T php ./vendor/bin/phpunit
  7. CODE

If you want to run multiple instances in the same pipeline, pass a name as argument to the run command. This will prefix the resulting container names and allows concurrency:

  1. unit_tests:
  2. stage: tests
  3. script:
  4. - |
  5. ci run unit_tests << CODE
  6. docker-compose exec -T php ./vendor/bin/phpunit
  7. CODE
  8. browser_tests:
  9. stage: tests
  10. script:
  11. - |
  12. ci run browser_tests << CODE
  13. docker-compose exec -T php php artisan dusk
  14. CODE

To copy files and folders from a running container to the host machine you can use the --copy option by specifying the service name followed by a colon and the path in the container:

  1. browser_tests:
  2. stage: tests
  3. artifacts:
  4. when: on_failure
  5. paths:
  6. - screenshots
  7. script:
  8. - |
  9. ci run --copy php:/var/www/tests/Browser/screenshots << CODE
  10. docker-compose exec -T php php artisan:dusk
  11. CODE

The example above will create a new folder named screenshots in the current working directory with the contents of var/www/tests/Browser/screenshots from inside the php container. The copying is done after your script finished, so it can be used for artifacts storage or caching purposes.

ci down

The down command stops and deletes running containers, networks and volumes.

If you use the run command, invoking this command manually is not required. But it comes in handy for regular cleaning jobs in case someone terminated the pipeline prematurely.

Default usage:

  1. clean:
  2. stage: clean
  3. only:
  4. - schedules
  5. script:
  6. - ci down unit_tests
  7. - ci down browser_tests

ci ssh

The ssh command adds the private and public ssh keys stored in environment variables to the ssh-agent and disables host key verification.

Another common task in a pipeline is connecting to a remote host to deploy changes. The ssh command simplifies this process. Put the contents of your private and public ssh key in Settings → CI/CD → Variables as SSH_KEY and SSH_KEY_PUB and run the ssh command before the rest of your script:

Default usage:

  1. deploy:
  2. stage: deploy
  3. script:
  4. - ci ssh
  5. - rsync -arz --progress src/ remote-host:/var/www/

ci clean-registry

The clean-registry command can clean up your GitLab docker registry for the current project. (For other registries, use the included deckschrubber)

To use this command, the ci helper script needs API access to your GitLab instance. Add an access token with API and Registry access to your Settings → CI/CD → Variables as GITLAB_ACCESS_TOKEN. Pass the name of the image you want to delete repository tags in bulk for as an argument.

Default usage:

  1. clean-registry:
  2. stage: clean
  3. script:
  4. - ci clean-registry image-name

Advanced usage:

The ci helper script accepts the same parameters as the original API method documented here.

  1. clean-registry:
  2. stage: clean
  3. script:
  4. - ci clean-registry image-name --name_regex_delete '.*' --name_regex_keep 'master.*' --keep_n 5 --older_than 14d

Customization

By default the container names will be prefixed with project and pipeline id. You can customize this via an environment variable:

  1. variables:
  2. COMPOSE_PROJECT_NAME: your-desired-prefix

If you want to use a compose with a completely different name, you can specify this via an environment variable too:

  1. variables:
  2. COMPOSE_FILE: awesome-compose-file.yml

License

GNU General Public License v3.0