151 lines
3.8 KiB
YAML
151 lines
3.8 KiB
YAML
stages:
|
|
- validate
|
|
- test
|
|
- analyze
|
|
- container
|
|
- package
|
|
- deploy
|
|
|
|
workflow:
|
|
rules:
|
|
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
|
|
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
|
- when: never
|
|
|
|
default:
|
|
interruptible: true
|
|
|
|
.python-job:
|
|
image: python:3.12-slim
|
|
cache:
|
|
key: "pip-${CI_COMMIT_REF_SLUG}"
|
|
paths:
|
|
- .cache/pip
|
|
variables:
|
|
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
|
|
PIP_DISABLE_PIP_VERSION_CHECK: "1"
|
|
before_script:
|
|
- python -m pip install -e '.[dev]'
|
|
|
|
quality:
|
|
extends: .python-job
|
|
stage: validate
|
|
script:
|
|
- ruff check .
|
|
- ruff format --check .
|
|
- mypy src
|
|
- bash -n deploy/scripts/warehouse-allocation-deploy
|
|
- apt-get update -qq
|
|
- apt-get install -y --no-install-recommends shellcheck >/dev/null
|
|
- shellcheck deploy/scripts/warehouse-allocation-deploy
|
|
|
|
tests:
|
|
extends: .python-job
|
|
stage: test
|
|
services:
|
|
- name: redis:7.4-alpine
|
|
alias: redis
|
|
variables:
|
|
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
|
|
PIP_DISABLE_PIP_VERSION_CHECK: "1"
|
|
WA_TEST_REDIS_URL: "redis://redis:6379/15"
|
|
script:
|
|
- pytest --junitxml=junit.xml --cov-report=xml
|
|
artifacts:
|
|
when: always
|
|
expire_in: 14 days
|
|
reports:
|
|
junit: junit.xml
|
|
coverage_report:
|
|
coverage_format: cobertura
|
|
path: coverage.xml
|
|
paths:
|
|
- coverage.xml
|
|
|
|
sonarqube:
|
|
stage: analyze
|
|
image:
|
|
name: sonarsource/sonar-scanner-cli:11
|
|
entrypoint: [""]
|
|
needs:
|
|
- job: tests
|
|
artifacts: true
|
|
variables:
|
|
SONAR_USER_HOME: "$CI_PROJECT_DIR/.sonar"
|
|
GIT_DEPTH: "0"
|
|
cache:
|
|
key: sonar
|
|
paths:
|
|
- .sonar/cache
|
|
script:
|
|
- sonar-scanner -Dsonar.qualitygate.wait=true
|
|
rules:
|
|
- if: $SONAR_HOST_URL && $SONAR_TOKEN
|
|
- when: never
|
|
|
|
container-build:
|
|
stage: container
|
|
image:
|
|
name: gcr.io/kaniko-project/executor:v1.23.2-debug
|
|
entrypoint: [""]
|
|
script:
|
|
- >-
|
|
/kaniko/executor
|
|
--context "$CI_PROJECT_DIR"
|
|
--dockerfile "$CI_PROJECT_DIR/Dockerfile"
|
|
--destination "local/warehouse-allocation:${CI_COMMIT_SHA}"
|
|
--no-push
|
|
|
|
package-release:
|
|
stage: package
|
|
image: alpine:3.22
|
|
needs:
|
|
- quality
|
|
- tests
|
|
- container-build
|
|
- job: sonarqube
|
|
optional: true
|
|
before_script:
|
|
- apk add --no-cache git
|
|
script:
|
|
- git archive --format=tar.gz --output="release-${CI_COMMIT_SHA}.tar.gz" HEAD
|
|
- sha256sum "release-${CI_COMMIT_SHA}.tar.gz" > "release-${CI_COMMIT_SHA}.sha256"
|
|
artifacts:
|
|
expire_in: 7 days
|
|
paths:
|
|
- "release-${CI_COMMIT_SHA}.tar.gz"
|
|
- "release-${CI_COMMIT_SHA}.sha256"
|
|
rules:
|
|
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_REF_PROTECTED == "true"'
|
|
deploy-production:
|
|
stage: deploy
|
|
image: alpine:3.22
|
|
interruptible: false
|
|
resource_group: production
|
|
needs:
|
|
- job: package-release
|
|
artifacts: true
|
|
environment:
|
|
name: production
|
|
url: https://warehouse-allocation.mvphub.ir
|
|
before_script:
|
|
- apk add --no-cache openssh-client
|
|
- install -d -m 0700 "$HOME/.ssh"
|
|
- install -m 0600 "$DEPLOY_SSH_PRIVATE_KEY" "$HOME/.ssh/id_ed25519"
|
|
- install -m 0600 "$DEPLOY_KNOWN_HOSTS" "$HOME/.ssh/known_hosts"
|
|
script:
|
|
- test "$DEPLOY_HOST" = "188.137.233.91"
|
|
- test "$DEPLOY_PORT" = "22"
|
|
- test "$DEPLOY_USER" = "warehouse-deploy"
|
|
- checksum="$(cut -d ' ' -f 1 "release-${CI_COMMIT_SHA}.sha256")"
|
|
- >-
|
|
scp -P "$DEPLOY_PORT" -o BatchMode=yes
|
|
"release-${CI_COMMIT_SHA}.tar.gz"
|
|
"${DEPLOY_USER}@${DEPLOY_HOST}:incoming/release-${CI_COMMIT_SHA}.tar.gz"
|
|
- >-
|
|
ssh -p "$DEPLOY_PORT" -o BatchMode=yes
|
|
"${DEPLOY_USER}@${DEPLOY_HOST}"
|
|
"sudo /usr/local/sbin/warehouse-allocation-deploy '${CI_COMMIT_SHA}' '${checksum}'"
|
|
rules:
|
|
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_REF_PROTECTED == "true"'
|