|
| 1 | +name: Test & Deploy Evaluation Function to AWS Lambda |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - main |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +jobs: |
| 11 | + test: |
| 12 | + name: Test |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + strategy: |
| 16 | + fail-fast: false |
| 17 | + matrix: |
| 18 | + python-version: [3.8] |
| 19 | + |
| 20 | + defaults: |
| 21 | + run: |
| 22 | + working-directory: app/ |
| 23 | + |
| 24 | + env: |
| 25 | + REQUEST_SCHEMA_URL: https://raw.githubusercontent.com/lambda-feedback/request-response-schemas/master/request.json |
| 26 | + RESPONSE_SCHEMA_URL: https://raw.githubusercontent.com/lambda-feedback/request-response-schemas/master/responsev2.json |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Checkout |
| 30 | + uses: actions/checkout@v2 |
| 31 | + |
| 32 | + - name: Set up Python ${{ matrix.python-version }} |
| 33 | + uses: actions/setup-python@v2 |
| 34 | + with: |
| 35 | + python-version: ${{ matrix.python-version }} |
| 36 | + |
| 37 | + - name: Install dependencies |
| 38 | + run: | |
| 39 | + python -m pip install --upgrade pip |
| 40 | + python -m pip install flake8 pytest |
| 41 | + python -m pip install -r requirements.txt |
| 42 | +
|
| 43 | + - name: Lint with flake8 |
| 44 | + run: | |
| 45 | + # stop the build if there are Python syntax errors or undefined names |
| 46 | + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics |
| 47 | + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide |
| 48 | + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics |
| 49 | +
|
| 50 | + - name: Test Evaluation Function |
| 51 | + run: | |
| 52 | + pytest -v evaluation_tests.py::TestEvaluationFunction |
| 53 | +
|
| 54 | + - name: Test Preview Function |
| 55 | + run: | |
| 56 | + pytest -v preview_tests.py::TestPreviewFunction |
| 57 | +
|
| 58 | + deploy-staging: |
| 59 | + name: Deploy Staging |
| 60 | + needs: test |
| 61 | + runs-on: ubuntu-latest |
| 62 | + environment: production |
| 63 | + env: |
| 64 | + ECR_REPOSITORY: lambda-feedback-staging-functions-repository |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: Checkout |
| 68 | + uses: actions/checkout@v2 |
| 69 | + |
| 70 | + - name: Set config.json output |
| 71 | + id: set_config_var |
| 72 | + run: | |
| 73 | + content=`cat ./config.json` |
| 74 | + # the following lines are only required for multi line json |
| 75 | + content="${content//'%'/'%25'}" |
| 76 | + content="${content//$'\n'/'%0A'}" |
| 77 | + content="${content//$'\r'/'%0D'}" |
| 78 | + # end of optional handling for multi line json |
| 79 | + echo "::set-output name=configJson::$content" |
| 80 | +
|
| 81 | + - name: set Evaluation Function Name |
| 82 | + id: set_function_name |
| 83 | + run: | |
| 84 | + functionName="${{fromJson(steps.set_config_var.outputs.configJson).EvaluationFunctionName}}" |
| 85 | + [[ -z "$functionName" ]] && { echo "Add EvaluationFunctionName to config.json" ; exit 1; } |
| 86 | + echo "::set-output name=function_name::$functionName" |
| 87 | +
|
| 88 | + - name: Configure AWS credentials |
| 89 | + uses: aws-actions/configure-aws-credentials@v1 |
| 90 | + with: |
| 91 | + aws-access-key-id: ${{ secrets.LAMBDA_CONTAINER_PIPELINE_AWS_ID }} |
| 92 | + aws-secret-access-key: ${{ secrets.LAMBDA_CONTAINER_PIPELINE_AWS_SECRET }} |
| 93 | + aws-region: eu-west-2 |
| 94 | + |
| 95 | + - name: Login to Amazon ECR |
| 96 | + id: login-ecr |
| 97 | + uses: aws-actions/amazon-ecr-login@v1 |
| 98 | + |
| 99 | + - name: Build, tag, and push image to Amazon ECR |
| 100 | + id: build-image |
| 101 | + env: |
| 102 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 103 | + IMAGE_TAG: ${{ steps.set_function_name.outputs.function_name }} |
| 104 | + run: | |
| 105 | + # Build docker image from algorithm, schema and requirements |
| 106 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG app/ |
| 107 | + # Push image to ECR |
| 108 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 109 | + echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" |
| 110 | +
|
| 111 | + - name: deploy evaluation function |
| 112 | + id: deploy-evaluation-function |
| 113 | + env: |
| 114 | + BACKEND_API_URL: https://staging-api.lambdafeedback.com |
| 115 | + API_KEY: ${{ secrets.FUNCTION_ADMIN_API_KEY }} |
| 116 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 117 | + IMAGE_TAG: ${{ steps.set_function_name.outputs.function_name }} |
| 118 | + run: | |
| 119 | + curl --location --request POST "$BACKEND_API_URL/grading-function/ensure" \ |
| 120 | + --header 'content-type: application/json' \ |
| 121 | + --data-raw "{ |
| 122 | + \"apiKey\": \"$API_KEY\", |
| 123 | + \"dockerImageUri\": \"$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG\", |
| 124 | + \"functionName\": \"$IMAGE_TAG\" |
| 125 | + }" |
| 126 | +
|
| 127 | + deploy-production: |
| 128 | + name: Deploy Production |
| 129 | + needs: deploy-staging |
| 130 | + runs-on: ubuntu-latest |
| 131 | + environment: production |
| 132 | + env: |
| 133 | + ECR_REPOSITORY: lambda-feedback-production-functions-repository |
| 134 | + |
| 135 | + steps: |
| 136 | + - name: Checkout |
| 137 | + uses: actions/checkout@v2 |
| 138 | + |
| 139 | + - name: Set config.json output |
| 140 | + id: set_config_var |
| 141 | + run: | |
| 142 | + content=`cat ./config.json` |
| 143 | + # the following lines are only required for multi line json |
| 144 | + content="${content//'%'/'%25'}" |
| 145 | + content="${content//$'\n'/'%0A'}" |
| 146 | + content="${content//$'\r'/'%0D'}" |
| 147 | + # end of optional handling for multi line json |
| 148 | + echo "::set-output name=configJson::$content" |
| 149 | +
|
| 150 | + - name: set Evaluation Function Name |
| 151 | + id: set_function_name |
| 152 | + run: | |
| 153 | + functionName="${{fromJson(steps.set_config_var.outputs.configJson).EvaluationFunctionName}}" |
| 154 | + [[ -z "$functionName" ]] && { echo "Add EvaluationFunctionName to config.json" ; exit 1; } |
| 155 | + echo "::set-output name=function_name::$functionName" |
| 156 | +
|
| 157 | + - name: Configure AWS credentials |
| 158 | + uses: aws-actions/configure-aws-credentials@v1 |
| 159 | + with: |
| 160 | + aws-access-key-id: ${{ secrets.LAMBDA_CONTAINER_PIPELINE_AWS_ID }} |
| 161 | + aws-secret-access-key: ${{ secrets.LAMBDA_CONTAINER_PIPELINE_AWS_SECRET }} |
| 162 | + aws-region: eu-west-2 |
| 163 | + |
| 164 | + - name: Login to Amazon ECR |
| 165 | + id: login-ecr |
| 166 | + uses: aws-actions/amazon-ecr-login@v1 |
| 167 | + |
| 168 | + - name: Build, tag, and push image to Amazon ECR |
| 169 | + id: build-image |
| 170 | + env: |
| 171 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 172 | + IMAGE_TAG: ${{ steps.set_function_name.outputs.function_name }} |
| 173 | + run: | |
| 174 | + # Build docker image from algorithm, schema and requirements |
| 175 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG app/ |
| 176 | + # Push image to ECR |
| 177 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG |
| 178 | + echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" |
| 179 | +
|
| 180 | + - name: deploy evaluation function |
| 181 | + id: deploy-evaluation-function |
| 182 | + env: |
| 183 | + BACKEND_API_URL: https://prod-api.lambdafeedback.com |
| 184 | + API_KEY: ${{ secrets.FUNCTION_ADMIN_API_KEY }} |
| 185 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 186 | + IMAGE_TAG: ${{ steps.set_function_name.outputs.function_name }} |
| 187 | + run: | |
| 188 | + curl --location --request POST "$BACKEND_API_URL/grading-function/ensure" \ |
| 189 | + --header 'content-type: application/json' \ |
| 190 | + --data-raw "{ |
| 191 | + \"apiKey\": \"$API_KEY\", |
| 192 | + \"dockerImageUri\": \"$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG\", |
| 193 | + \"functionName\": \"$IMAGE_TAG\" |
| 194 | + }" |
0 commit comments