File size: 4,365 Bytes
f32a529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
version: 2.1

parameters:
  # This parameter is used to trigger the main workflow
  image_name:
    type: string
    default: text_to_speech_open_api
  package_version:
    type: string
    default: "2.1.31"
  dependency_image_name:
    type: string
    default: text_to_speech_open_api_dependency
  dependency_version:
    type: string
    default: "2.1.7"
  build_dependency:
    type: boolean
    default: true

executors:
  node:
    docker:
      - image: cimg/python:3.8.6
jobs:
  sonar-scan:
    machine:
      image: ubuntu-2004:202201-02
    working_directory: ~/text-to-speech-open-api/
    steps:
      - checkout:
          path: ~/text-to-speech-open-api
      - run:
          name: Branch
          command: echo $CIRCLE_BRANCH
      - run:
          name: Sonar Scan
          command: docker run --rm -e SONAR_HOST_URL="https://sonarcloud.io" -e SONAR_TOKEN=$SONAR_TOKEN -v $PWD:/usr/src sonarsource/sonar-scanner-cli -Dsonar.branch.name=$CIRCLE_BRANCH

  test-suite:
    executor: node
    working_directory: ~/text-to-speech-open-api/
    steps:
      - checkout:
          path: ~/text-to-speech-open-api/
      - run:
          name: Setup Testing Environment
          command: |
            python --version ;pip --version ; pwd ; ls
            pip install --upgrade pip
            pip install pytest grpcio grpcio-tools pytest-grpc coverage pydantic
            python --version ; pip --version ; pwd ; ls
      - run:
          name: Run Tests
          command: |
            python --version ; pip --version ; pwd ; ls
            pwd ; coverage run -m unittest discover tests
            pytest
            coverage report --fail-under 20  src/*/*.py
            coverage html  # open htmlcov/index.html in a browser
      - store_artifacts:
          path: htmlcov


  build_and_deploy_api_image:
    machine:
      image: ubuntu-2004:202101-01
      resource_class: large
    working_directory: ~/text-to-speech-open-api/
    steps:
      - checkout:
          path: ~/text-to-speech-open-api
      - run:
          name: Setup Google credentials
          command: |
            echo ${GOOGLE_AUTH} > ${HOME}/gcp-key.json
            pip install google_compute_engine
            gcloud auth activate-service-account --key-file ${HOME}/gcp-key.json
            gcloud --quiet config set project ${GCP_PROJECT}
      - run:
          name: Build dependencies image
          command: |
            echo "Build dependencies..."
            if << pipeline.parameters.build_dependency >> ; then
              docker build --rm=false -t gcr.io/${GCP_PROJECT}/<< pipeline.parameters.dependency_image_name >>:<< pipeline.parameters.dependency_version >> dependencies/
              gcloud docker -- push gcr.io/${GCP_PROJECT}/<< pipeline.parameters.dependency_image_name >>:<< pipeline.parameters.dependency_version >>
            fi
#      - when:
#          condition:
#            equal: [ true, << pipeline.paramaters.build_dependency >> ]
#          steps:
#            - run: |
#                docker build --rm=false -t gcr.io/${GCP_PROJECT}/<< pipeline.parameters.dependency_image_name >>:<< pipeline.parameters.dependency_version >> dependencies/
#                gcloud docker -- push gcr.io/${GCP_PROJECT}/<< pipeline.parameters.dependency_image_name >>:<< pipeline.parameters.dependency_version >>
      - run:
          name: Build docker image
          command: |
            echo "Build api image..."
            docker build --rm=false -t gcr.io/${GCP_PROJECT}/<< pipeline.parameters.image_name >>:<< pipeline.parameters.package_version >> .
      - run:
          name: deploy image to registry
          command: |
            echo "Deploy api image to registry..."
            gcloud docker -- push gcr.io/${GCP_PROJECT}/<< pipeline.parameters.image_name >>:<< pipeline.parameters.package_version >>
      - run:
          name: Remove account details
          command: |
            rm ${HOME}/gcp-key.json ; ls
workflows:
  version: 2
  main_workflow:
    jobs:
    - sonar-scan:
        name: sonar-scan
    - test-suite:
        name: test-suite
    - approve_to_publish:
         name: approve-build-and-deploy
         type: approval
         requires:
           - test-suite
    - build_and_deploy_api_image:
         name: build_and_deploy_api_image
         requires:
            - approve-build-and-deploy