stages:
    - create_readme
    - build

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: ""
  DOCKER_VERSION: 19.03.8
  #Docker version MUST match the version in the runner config.toml
  DOCKER_HOST: tcp://docker:2376

services:
  - docker:19.03.8-dind

create_readme:
  stage: create_readme
  image: alpine:3.12
  script: 
    - echo "Creating README for repository."
    - source ./.gitlab/.readme_variables
    - echo $GITHUB_REPONAME
    - echo $DOCKER_REPONAME
    - echo $CREATOR_NAME
    - echo $CREATOR_REPO
    - echo $EXAMPLE_IMAGE
    - echo $EXAMPLE_WEBSITE
    - echo $DESCRIPTION
    - cp ./.gitlab/.readme_template ./README.md
    - sed -i "s/$GITHUB_REPONAME/${GITHUB_REPONAME/g}" README.md
    - sed -i "s/$DOCKER_REPONAME/${DOCKER_REPONAME/g}" README.md
    - sed -i "s/$CREATOR_NAME/${CREATOR_NAME/g}" README.md
    - sed -i "s/$CREATOR_REPO/${CREATOR_REPO/g}" README.md
    - sed -i "s/$EXAMPLE_IMAGE/${EXAMPLE_IMAGE/g}" README.md
    - sed -i "s/$EXAMPLE_WEBSITE/${EXAMPLE_WEBSITE/g}" README.md
    - sed -i "s/$DESCRIPTION/${DESCRIPTION/g}" README.md
  artifacts:
    expire_in: 1 week
    paths:
      - ./README.md

build:
  stage: build
  image: docker:$DOCKER_VERSION
  script:
    - echo "Build stage started."
    - docker info
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker pull $CI_REGISTRY_IMAGE:latest || true
    - docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
    - docker push $CI_REGISTRY_IMAGE:latest