Skip to content
Snippets Groups Projects
.gitlab-ci.yml 3.18 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jonas Leder's avatar
    Jonas Leder committed
    image: docker
    
    services:
      - docker:dind
    
    stages:
    
      - build-release
    
    Jonas Leder's avatar
    Jonas Leder committed
      - 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
    
      except:
        - master
    
      script:
    
    Jonas Leder's avatar
    Jonas Leder committed
        - echo "building for branch $CI_COMMIT_REF_NAME"
        - sudo docker image prune -f #Delete old unused images
    
        - sudo docker pull python:3
    
        - 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
    
    Jonas Leder's avatar
    Jonas Leder committed
        - 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.
    
    Jonas Leder's avatar
    Jonas Leder committed
    build-release: #This will be executed if you push on master, it makes a new release (latest) image
    
      only:
        - master
    
    Jonas Leder's avatar
    Jonas Leder committed
        - export version=`cat VERSION`
    
        - echo "building branch $CI_COMMIT_REF_NAME, Version $version"
    
    Jonas Leder's avatar
    Jonas Leder committed
        - git config --global user.name "build"
        - git config --global user.email "noreply@jonasled.de"
        - git tag -a $version -m "$version" #add a tag with the current version
    
    Jonas Leder's avatar
    Jonas Leder committed
        - git push --tags -o ci.skip ssh://git@gitlab.jonasled.de:2222/jonasled/url_shorter_docker #Push the tag to the repo ci.skip means don't build the image again. I've configured an ssh key for the runner user,so it can push via ssh.
    
    Jonas Leder's avatar
    Jonas Leder committed
        - sudo docker image prune -f #Delete old unused images
    
        - sudo docker pull python:3
    
        - sudo docker build -t gitlab.jonasled.de/jonasled/url_shorter_docker . #Build the image with the name already set to push
    
    Jonas Leder's avatar
    Jonas Leder committed
        - 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.
    
    
    Jonas Leder's avatar
    Jonas Leder committed
    deploy:
    
    Jonas Leder's avatar
    Jonas Leder committed
      stage: deploy
      only:
        - master
      script:
    
    Jonas Leder's avatar
    Jonas Leder committed
        - 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.
    
    Jonas Leder's avatar
    Jonas Leder committed
        - replace "domains=" "domains=$domains" -- docker-compose.yml #The domains I use for my url shorter
        - replace "recaptcha_private=" "recaptcha_private=$recaptcha_private" -- docker-compose.yml #Recaptcha keys for protecting the create form from attacks
        - replace "recaptcha_public=" "recaptcha_public=$recaptcha_public" -- docker-compose.yml
        - replace "GITHUB_CLIENT_ID=" "GITHUB_CLIENT_ID=$GITHUB_CLIENT_ID" -- docker-compose.yml
        - replace "GITHUB_CLIENT_SECRET=" "GITHUB_CLIENT_SECRET=$GITHUB_CLIENT_SECRET" -- docker-compose.yml
    
        - replace "GOOGLE_CLIENT_ID=" "GOOGLE_CLIENT_ID=$GOOGLE_CLIENT_ID" -- docker-compose.yml
        - replace "GOOGLE_CLIENT_SECRET=" "GOOGLE_CLIENT_SECRET=$GOOGLE_CLIENT_SECRET" -- docker-compose.yml
    
    Jonas Leder's avatar
    Jonas Leder committed
        - sudo docker-compose up -d #Start the new container
    
    Jonas Leder's avatar
    Jonas Leder committed
      environment: master