Skip to content
Snippets Groups Projects
.gitlab-ci.yml 3.07 KiB
Newer Older
Tim Repke's avatar
Tim Repke committed
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/reference/pip_install/#caching
#
# If you want to also cache the installed packages, you have to install
# them in a virtualenv and cache it as well.
cache:
  paths:
    - .cache/pip
    - venv/

Tim Repke's avatar
Tim Repke committed
before_script:
  # Provides credentials to pip to access private GitLab PyPi index.
  - echo "machine gitlab.pik-potsdam.de" > ~/.netrc
  - echo "login gitlab-ci-token" >> ~/.netrc
  - echo "password ${CI_JOB_TOKEN}" >> ~/.netrc

Tim Repke's avatar
Tim Repke committed
stages:
  - build
  - test
  - deploy

Tim Repke's avatar
Tim Repke committed
build-job:
  stage: build
Tim Repke's avatar
Tim Repke committed
  image: python:3.10.9
  script:
    - python -V
    - pip install virtualenv
    - virtualenv venv
    - source venv/bin/activate
    - pwd
    - ls -lisah
Tim Repke's avatar
Tim Repke committed
    - git config --global url."https://gitlab.pik-potsdam.de/".insteadOf "ssh://git@gitlab.pik-potsdam.de/"
    - pip install -r requirements.txt
    - pip install -r requirements_dev.txt
Tim Repke's avatar
Tim Repke committed

test-job1:
  stage: test
Tim Repke's avatar
Tim Repke committed
  image: python:3.10.9
  script:
    - source venv/bin/activate
    - flake8 --config .flake8
Tim Repke's avatar
Tim Repke committed

test-job2:
  stage: test
Tim Repke's avatar
Tim Repke committed
  image: python:3.10.9
  script:
    - source venv/bin/activate
Tim Repke's avatar
Tim Repke committed
    - which python
    - pip freeze
    - python -m mypy --config-file=pyproject.toml server
Tim Repke's avatar
Tim Repke committed

Tim Repke's avatar
Tim Repke committed
deploy-to-production:
  stage: deploy
  tags:
    - bare-metal
  script:
    - echo "Current working directory and user"
    - pwd
    - whoami
    - groups
    - echo $HOME
    - echo "Reset git config"
    - rm /home/gitlab-runner/.gitconfig
Tim Repke's avatar
Tim Repke committed
    - git config --global url."https://gitlab.pik-potsdam.de/".insteadOf "ssh://git@gitlab.pik-potsdam.de/"
Tim Repke's avatar
Tim Repke committed
    - git config --global user.name gitlab-runner
    - git config --global user.email gitlab-runner@gitlab.pik-potsdam.de
    - git config --global -l --show-origin
    - echo "Go to deployment location"
    - cd /home/nacsos/nacsos-core
    - ls -lisah
    - sudo chown -R gitlab-runner /home/nacsos/nacsos-core
    - sudo chgrp -R gitlab-runner /home/nacsos/nacsos-core
    - ls -lisah
    - echo "Stopping NACSOS-core service"
    - sudo systemctl stop nacsos-core.service
    - echo "Dropping virtual environment"
    - rm -rf venv
    - echo "Fetching updated source"
    - git stash  # "reset" softly by stashing (in case files changed)
    - git pull origin production  # pull from origin (production branch)
    - echo "Creating new virtual environment"
    - python3.10 -m venv venv
    - source venv/bin/activate
Tim Repke's avatar
Tim Repke committed
    - which python
    - python -V
Tim Repke's avatar
Tim Repke committed
    - echo "Installing requirements"
    - pip install -r requirements.txt
    - echo "Handling migrations"
Tim Repke's avatar
Tim Repke committed
    - nacsos_migrate upgrade --revision head
Tim Repke's avatar
Tim Repke committed
    - sudo chown -R nacsos /home/nacsos/nacsos-core
    - sudo chgrp -R nacsos /home/nacsos/nacsos-core
    - echo "Starting NACSOS-core service"
    - sudo systemctl start nacsos-core.service
# DEPRECATED
#    - echo "Run as nacsos user"
#    - sudo -u nacsos bash ./deploy.sh $CI_JOB_TOKEN
#    - echo "Becoming 'nacsos' user"
#    - sudo -u nacsos -s
  when: manual
  only:
    - production