Skip to content
Snippets Groups Projects
.gitlab-ci.yml 4.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • image: python:3.10-buster
    
    David Trattnig's avatar
    David Trattnig committed
    
    stages:
    
    Lars Kruse's avatar
    Lars Kruse committed
      - test
    
    David Trattnig's avatar
    David Trattnig committed
      - deploy
    
    David Trattnig's avatar
    David Trattnig committed
    
    variables:
    
      API_ROOT_FILE: ./src/aura_engine_api/rest/swagger/swagger.yaml
    
    David Trattnig's avatar
    David Trattnig committed
      BUNDLED_JSON_FILE: ./public/api.json
    
    
    David Trattnig's avatar
    David Trattnig committed
    .install_requirements_python: &install_requirements_python
      - apt-get --quiet update
    
    David Trattnig's avatar
    David Trattnig committed
      - apt-get -y install make
    
    David Trattnig's avatar
    David Trattnig committed
      - python3 -m pip install --upgrade pip
      - pip install poetry
      - poetry install --no-interaction
    
    
    .install_requirements_node: &install_requirements_node
      - apt-get --quiet update
      - apt-get --quiet --yes install nodejs npm
      - nodejs -v
      - npm i -g npm@latest
    
    
    .release-rules: &release-rules
      # rule to run job on a tag-reference which has the form number.number.number (semantic versioning)
      # or number.number.number-text (semantic versioning + release-name)
      # and where a Dockerfile exists
      - if: $CI_COMMIT_TAG =~ /^\d+[.]\d+[.]\d+$/ || $CI_COMMIT_TAG =~ /^\d+[.]\d+[.]\d+[-]\S+$/
        exists:
          - Dockerfile
    
    
    David Trattnig's avatar
    David Trattnig committed
    cache:
      paths:
        - ./node_modules
    
    
    Lars Kruse's avatar
    Lars Kruse committed
    check_style:
      stage: test
      before_script:
    
    David Trattnig's avatar
    David Trattnig committed
        - *install_requirements_python
    
    Lars Kruse's avatar
    Lars Kruse committed
      script:
        - make lint
    
    David Trattnig's avatar
    David Trattnig committed
        - make spell
    
    David Trattnig's avatar
    David Trattnig committed
    test_api:
      stage: test
      before_script:
        - *install_requirements_python
      script:
        - make test
    
    bundle_and_test:
      stage: bundle
      before_script:
    
        - *install_requirements_node
    
        - npm install -g @apidevtools/swagger-cli
      script:
        - mkdir public
        - swagger-cli bundle -t json $API_ROOT_FILE > $BUNDLED_JSON_FILE
        - swagger-cli validate $BUNDLED_JSON_FILE
      artifacts:
        paths:
          - public
        expire_in: 2 days
      rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
          when: always
    
    deploy_spec:
    
    David Trattnig's avatar
    David Trattnig committed
      stage: deploy
      before_script:
    
        - *install_requirements_node
    
    David Trattnig's avatar
    David Trattnig committed
        - apt-get install -y -qq lftp
    
    David Trattnig's avatar
    David Trattnig committed
        - npm install swagger-ui-dist@3.22.1
      script:
        - cp -rp node_modules/swagger-ui-dist/* ./public
        - 'sed -i "s@.*url.*@url: \"api.json\",@" ./public/index.html'
    
        - echo "Deploying AURA Engine API Spec..."
        - lftp -c "set ftp:ssl-allow no; open -u $FTP_AURA_RADIO_USER,$FTP_AURA_RADIO_PWD $FTP_AURA_RADIO_HOST; mirror -Rnev ./public/ ./api.aura.radio/engine --ignore-time --parallel=10 --exclude-glob .git* --exclude .git/"
        - echo "AURA Engine API Spec successfully deployed."
    
    David Trattnig's avatar
    David Trattnig committed
      artifacts:
        paths:
          - public
        expire_in: 2 days
    
      rules:
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
          when: always
    
    
    docker-push:
      # Use the official docker image.
      image: docker:latest
      stage: release
      variables:
        # the name of the image without version
        AURA_IMAGE_NAME: "autoradio/engine-api"
      services:
        - docker:dind
      before_script:
    
        # on a feature branch will login to gitlab registry
        # else to docker hub
        # hint: feature branches must begin with "feat"
        - |
          if expr "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" : ^feat > /dev/null
            then docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
          else docker login -u "$DOCKER_ID" -p "$DOCKER_HUB_AUTH"
          fi
    
        # every commit on main branch should build and push image as unstable
        # elseif its a feature branch build and push to gitlab registry
    
        # else it is from a tag (enforced by gitlab-ci rules)
        # hint: tags are references independent of branches
    
        # hint: feature branches must begin with "feat"
    
        - |
          if [ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]
            then docker build -t $AURA_IMAGE_NAME:unstable .
    
            docker push $AURA_IMAGE_NAME:unstable 
    
          elif expr "$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" : ^feat > /dev/null
            then docker build -t $AURA_IMAGE_NAME -t $CI_REGISTRY_IMAGE:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME .
            docker push $CI_REGISTRY_IMAGE:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
          else docker build -t $AURA_IMAGE_NAME -t $AURA_IMAGE_NAME:$CI_COMMIT_TAG .
    
          docker push $AURA_IMAGE_NAME:$CI_COMMIT_TAG
    
        # every commit on master/main or feature branch should trigger a push
        - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^feat/
    
        - docker-push
      image: registry.gitlab.com/gitlab-org/release-cli:latest
      rules: *release-rules
      script:
        - echo "this will be a release when there is a tag, but tags should be protected to be only createable by maintainers."
      release:
        name: 'Release $CI_COMMIT_TAG'
    
        description: ./CHANGELOG.md
    
        tag_name: '$CI_COMMIT_TAG'
        ref: '$CI_COMMIT_TAG'