Skip to content
Snippets Groups Projects
.gitlab-ci.yml 2.51 KiB
image: docker

services:
  - docker:dind

stages:
  - build
  - build-release
  - deploy


build: #This will be executed on every push. It builds an docker image, which is named after the branch, so you can always use the last state of the different branches.
  stage: build
  script:
    - echo "building for branch $CI_COMMIT_REF_NAME"
    - sudo docker image prune -f #Delete old unused images
    - sudo docker build -t gitlab.jonasled.de/jonasled/url_shorter_docker:$CI_COMMIT_REF_NAME . #Build the image with the name already set to push
    - sudo docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY #Login to docker registry, username and password will be filled while executing
    - sudo docker push gitlab.jonasled.de/jonasled/url_shorter_docker:$CI_COMMIT_REF_NAME #Push the image onto the Docker registry.


build-release: #This will be executed if you push on master, it makes a new release (latest) image
  stage: build
  only:
    - master
  script:
    - export version='cat VERSION'
    - echo "building branch $CI_COMMIT_REF_NAME, Version $version"
    - 'curl -X POSRT --silent --show-error --fail --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"https://gitlab.jonasled.de/api/v4/projects/${CI_PROJECT_ID}/repository/tags?tag_name=$version&ref=${CI_COMMIT_SHS}' #Tag the current commit with the version.
    - sudo docker image prune -f #Delete old unused images
    - sudo docker build -t gitlab.jonasled.de/jonasled/url_shorter_docker . #Build the image with the name already set to push
    - sudo docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY #Login to docker registry, username and password will be filled while executing
    - sudo docker push gitlab.jonasled.de/jonasled/url_shorter_docker #Push the image onto the Docker registry.


deploy:
  stage: deploy
  only:
    - master
  script:
    - sudo apt-get install -y python-pip #install pip, this is needed to install docker-compose in the next step
    - sudo pip install docker-compose #Install docker-compose with pip
    - replace "5000:5000" "5003:5000" -- docker-compose.yml #replace in docker-compose some settings. Variables will be replaced on execute.
    - replace "domains=" "$domains" -- docker-compose.yml #The domains I use for my url shorter
    - replace "recaptcha_private=" "$recaptcha_private" -- docker-compose.yml #Recaptcha keys for protecting the create form from attacks
    - replace "recaptcha_public=" "$recaptcha_public" -- docker-compose.yml
    - sudo docker-compose up -d #Start the new container
  environment: master