forked from Liron-Refaeli/code2cloud-python-flask-webserver
-
Notifications
You must be signed in to change notification settings - Fork 10
88 lines (70 loc) · 2.98 KB
/
build.yml
File metadata and controls
88 lines (70 loc) · 2.98 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
name: CI/CD
on:
push:
branches:
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
deploy_to_production:
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Configure AWS credentials
run: |
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws configure set region ${{ vars.AWS_REGION }}
- name: Install AWS CLI & kubectl
run: |
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
pip install --upgrade awscli
aws --version
- name: Log in to AWS ECR
run: |
aws ecr get-login-password --region ${{ vars.AWS_REGION }} | docker login --username AWS --password-stdin ${{ vars.IMAGE_REGISTRY }}
- name: Update kubeconfig for EKS
run: |
aws eks update-kubeconfig --name eks-code2cloud-test --region ${{ vars.AWS_REGION }}
- name: Build Docker image
run: |
LATEST_SHA=$(git rev-parse HEAD)
echo "LATEST_SHA=$LATEST_SHA" >> $GITHUB_ENV
docker build -t ${{ vars.IMAGE_REGISTRY }}:$LATEST_SHA .
docker tag ${{ vars.IMAGE_REGISTRY }}:$LATEST_SHA ${{ vars.IMAGE_REGISTRY }}:latest
- name: Push Docker image to AWS ECR
run: |
docker push ${{ vars.IMAGE_REGISTRY }}:$LATEST_SHA
docker push ${{ vars.IMAGE_REGISTRY }}:latest
- name: Deploy to EKS
run: |
# Set deployment name
DEPLOYMENT_NAME=${{ vars.DEPLOYMENT_NAME }}
# Check if the deployment exists
DEPLOYMENT_EXISTS=$(kubectl get deployment $DEPLOYMENT_NAME -n default --ignore-not-found)
if [ -z "$DEPLOYMENT_EXISTS" ]; then
echo "Deployment does not exist. Creating deployment."
kubectl create deployment $DEPLOYMENT_NAME --image=${{ vars.IMAGE_REGISTRY }}:$LATEST_SHA -n default
else
echo "Deployment exists. Updating..."
# Automatically detect the container name from the deployment
CONTAINER_NAME=$(kubectl get deployment $DEPLOYMENT_NAME -n default -o jsonpath='{.spec.template.spec.containers[0].name}')
# Use the detected container name to update the image
echo "Container name detected: $CONTAINER_NAME"
kubectl set image deployment/$DEPLOYMENT_NAME $CONTAINER_NAME=${{ vars.IMAGE_REGISTRY }}:$LATEST_SHA -n default
fi