Skip to content

Latest commit

 

History

History
169 lines (144 loc) · 3.1 KB

14.Angular.md

File metadata and controls

169 lines (144 loc) · 3.1 KB

Mise en pratique avec un projet Angular

Création dur projet

(Sur votre machine)

ng new my-super-app

Pour installer et générer le fichier de configuation pour Eslint

ng lint

On créé un nouveau dépot gitlab (Sans readme.md) et on rajoute le remote dans l'application.

Push an existing Git repository
cd existing_repo
git remote rename origin old-origin
git remote add origin git@gitlab.das.becode:becode/my-web-app.git
git push --set-upstream origin --all
git push --set-upstream origin --tags

Création du pipeline

Cette fois nous allons créer un pipeline en 4 étapes qui auront une ou plusieurs tâches.

  • dependencies : Installation des dépendances NPM
  • quality : Test unitaire et lint
  • build : Compilation de l'application et push sur docker
  • deploy : Deployement sur un serveur avec docker

stages:
  - dependencies
  - quality
  - build
  - deploy

Paramètre par défaut

default:
  interruptible: true
  image: trion/ng-cli-karma:17.1.3
  tags:
    - docker
  cache:
    key:
      files:
        - package.json
    paths:
      - node_modules

Installation des dépendances

install:
  stage: dependencies
  script:
    - npm ci --prefer-offline

lint

lint:
  stage: quality
  needs: ["install"]
  script:
    - ng lint

test

test:
  stage: quality
  needs: ["install"]
  script:
    - ng test --watch=false

Angular Build

ng_build:
  stage: build
  needs: ["test", "lint"]
  artifacts:
    paths:
      - $CI_PROJECT_DIR/dist
  script:
      -  ng build --base-href /app/

Docker build

docker_build:
  stage: build
  image: docker
  needs: ["ng_build"]
  services:
    - name: docker:dind
      alias: docker
  variables:
    DOCKER_BUILDKIT: "1"
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""
  script:
    - docker build  -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
                    -t $CI_REGISTRY_IMAGE/myapp:latest
                      .
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
    - docker push  $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
    - docker push  $CI_REGISTRY_IMAGE/myapp:latest
  environment:
    name: prod
  tags:
    - docker

deploy

Il faut créer un nouveau runner.

deploy_prod:
  needs: ["scan", "docker_build"]
  stage: deploy
  environment: prod
  variables:
    DOCKER_BUILDKIT: "1"
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
  before_script:
    - container_id=$(docker ps | grep $CI_REGISTRY_IMAGE | awk '{ print $1 }')
    - if [ -n "$container_id" ]; then docker stop $container_id; fi;
  script: 
    - echo "Deploy Starting"
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
    - docker run -d -p 80:80 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA
    - container_id=$(docker ps | grep $CI_REGISTRY_IMAGE | awk '{ print $1 }')
    - docker exec $container_id ls -la
  tags:
    - prod
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: manual