.gitlab-ci _fornt_balancer

Pipe for build and deploy front
 avatar
Geksogen
yaml
2 years ago
3.0 kB
6
Indexable
Never
variables:
   VERSION: 1.0.${CI_PIPELINE_ID}
   MAVEN_REPO_PATH: ./.m2/repository
   JAVA_OPTS: -XX:MaxRAMPercentage=90

include:
  - project: 'templates/ci'
    file: 'DockerInDockerTemplate.yml'

stages:
  - build
  - release
  - deploy

.ssh_settings:
  before_script:
    ## устанавливаем ssh-agent для удобства аутентификации по ssh
    - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    #сохраняем сгенеренный ранее приватный ключ для раннера
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 600 ~/.ssh
    - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts


build-frontend:
  stage: build
  image: docker:20.10.12-dind-rootless
  before_script:
    - until docker info; do sleep 1; done
    # переменные CI_REGISTRY_USER, CI_REGISTRY_PASSWORD, CI_REGISTRY генерятся Гитлабом, их задавать не надо
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - cd frontend
    - >
      docker build
      --build-arg VERSION=$VERSION
      --tag $CI_REGISTRY_IMAGE/sausage-frontend:$CI_COMMIT_SHA
      .
    - docker push $CI_REGISTRY_IMAGE/sausage-frontend:$CI_COMMIT_SHA
  rules:
    - changes:
        - frontend/*


upload-frontend-latest:
  variables:
    GIT_STRATEGY: none
  image: docker:20.10.12-dind-rootless
  stage: release
  before_script:
    - until docker info; do sleep 1; done
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker pull $CI_REGISTRY_IMAGE/sausage-frontend:$CI_COMMIT_SHA
    # если образ прошел проверки в CI (сканирование, тесты и т.д), то тегаем latest
    - docker tag $CI_REGISTRY_IMAGE/sausage-frontend:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE/sausage-frontend:latest
    - docker push $CI_REGISTRY_IMAGE/sausage-frontend:latest

deploy-frontend:
   stage: deploy
   image: alpine:3.15.0

   before_script:
     - apk add openssh-client bash
     - apk add --no-cache gettext
     - eval $(ssh-agent -s)
     - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
     - mkdir -p ~/.ssh
     - chmod 700 ~/.ssh
     - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
     - chmod 644 ~/.ssh/known_hosts

   script:
     - envsubst < docker-compose.yml > docker-compose-replaced-env.yml
     - scp docker-compose-replaced-env.yml  ci-user@178.154.227.45:/home/ci-user/docker-compose.yml
     - ssh ci-user@178.154.227.45        
       "export "VERSION=${VERSION}";
       export "CI_REGISTRY_USER=$CI_REGISTRY_USER";
       export "CI_REGISTRY_PASSWORD=$CI_REGISTRY_PASSWORD";
       export "CI_REGISTRY=$CI_REGISTRY";   
       /bin/bash -s " < ./frontend/frontend_deploy.sh