-
Notifications
You must be signed in to change notification settings - Fork 2.2k
99 lines (89 loc) · 4.28 KB
/
cd-deploy.yml
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
name: CD-Deploy
on:
push:
branches:
- 'develop'
# paths:
# - '06-best-practices/code/**'
jobs:
build-push-deploy:
runs-on: ubuntu-latest
steps:
- name: Check out repo
uses: actions/checkout@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: "eu-west-1"
- uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false
# Define the infrastructure
- name: TF plan
id: tf-plan
working-directory: '06-best-practices/code/infrastructure'
run: |
terraform init -backend-config="key=mlops-zoomcamp-prod.tfstate" -reconfigure && terraform plan -var-file=vars/prod.tfvars
- name: TF Apply
id: tf-apply
working-directory: '06-best-practices/code/infrastructure'
if: ${{ steps.tf-plan.outcome }} == 'success'
run: |
terraform apply -auto-approve -var-file=vars/prod.tfvars
echo "::set-output name=ecr_repo::$(terraform output ecr_repo | xargs)"
echo "::set-output name=predictions_stream_name::$(terraform output predictions_stream_name | xargs)"
echo "::set-output name=model_bucket::$(terraform output model_bucket | xargs)"
echo "::set-output name=lambda_function::$(terraform output lambda_function | xargs)"
# Build-Push
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push image to Amazon ECR
id: build-image-step
working-directory: "06-best-practices/code"
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ steps.tf-apply.outputs.ecr_repo }}
IMAGE_TAG: "latest" # ${{ github.sha }}
run: |
docker build -t ${ECR_REGISTRY}/${ECR_REPOSITORY}:${IMAGE_TAG} .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image_uri::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
# Deploy
- name: Get model artifacts
# The steps here are not suited for production.
# In practice, retrieving the latest model version or RUN_ID from a service like MLflow or DVC can also be integrated into a CI/CD pipeline.
# But due to the limited scope of this workshop, we would be keeping things simple.
# In practice, you would also have a separate training pipeline to write new model artifacts to your Model Bucket in Prod.
id: get-model-artifacts
working-directory: "06-best-practices/code"
env:
MODEL_BUCKET_DEV: "mlflow-models-alexey"
MODEL_BUCKET_PROD: ${{ steps.tf-apply.outputs.model_bucket }}
run: |
export RUN_ID=$(aws s3api list-objects-v2 --bucket ${MODEL_BUCKET_DEV} \
--query 'sort_by(Contents, &LastModified)[-1].Key' --output=text | cut -f2 -d/)
aws s3 sync s3://${MODEL_BUCKET_DEV} s3://${MODEL_BUCKET_PROD}
echo "::set-output name=run_id::${RUN_ID}"
- name: Update Lambda
env:
LAMBDA_FUNCTION: ${{ steps.tf-apply.outputs.lambda_function }}
PREDICTIONS_STREAM_NAME: ${{ steps.tf-apply.outputs.predictions_stream_name }}
MODEL_BUCKET: ${{ steps.tf-apply.outputs.model_bucket }}
RUN_ID: ${{ steps.get-model-artifacts.outputs.run_id }}
run: |
variables="{ \
PREDICTIONS_STREAM_NAME=$PREDICTIONS_STREAM_NAME, MODEL_BUCKET=$MODEL_BUCKET, RUN_ID=$RUN_ID \
}"
STATE=$(aws lambda get-function --function-name $LAMBDA_FUNCTION --region "eu-west-1" --query 'Configuration.LastUpdateStatus' --output text)
while [[ "$STATE" == "InProgress" ]]
do
echo "sleep 5sec ...."
sleep 5s
STATE=$(aws lambda get-function --function-name $LAMBDA_FUNCTION --region "eu-west-1" --query 'Configuration.LastUpdateStatus' --output text)
echo $STATE
done
aws lambda update-function-configuration --function-name $LAMBDA_FUNCTION \
--environment "Variables=${variables}"