我对 Gitlab CI/CD 非常陌生,我已经非常仔细地阅读了它的文档,了解如何使用
.gitlab-ci.yml
文件创建新的 CI/CD 流程。正如我发现的,为了实现持续部署(也称为 CD),需要在我的 Linux 服务器上注册一个新的 gitlab-runner。
解释
这是我的
.gitlab-ci.yml
文件:
stages:
- build
- deploy
docker-build:
image: docker:stable
services:
- docker:dind
stage: build
only:
refs:
- ci-test
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build --pull -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA .
- echo $CI_REGISTRY_IMAGE
- echo $CI_COMMIT_SHORT_SHA
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $CI_REGISTRY_IMAGE:latest
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
- docker push $CI_REGISTRY_IMAGE:latest
deploy:
image: docker:stable
stage: deploy
services:
- docker:dind
only:
refs:
- ci-test
when: manual
except:
changes:
- "*.md"
script:
- docker build --pull -t $CI_REGISTRY_IMAGE$CI_COMMIT_SHORT_SHA .
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $CI_REGISTRY_IMAGE:latest
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
- docker push $CI_REGISTRY_IMAGE:latest
这是我的 Dockerfile:
version: '3'
services:
app:
# build: .
environment:
- DEFAULT_DATABASE_HOST=${DEFAULT_DATABASE_HOST}
- DEFAULT_DATABASE_NAME=${DEFAULT_DATABASE_NAME}
- DEFAULT_DATABASE_USER=${DEFAULT_DATABASE_USER}
- DEFAULT_DATABASE_PASSWORD=${DEFAULT_DATABASE_PASSWORD}
- DEBUG=${DEBUG}
- DJANGO_SETTINGS_MODULE=_base.settings
- CELERY_BROKER_URL=redis://redis:6379/0
# command: uwsgi --http :8000 --socket /socket/api.sock --chmod-socket=666 --module _base.wsgi --master --processes 5 --threads 2
command: uwsgi --http :8000 --module _base.wsgi --master --processes 5 --threads 2
# command: python manage.py runserver 0.0.0.0:8000
depends_on:
- redis
ports:
- 8000:8000
image: ${CLEARSIGHT_IMAGE_NAME}:${CLEARSIGHT_IMAGE_TAG}
restart: on-failure
network_mode: host
redis:
image: redis:6.0-alpine
volumes:
- /var/lib/redis/redis.dump:
ports:
- 6379:6379
#
celery-beat:
environment:
- DEFAULT_DATABASE_HOST=${DEFAULT_DATABASE_HOST}
- DEFAULT_DATABASE_NAME=${DEFAULT_DATABASE_NAME}
- DEFAULT_DATABASE_USER=${DEFAULT_DATABASE_USER}
- DEFAULT_DATABASE_PASSWORD=${DEFAULT_DATABASE_PASSWORD}
- DEBUG=${DEBUG}
- DJANGO_SETTINGS_MODULE=_base.settings
- CELERY_BROKER_URL=redis://redis:6379/0
command: celery -A _base beat -l info
depends_on:
- redis
image: ${CLEARSIGHT_IMAGE_NAME}:${CLEARSIGHT_IMAGE_TAG}
celery-worker-default:
environment:
- DEFAULT_DATABASE_HOST=172.17.0.1
- DEFAULT_DATABASE_NAME=${DEFAULT_DATABASE_NAME}
- DEFAULT_DATABASE_USER=${DEFAULT_DATABASE_USER}
- DEFAULT_DATABASE_PASSWORD=${DEFAULT_DATABASE_PASSWORD}
- DEBUG=${DEBUG}
- DJANGO_SETTINGS_MODULE=_base.settings
- CELERY_BROKER_URL=redis://redis:6379/0
command: celery -A _base worker -l INFO -Q clearsight-default --concurrency 1 -n clearsight-default
depends_on:
- redis
image: ${CLEARSIGHT_IMAGE_NAME}:${CLEARSIGHT_IMAGE_TAG}
celery-worker-aws:
environment:
- DEFAULT_DATABASE_HOST=172.17.0.1
- DEFAULT_DATABASE_NAME=${DEFAULT_DATABASE_NAME}
- DEFAULT_DATABASE_USER=${DEFAULT_DATABASE_USER}
- DEFAULT_DATABASE_PASSWORD=${DEFAULT_DATABASE_PASSWORD}
- DEBUG=${DEBUG}
- DJANGO_SETTINGS_MODULE=_base.settings
- CELERY_BROKER_URL=redis://redis:6379/0
command: celery -A _base worker -l INFO -Q clearsight-aws --concurrency 1 -n clearsight-default
depends_on:
- redis
image: ${CLEARSIGHT_IMAGE_NAME}:${CLEARSIGHT_IMAGE_TAG}
watchtower:
image: containrrr/watchtower:1.3.0
container_name: watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ~/.docker/config.json:/config.json
restart: always
command: --interval 60
问题 我已经尝试过 CI 管道,它可以与共享运行器正常工作:
所以,我认为CI方面没有任何问题。因此,我在服务器中注册了一个新的
gitlab-runner
(docker +机器),并且可以在存储库CI / CD运行器中看到它,但如下图所示,它没有连接!
问题 如何解决运行程序问题并使运行程序连接到作业?
运行
gitlab-runner verify
命令后问题解决了。
~ ➤ gitlab-runner -h # shows the help and other commands of gitlab-runner
NAME:
gitlab-runner - a GitLab Runner
USAGE:
gitlab-runner [global options] command [command options] [arguments...]
VERSION:
13.11.0 (7f7a4bb0)
AUTHOR:
GitLab Inc. <[email protected]>
COMMANDS:
exec execute a build locally
list List all configured runners
run run multi runner service
register register a new runner
install install service
uninstall uninstall service
start start service
stop stop service
restart restart service
status get status of a service
run-single start single runner
unregister unregister specific runner
verify verify all registered runners
artifacts-downloader download and extract build artifacts (internal)
artifacts-uploader create and upload build artifacts (internal)
cache-archiver create and upload cache artifacts (internal)
cache-extractor download and extract cache artifacts (internal)
cache-init changed permissions for cache paths (internal)
health-check check health for a specific address
read-logs reads job logs from a file, used by kubernetes executor (internal)
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--cpuprofile value write cpu profile to file [$CPU_PROFILE]
--debug debug mode [$DEBUG]
--log-format value Choose log format (options: runner, text, json) [$LOG_FORMAT]
--log-level value, -l value Log level (options: debug, info, warn, error, fatal, panic) [$LOG_LEVEL]
--help, -h show help
--version, -v print the version
现在可以正常使用了:
感谢@WytrzymałyWiktor 对this 帖子的评论。除了他的评论之外,我没有发现任何有用的东西。
P.S 执行上述步骤后,您可能需要运行
gitlab-runner start
才能解决您的问题!
对我来说
sudo gitlab-runner verify
有效。要执行的第二个命令是sudo gitlab-runner run
。对于自定义运行器,手动执行运行器非常重要。
对于那些在 Docker 下使用 GitLab Runner 的用户,请使用上面 @MostafaGhadimi 提供的等效命令来验证您的新实例:
docker exec <container name> /bin/sh -c "gitlab-runner verify"
跑步
gitlab-runner 验证
使其再次连接。
对我来说
sudo gitlab-runner verify
有效