-
Notifications
You must be signed in to change notification settings - Fork 3
131 lines (117 loc) · 4.61 KB
/
train_deploy.yaml
File metadata and controls
131 lines (117 loc) · 4.61 KB
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
127
128
129
130
131
name: Train and Deploy ML Model
on:
push:
branches:
- main
- citest
jobs:
build-deploy:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout repository
- name: Checkout repository
uses: actions/checkout@v2
# Step 2: Set up Google Cloud SDK
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v0.2.0
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}
export_default_credentials: true
# Step 3: Build Docker Image
- name: Build Docker Image
run: |
IMAGE_NAME="gcr.io/${{ secrets.GCP_PROJECT_ID }}/mpg"
MODEL_VERSION="v2"
echo "Building Docker image..."
# docker build ./ -t ${IMAGE_NAME}:${MODEL_VERSION} -f GCP/gcpdeploy/trainer/Dockerfile
# echo "Pushing Docker image..."
# docker push ${IMAGE_NAME}:${MODEL_VERSION}
echo "Tagging image as 'latest'..."
# docker tag ${IMAGE_NAME}:${MODEL_VERSION} ${IMAGE_NAME}:latest
# docker push ${IMAGE_NAME}:latest
# Step 4: Retrieve Image Digest
- name: Retrieve Image Digest
id: digest
run: |
IMAGE_NAME="gcr.io/${{ secrets.GCP_PROJECT_ID }}/mpg"
MODEL_VERSION="v2"
DIGEST=$(gcloud container images describe ${IMAGE_NAME}:${MODEL_VERSION} --format="value(image_summary.fully_qualified_digest)")
if [ -z "$DIGEST" ]; then
echo "Error: Failed to retrieve image digest. Ensure the image was pushed successfully."
exit 1
fi
echo "Digest: $DIGEST"
echo "::set-output name=digest::$DIGEST"
# Step 5: Upload Model to Vertex AI
- name: Upload or Retrieve Model
id: model-upload
run: |
IMAGE_NAME="gcr.io/${{ secrets.GCP_PROJECT_ID }}/mpg"
MODEL_VERSION="v2"
DIGEST=${{ steps.digest.outputs.digest }}
# Try uploading the model
MODEL_ID=$(gcloud ai models upload \
--region=us-east1 \
--display-name=model-${MODEL_VERSION} \
--artifact-uri=gs://stock_price_prediction_dataset/model_checkpoints/ \
--container-image-uri=${DIGEST} \
--format="value(name)")
# If upload fails, try retrieving the existing model
if [ -z "$MODEL_ID" ]; then
echo "Upload failed, trying to retrieve existing model..."
MODEL_ID=$(gcloud ai models list \
--region=us-east1 \
--filter="display_name:model-${MODEL_VERSION}" \
--format="value(name)" | head -n 1)
fi
if [ -z "$MODEL_ID" ]; then
echo "Error: Failed to upload or retrieve the model."
exit 1
fi
echo "Model ID: $MODEL_ID"
echo "::set-output name=model_id::$MODEL_ID"
# Step 6: Create or Retrieve Endpoint
- name: Create or Retrieve Endpoint
id: endpoint
run: |
ENDPOINT_NAME="model-endpoint-v2"
ENDPOINT_ID=$(gcloud ai endpoints list \
--region=us-east1 \
--filter="display_name:${ENDPOINT_NAME}" \
--format="value(name)")
if [ -z "$ENDPOINT_ID" ]; then
echo "Creating new endpoint: ${ENDPOINT_NAME}"
ENDPOINT_ID=$(gcloud ai endpoints create \
--region=us-east1 \
--display-name=${ENDPOINT_NAME} \
--format="value(name)")
if [ -z "$ENDPOINT_ID" ]; then
echo "Error: Failed to create endpoint."
exit 1
fi
else
echo "Using existing endpoint: ${ENDPOINT_NAME}"
fi
echo "Endpoint ID: $ENDPOINT_ID"
echo "::set-output name=endpoint_id::$ENDPOINT_ID"
# Step 7: Deploy Model to Endpoint
- name: Deploy Model
run: |
ENDPOINT_ID=${{ steps.endpoint.outputs.endpoint_id }}
MODEL_ID=${{ steps.model-upload.outputs.model_id }}
# gcloud ai endpoints deploy-model \
# $ENDPOINT_ID \
# --region=us-east1 \
# --model=$MODEL_ID \
# --display-name=model-v2 \
# --machine-type=n1-highmem-4 \
# --traffic-split="0=100"
# if [ $? -ne 0 ]; then
# echo "Error: Model deployment failed."
# exit 1
# fi
echo "Model deployed successfully."
# Step 8: Notify Completion
- name: Notify Completion
run: echo "Model deployment process completed successfully."