Skip to content
Snippets Groups Projects
.gitlab-ci.yml 3.67 KiB
Newer Older
Lemni's avatar
Lemni committed
stages:
  - build
  - deploy

.prepare_pipenv: &prepare_pipenv  |
  export LC_ALL=C.UTF-8
  export LANG=C.UTF-8
  pip install pipenv --ignore-installed
  set +e
  pipenv --venv; NO_VENV=$? ; echo 'No fail'
  set -e
  if [ $NO_VENV -eq 1 ]; then
    pipenv --python 3.10
  fi
  #pipenv run pip install pip=="18.0"
  pipenv install --dev

.prepare_awscli: &prepare_awscli  |
  export LC_ALL=C.UTF-8
  export LANG=C.UTF-8
  pip install pipenv --ignore-installed
  set +e
  pipenv --venv; NO_VENV=$? ; echo 'No fail'
  set -e
  if [ $NO_VENV -eq 1 ]; then
    pipenv --python 3.10
  fi
  pipenv run pip install awscli

.get_ecscli: &get_ecscli |
  if [ -e ecs-cli ]
  then
    echo "ecs-cli already downloaded"
  else
    apk add curl
    curl -o ./ecs-cli https://amazon-ecs-cli.s3.amazonaws.com/ecs-cli-linux-amd64-latest
  fi
  chmod +x ecs-cli


variables:
   GIT_SUBMODULE_STRATEGY: recursive
   DOCKER_TLS_CERTDIR: ""


build_image_prod:
  stage: build
  image: docker
  services:
    - docker:dind
  before_script:
    - apk --no-cache add --virtual git build-base gcc libgcc musl-dev jpeg-dev zlib-dev libc-dev libffi-dev mariadb-dev openssl-dev python3-dev py3-pip
    - *prepare_awscli
  script:
    - $(pipenv run aws ecr get-login --no-include-email --region eu-west-1 )
    - |
      set +e
      docker pull "${AWS_REGISTRY_IMAGE}:$CI_BUILD_REF_NAME"
      set -e
    - docker build --cache-from "${AWS_REGISTRY_IMAGE}:$CI_BUILD_REF_NAME" -t "${AWS_REGISTRY_IMAGE}:$CI_BUILD_REF_NAME" .
    - docker push "$AWS_REGISTRY_IMAGE:$CI_BUILD_REF_NAME"
  only:
    - tags
  cache:
    key: docker
    paths:
      - .venv
      - .pip-cache
      - Pipfile.lock


deploy_prod:
  stage: deploy
  image: python:3.10-alpine
  environment: production
  before_script:
    - *get_ecscli
  only:
    - tags
  script:
    - export TAG=$CI_BUILD_REF_NAME
    - source backend/production.env
    - ./ecs-cli configure --region eu-west-1 --cluster spotfleet-gitlab
    - ./ecs-cli compose --file ecs-compose.yml  --ecs-params ecs-params.yml --project-name saf-prod service up --create-log-groups --target-group-arn $AWS_TG_ARN --container-name saf --container-port 80 --timeout 10 --role $AWS_ROLE
  cache:
    key: python
    paths:
      - .venv
      - .pip-cache
      - Pipfile.lock
      - ecs-cli


build_image_staging:
  stage: build
  image: docker
  services:
    - docker:dind
  before_script:
    - apk --no-cache add --virtual git build-base gcc libgcc musl-dev jpeg-dev zlib-dev libc-dev libffi-dev mariadb-dev openssl-dev python3-dev py3-pip
    - *prepare_awscli
  script:
    - $(pipenv run aws ecr get-login --no-include-email --region eu-west-1 )
    - |
      set +e
      docker pull "${AWS_REGISTRY_IMAGE}:PREPROD-$CI_BUILD_REF_NAME"
      set -e
    - docker build --cache-from "${AWS_REGISTRY_IMAGE}:PREPROD-$CI_BUILD_REF_NAME" -t "${AWS_REGISTRY_IMAGE}:PREPROD-$CI_BUILD_REF_NAME" .
    - docker push "$AWS_REGISTRY_IMAGE:PREPROD-$CI_BUILD_REF_NAME"
  except:
    - tags
  cache:
    key: docker
    paths:
      - .venv
      - .pip-cache
      - Pipfile.lock 


deploy_staging:
    stage: deploy
    image: python:3.10-alpine
    environment: staging
    before_script:
      - *get_ecscli
    except:
      - tags
    script:
      - export TAG=PREPROD-$CI_BUILD_REF_NAME
      - source backend/staging.env
      - ./ecs-cli configure --region eu-west-1 --cluster spotfleet-gitlab
      - ./ecs-cli compose --file ecs-compose.yml  --ecs-params ecs-params.yml --project-name saf-stag service up --create-log-groups --target-group-arn $AWS_TG_ARN --container-name saf --container-port 80 --timeout 10 --role $AWS_ROLE
    cache:
      key: python
      paths:
        - .venv
        - .pip-cache
        - Pipfile.lock
        - ecs-cli