5

I would like to execute an external (on the local machine) bash script from gitlab-ci.yml which uses the docker:stable image. I would like to execute startup.sh located outside the gitlab docker image. Is this possible or are there better options?

gitlab-ci.yaml

image: docker:stable

#Build script

variables:
  CI_DEBUG_TRACE: "true"
  DOCKER_DRIVER: overlay

before_script:
  - docker --version

build:
  services:
  - docker:dind
  script:
    - docker build --no-cache -t <tag> .
    - docker login -u root -p <pass> <registry>
    - docker tag ...
    - docker push ...
    - echo "build completed"
  stage: build
  tags:
    - <tag>

deploy_staging:
  stage: deploy
  script:
    - ./sh startup.sh

bash script

#!/bin/bash

docker login -u root -p <pass>
docker pull <image>
docker-compose up -d
David Maze
  • 130,717
  • 29
  • 175
  • 215
BobbyOrlando
  • 148
  • 1
  • 2
  • 8
  • can you elaborate what exactly you want achieve with this setup. Questions is quite confusing. – Akshay barahate May 20 '19 at 12:58
  • Where you want to execute your external script inside container or outside container? – Akshay barahate May 20 '19 at 13:05
  • So my setup is this currently: I have a docker gitlab instance on my server, when I commit code to my repository it automatically builds a docker image with updated code and pushes it to the gitlab registry as a "latest" image. Now when the build is done, the bash script should automatically pull the new image from the gitlab registry, docker compose it up so it recreates the container which serves a website. The build script (gitlab-ci.yaml) happens entirely inside a docker container, so I don't know how it can access the bash script on the host machine. I hope that clears it up? – BobbyOrlando May 21 '19 at 11:35

1 Answers1

0

I am not sure if this is the best practice for your use-case but the simple way to share files with an image is to add volume and share this code to the image by editing your config.toml file .

add this line to config.toml under [runners.docker]

volumes = ["/cache",path to startup.sh:/root/scripts"]

and then inside your.gilatb.yml

    deploy_staging:
      stage: deploy
      script:
        - chmod +x /root/scripts/startup.sh     
        - ./sh /root/scripts/startup.sh
Naor Tedgi
  • 5,204
  • 3
  • 21
  • 48
  • This would execute the sh inside the docker image created in the gitlab-ci.yml file, not on the server where the file is located, right? – BvdVen Jun 25 '19 at 07:39