CI

Gitlab


./gitlab-ci.yaml

Note: Change master to main if needed.

stages:
  - pre
  - test
  - development
  - staging
  - production

## Always build develop unless it contains hotfix and do not build unless tag matches the semver
.rules:template:
  rules:
    - if: '$CI_COMMIT_TAG !~ /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/'
      when: never
    - if: '$CI_COMMIT_TAG =~ /hotfix/ || $ENVIRONMENT == "staging" || $ENVIRONMENT == "production"'
      when: manual
    - if: '$CI_COMMIT_TAG =~ /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/'
      when: always

## Build Template using ENVIRONMENT
.build:template:
  artifacts:
    untracked: true
  image:
    name: node:16
  when: manual
  script:
    - if [ ! -d "node_modules" ]; then yarn install; fi
    - REACT_APP_VERSION=$CI_COMMIT_TAG yarn run build:$ENVIRONMENT
    - mv build/ $CI_COMMIT_TAG-$ENVIRONMENT/
  environment:
    name: $ENVIRONMENT
    url: $AWS_CLOUDFRONT_URL
  tags:
    - docker


## Deploy Template using ENVIRONMENT
.deploy:template:
  image: registry.gitlab.com/gitlab-org/cloud-deploy/aws-base:latest
  when: on_success
  script:
    - aws s3 sync "$CI_COMMIT_TAG-$ENVIRONMENT/" "$AWS_S3_BUCKET/live" --delete
    - aws s3 cp "$AWS_S3_BUCKET/live" "$AWS_S3_BUCKET/archive/$CI_COMMIT_TAG" --recursive
    - aws cloudfront create-invalidation --distribution-id $AWS_CLOUDFRONT_DID --paths "/*"
  environment:
    name: $ENVIRONMENT
    url: $AWS_CLOUDFRONT_URL
  tags:
    - docker

## Automatically creating MR
create:merge-request:
  image: registry.gitlab.com/gitlab-automation-toolkit/gitlab-auto-mr
  stage: pre
  except:
    - master
    - tags
  script:
    - gitlab_auto_mr -t master -c Draft -d .gitlab/merge_request_templates/merge_request.md -r -s
  tags:
    - docker

## Test that is run on every merge, branch etc
test:
  stage: test
  image: node:16
  script:
    - echo Test
  when: always
  except:
      - /^release\/*/
      - master
  tags:
    - docker


### BUILD AND DEPLOY PER stages and environments

build-development:
  stage: development
  variables:
    ENVIRONMENT: "develop"
  extends:
    - .build:template
    - .rules:template

deploy-development:
  stage: development
  variables:
    ENVIRONMENT: "develop"
  extends:
    - .deploy:template
    - .rules:template
  dependencies:
    - build-development
  needs: [build-development]


build-staging:
  stage: staging
  variables:
    ENVIRONMENT: "staging"
  extends:
    - .build:template
    - .rules:template

deploy-staging:
  stage: staging
  variables:
    ENVIRONMENT: "staging"
  extends:
    - .deploy:template
    - .rules:template
  dependencies:
    - build-staging
  needs: [build-staging]

build-production:
  stage: production
  variables:
    ENVIRONMENT: "production"
  extends:
    - .build:template
    - .rules:template

deploy-production:
  stage: production
  variables:
    ENVIRONMENT: "production"
  extends:
    - .deploy:template
    - .rules:template
  dependencies:
    - build-production
  needs: [build-production]

./.gitlab/merge_request_templates/marge_request.md

# Description

<!-- please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. -->

## Type

- [ ] Fix
- [ ] Improvement / Refactor
- [ ] New Feature

Closes <!-- Issue Number -->