Skip to content
Snippets Groups Projects
.gitlab-ci.yml 4.83 KiB
Newer Older
  • Learn to ignore specific revisions
  • sivan's avatar
    sivan committed
    image: golang:1.16
    
    stages:
    
      - test
      - build
      - docker
    
    thijsheijden's avatar
    thijsheijden committed
      - integration
    
      - deploy
    
    sivan's avatar
    sivan committed
    
    
    lint:
      stage: test
      script:
        - make lint
    
    sivan's avatar
    sivan committed
    
    
    unit_tests:
      stage: test
      script:
        - make dep
        - make test
        - gocover-cobertura < coverage.txt > coverage.xml
      artifacts:
        reports:
          cobertura: coverage.xml
    
    sivan's avatar
    sivan committed
    
    
    coverage:
      stage: test
      script:
        - make coverage
      after_script:
        - mkdir $CI_COMMIT_BRANCH
        - cp cover.html $CI_COMMIT_BRANCH
        - mv $CI_COMMIT_BRANCH/cover.html $CI_COMMIT_BRANCH/index.html
    
    sivan's avatar
    sivan committed
    
    
        - if [[ $CI_COMMIT_BRANCH = "develop" || $CI_COMMIT_BRANCH = "main" ]]; then COVERAGE_PATH=""; else COVERAGE_PATH="features"; fi
    
        # install openssh client and add ssh keys
        - apt-get install openssh-client curl -y >/dev/null
        - mkdir ~/.ssh/
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
        - chmod 600 ~/.ssh/id_rsa
        - ssh-add ~/.ssh/id_rsa
        - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - ssh -fN -L 1234:science-vs260.science.uu.nl:22 sivan@up.science.uu.nl
        - scp -r -o StrictHostKeyChecking=no -P 1234 -i ~/.ssh/id_rsa $CI_COMMIT_BRANCH root@localhost:/datadisk/documentation-coverage/home/backend/$CI_PROJECT_NAME/$COVERAGE_PATH
      artifacts:
        untracked: false
        expire_in: 30 days
        paths:
          - cover.html
    
    build:
      stage: build
      script:
        - make linux
      artifacts:
        paths:
          - builds/
      dependencies: []
    
    docker:
      image: docker:stable
      tags:
        - docker
      stage: docker
      only:
        - develop
      before_script:
        - docker login datastropheregistry.azurecr.io -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD
    
      script:
        - docker build --progress plain -t $CI_PROJECT_NAME:latest .
        - docker tag $CI_PROJECT_NAME datastropheregistry.azurecr.io/$CI_PROJECT_NAME:latest
        - docker push datastropheregistry.azurecr.io/$CI_PROJECT_NAME:latest
      dependencies:
        - build
    
    docker-staging:
      image: docker:stable
      tags:
        - docker
      stage: docker
      except:
        - develop
        - main
      before_script:
        - docker login datastropheregistry.azurecr.io -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD
    
      script:
        - docker build --progress plain -t $CI_PROJECT_NAME:latest .
        - docker tag $CI_PROJECT_NAME datastropheregistry.azurecr.io/$CI_PROJECT_NAME-staging:latest
        - docker push datastropheregistry.azurecr.io/$CI_PROJECT_NAME-staging:latest
      dependencies:
        - build
    
    thijsheijden's avatar
    thijsheijden committed
    integration-tests:
      image: golang:1.16
      stage: integration
      script:
    
        - apt-get install openssh-client curl grep -y >/dev/null
    
    thijsheijden's avatar
    thijsheijden committed
        - mkdir ~/.ssh/
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
        - chmod 600 ~/.ssh/id_rsa
        - ssh-add ~/.ssh/id_rsa
        - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - ssh -fN -L 1234:science-vs260.science.uu.nl:22 sivan@up.science.uu.nl
    
    thijsheijden's avatar
    thijsheijden committed
        # Delete pod
    
        - ssh -p 1234 -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa root@localhost "kubectl delete -f /root/kubernetes/$CI_PROJECT_NAME/integration/pod.yml --ignore-not-found=true"
    
    thijsheijden's avatar
    thijsheijden committed
        # Copy kubernetes files over
    
    thijsheijden's avatar
    thijsheijden committed
        - scp -r -o StrictHostKeyChecking=no -P 1234 -i ~/.ssh/id_rsa integration-testing/pod.yml root@localhost:/root/kubernetes/$CI_PROJECT_NAME/integration/pod.yml
    
    thijsheijden's avatar
    thijsheijden committed
        # Deploy all yml files
    
    thijsheijden's avatar
    thijsheijden committed
        - ssh -p 1234 -i ~/.ssh/id_rsa root@localhost "kubectl apply -f /root/kubernetes/$CI_PROJECT_NAME/integration/pod.yml"
    
    thijsheijden's avatar
    thijsheijden committed
        # Wait 5 seconds for the pod to start up
        - sleep 5
        # CURL the config json to the integration service and store the response in the environment variable 'output'
        - |
    
          output=$(curl -k --header "Content-Type: multipart/form-data" --request POST -F "configFile=@integration-testing/config.json;type=application/json" https://datastrophe.science.uu.nl/integration-test/start/)
    
    thijsheijden's avatar
    thijsheijden committed
        - echo $output
        # Count the number of occurences of the word FAIL in the output, if this is 1 then the test has failed
    
        - echo "$output" | grep -c "SUCCESS"
    
    thijsheijden's avatar
    thijsheijden committed
      dependencies: []
    
    thijsheijden's avatar
    thijsheijden committed
    
    
    deploy:
      stage: deploy
      only:
        - develop
      script:
        - apt-get install openssh-client curl -y >/dev/null
        - mkdir ~/.ssh/
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
        - chmod 600 ~/.ssh/id_rsa
        - ssh-add ~/.ssh/id_rsa
        - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts
        - ssh -fN -L 1234:science-vs260.science.uu.nl:22 sivan@up.science.uu.nl
        # Copy kubernetes files over
    
        - scp -r -o StrictHostKeyChecking=no -P 1234 -i ~/.ssh/id_rsa deployments/* root@localhost:/root/kubernetes/$CI_PROJECT_NAME
    
        # Deploy all yml files
    
        - ssh -p 1234 -i ~/.ssh/id_rsa root@localhost "for i in kubernetes/$CI_PROJECT_NAME/*.yml; do kubectl apply -f \$i; done"
    
        # Perform rolling update for deployment
    
        - ssh -p 1234 -i ~/.ssh/id_rsa root@localhost "kubectl rollout restart -f kubernetes/$CI_PROJECT_NAME/deployment.yml"
    
      dependencies: []