deploy after gitlab runner completes build

I’m want to use gitlab runners to deploy a successfully built docker image but I am not sure how to use the deploy stage in .gitlab-ci.yml to do this. The build log shows the database is properly created on the docker image during the build process.

I use docker locally on a Mac (OSX 10.11.6) to build my docker container. Gitlab is running remotely. I registered a specific local runner to handle the build. When I push changes to my project, gitlab CI runs the build script to create a test database. What happens to the image after it’s built? There is no docker image for the completed build listed on my local machine. The gitlab-runner-prebuilt-x86_64 is a barebones linux image that isn’t connected with the build.

  • non-loading with www dir, nginx, nette - set nginx path?
  • Using enviroment variables in a dockerfile RUN command
  • Running alembic in a python container that depends upon a mysql container
  • How do I avoid download images on all docker hosts which are part of my swarm?
  • Error when creating a MySQL database in a Dockerfile
  • Linking containers together on production deploys
  • https://docs.gitlab.com/ce/ci/docker/using_docker_build.html

    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"]
    

  • mysql with Exited(1) from docker
  • Running migrations with Rails in a Docker container with multiple container instances
  • Docker-compose: Copying files from local env to EC2 instance
  • Is it possible to assign a specific node to create container with docker swarm?
  • Docker Compose and execute command on starting container
  • how does incorrect Docker postGIS command RUN succesfully?
  • One Solution collect form web for “deploy after gitlab runner completes build”

    You’re not building any docker image on CI.

    You are using php7 image from DockerHub to execute all jobs. This includes the job deploy_job1, that are trying to use docker binary to push an image (deploy:latest) that is not inside that container. Additionaly, I think that docker binary is not included on the php7 image.

    I guess that you want to push the image that you build locally on your Mac, isn’t it? In that case, you need to use another runner, which its executor should be shell. On that scenario, you will have 2 runners, one using docker to run the build_job1 job, and another one to push the locally built image. But there is a better solution that build manually the docker image, and it’s to make GitLab CI to build it.

    So, modifing your .gitlab-ci.yml (removing your comments, adding mines for explanation):

    # 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
    

    When you register the new runner, set executor as shell and tags docker_builder. I’m asuming that you have installed docker engine on your Mac.

    On the other hand, this example makes no sense, at least for me. The build stage does nothing, as the container is ephemeral. I guess you should do that on the Dockerfile.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.