Search code examples
dockergo-cd

Run a docker container with gocd CI/CD


I have build a CI/CD with gitlab and docker. In my gitlab-ci.yml i just write commands to build and launch container to run test and that's it.

Now i m looking for a way to do the same thing with a GOCD ci/cd server. But the official documentation from gocd directly come to kubernetees or Docker in Docker or Docker outside Docker. I don't need this.

I have one GOCD-server and two GOCD-agents.

I want to build and run ONE container and perhaps deploy it. I m not able to find a simple tutorial to make a pipeline with three stages on GOCD:

  1. build the image;
  2. run container and test it;
  3. deploy.

Solution

  • Finally, i was able to get it working by myself, here are the following steps :

    Pre-requisites:

    • docker-ce and docker-compose must be installed on each agents
    • go user must be on group docker
    • /var/run/docker.sock must be on mode 666
    • each agent must have an access to your gitlab/github/repository hub and the fingerprint host must be added before first connection

    add manually the fingerprint host on each gocd-agent, you must be connected as go user (or don't forget to set the right owner to known_hosts file):

      ssh-keyscan -H your.repository.com >> /var/go/.ssh/known_hosts
    

    Then on gocd-server ui:

    1. create pipeline

    2. create a stage

    3. create a job

    4. create one or more tasks

      --> add a new task -> choose "more" and you will have the possibility to add a custom command

    at this point, you can add in the cell "commands"

     /usr/bin/docker
    

    or (depends of your use-case)

     /usr/local/bin/docker-compose
    

    and in the arguments list, one argument by line

     build
     .
     -f
     dockerfile
     -t
     yourrepository/tagOfYourImage
    

    After that, don't forget, to clean before and after each run, your containers and created images. You can also use "/bin/bash" in cell "commands" and then two arguments

     -c
     "/usr/bin/docker rmi $(/usr/bin/docker image ls|/usr/bin/grep server_php|/usr/bin/awk '{ print $3 }'|/usr/bin/tr '\r' ' ') --force||true"
    

    Tips:

    If you want all your jobs to success on each agent, don't forget to choose the option "Run on all agents" in the tab "Job settings".

    Update:

    If you restart docker service or one of your agent, it will remove the 666 permissons on /var/run/docker.sock, adding the user go to the group docker didn't help me. I have to set 666 permissions to this file again.