【问题标题】:deploy after gitlab runner completes buildgitlab runner 完成构建后部署
【发布时间】:2017-01-17 22:44:47
【问题描述】:

我想使用 gitlab 运行器部署成功构建的 docker 映像,但我不确定如何使用 .gitlab-ci.yml 中的部署阶段来执行此操作。构建日志显示在构建过程中在 docker 映像上正确创建了数据库。

我在 Mac (OSX 10.11.6) 上本地使用 docker 来构建我的 docker 容器。 Gitlab 正在远程运行。我注册了一个特定的本地跑步者来处理构建。当我将更改推送到我的项目时,gitlab CI 会运行构建脚本来创建一个测试数据库。图像构建后会发生什么?我的本地计算机上没有列出已完成构建的 docker 映像。 gitlab-runner-prebuilt-x86_64 是一个准系统 linux 映像,与构建无关。

https://docs.gitlab.com/ce/ci/docker/using_docker_build.html

http://container-solutions.com/running-docker-in-jenkins-in-docker/

>gitlab-ci-multi-runner list
Listing configured runners                          ConfigFile=/Users/username/.gitlab-runner/config.toml
local-docker-executor                               Executor=docker Token=[token] URL=http://gitlab.url/ci

>docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
gitlab-runner-prebuilt-x86_64   f6fdece             [id1]        25 hours ago        50.87 MB
php7                            latest              [id2]        26 hours ago        881.8 MB
ubuntu                          latest              [id3]        13 days ago         126.6 MB
docker                          latest              [id4]        2 weeks ago         104.9 MB

.gitlab-ci.yml:

image: php7:latest

# build_image:
#   script:
#     - docker build -t php7 .

# Define commands that run before each job's script
# before_script:
#   - docker info

# Define build stages
  # First, all jobs of build are executed in parallel.
  # If all jobs of build succeed, the test jobs are executed in parallel.
  # If all jobs of test succeed, the deploy jobs are executed in parallel.
  # If all jobs of deploy succeed, the commit is marked as success.
  # If any of the previous jobs fails, the commit is marked as failed and no jobs of further stage are executed.
stages:
  - build
  - test
  - deploy     

variables:
  db_name: db_test
  db_schema: "db_test_schema.sql"

build_job1:
  stage: build
  script:
    - service mysql start
    - echo "create database $db_name" | mysql -u root
    - mysql -u root $db_name < $db_schema
    - mysql -u root -e "show databases; use $db_name; show tables;"
    #- echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');" | mysql -u root   
    #- echo "run unit test command here"
  #Defines a list of tags which are used to select Runner  
  tags:
    - docker

deploy_job1:
  stage: deploy
  #this script is run inside the docker container
  script:
    - whoami
    - pwd
    - ls -la
    - ls /
    #Usage: docker push [OPTIONS] NAME[:TAG]
    #Push an image or a repository to a registry
    - docker push deploy:latest
  #gitlab runners will look for and run jobs with these tags 
  tags:
    - docker

config.toml:

concurrent = 1
check_interval = 0

[[runners]]
  name = "local-docker-executor"
  url = "http://gitlab.url/ci"
  token = "[token]"
  executor = "docker"
  builds_dir = "/Users/username/DOCKER_BUILD_DIR"
  [runners.docker]
    tls_verify = false
    image = "ubuntu:latest"
    privileged = false
    disable_cache = false
    volumes = ["/cache"]
  [runners.cache]

Dockerfile:

FROM ubuntu:latest

#https://github.com/sameersbn/docker-mysql/blob/master/Dockerfile
ENV DEBIAN_FRONTEND noninteractive                                      
ENV MYSQL_USER mysql                                                    
ENV MYSQL_DATA_DIR /var/lib/mysql                                       
ENV MYSQL_RUN_DIR /run/mysqld                                           
ENV MYSQL_LOG_DIR /var/log/mysql                                        
ENV DB_NAME "db_test"                                               
ENV DB_IMPORT "db_test_schema.sql"                                  

# RUN apt-get update && \
#       apt-get -y install sudo
# RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
# USER docker
# CMD /bin/bash

RUN apt-get update                                                      \
 && DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server      
 # \
 # && rm -rf ${MYSQL_DATA_DIR}                                          \
 # && rm -rf /var/lib/apt/lists/*

ADD ${DB_IMPORT} /tmp/${DB_IMPORT}


# #RUN /usr/bin/sudo service mysql start                                                    \
# RUN service mysql start                                                   \
#  && mysql -u root -e "CREATE DATABASE $DB_NAME"                           \
#  && mysql -u root $DB_NAME < /tmp/$DB_IMPORT

RUN locale-gen en_US.UTF-8 \
    && export LANG=en_US.UTF-8 \
    && apt-get update \
    && apt-get -y install apache2 libapache2-mod-php7.0 php7.0 php7.0-cli php-xdebug php7.0-mbstring php7.0-mysql php-memcached php-pear php7.0-dev php7.0-json vim git-core libssl-dev libsslcommon2-dev openssl libssl-dev \
    && a2enmod headers 

ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
RUN ln -sf /dev/stdout /var/log/apache2/access.log && \
    ln -sf /dev/stderr /var/log/apache2/error.log
RUN mkdir -p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR


#VOLUME [ "/var/www/html" ]
WORKDIR /var/www/html

EXPOSE 80 3306

#ENTRYPOINT [ "/usr/sbin/apache2" ]
#CMD ["-D", "FOREGROUND"]

#ENTRYPOINT ["/bin/bash"]

【问题讨论】:

  • 我可能是错的,但你的问题与 Dockers 的关系比 Gitlabs 更重要,对吧?
  • 我想让 GitLab 运行器部署我的 docker 映像。为此,看起来我需要通过套接字或通过在图像上安装 docker 让 docker 作为服务在图像上运行。

标签: docker deployment gitlab-ci gitlab-ci-runner


【解决方案1】:

您没有在 CI 上构建任何 docker 映像。

您正在使用来自 DockerHub 的 php7 映像来执行所有作业。这包括工作deploy_job1,它试图使用docker 二进制来推送不在该容器内的图像(deploy:latest)。另外,我认为docker 二进制文件不包含在php7 图像中。

我猜你想在你的 Mac 上推送你本地构建的镜像,不是吗?在这种情况下,您需要使用另一个运行器,它的执行器应该是shell。在这种情况下,您将有 2 个运行器,一个使用 docker 运行 build_job1 作业,另一个用于推送本地构建的图像。但是有一个更好的解决方案是手动构建docker镜像,就是让GitLab CI来构建它。

所以,修改你的.gitlab-ci.yml(删除你的 cmets,添加地雷进行解释):

# Removed global image definition

stages:
  - build
  - test
  - deploy     

variables:
  db_name: db_test
  db_schema: "db_test_schema.sql"

build_job1:
  stage: build
  # Use image ONLY in docker runner
  image: php7:latest
  script:
    - service mysql start
    - echo "create database $db_name" | mysql -u root
    - mysql -u root $db_name < $db_schema
    - mysql -u root -e "show databases; use $db_name; show tables;"
  # Run on runner with docker executor, this is ok
  tags:
    - docker

deploy_job1:
  stage: deploy
  script:
    # Build the docker image first, and then push it
    - docker build -t deploy:latest .
    - docker push deploy:latest
  # Run on runner with shell executor, set proper tag
  tags:
    - docker_builder

注册新的runner时,设置executor为shell,tag为docker_builder。我假设你已经在你的 Mac 上安装了 docker 引擎。

另一方面,这个例子毫无意义,至少对我来说是这样。 build 阶段什么都不做,因为容器是短暂的。我想你应该在 Dockerfile 上这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多