Skip to content

Commit b7f03a2

Browse files
authored
Update build.yml
1 parent 21e3e38 commit b7f03a2

1 file changed

Lines changed: 27 additions & 36 deletions

File tree

.github/workflows/build.yml

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: CI/CD
33
on:
44
push:
55
branches:
6-
- main # or specify your branch here
6+
- main
77

88
jobs:
99
build-and-test:
@@ -16,75 +16,66 @@ jobs:
1616
- name: Set up Python
1717
uses: actions/setup-python@v2
1818
with:
19-
python-version: '3.8' # Change the Python version if needed
19+
python-version: '3.8'
2020

2121
- name: Install dependencies
2222
run: |
2323
python -m pip install --upgrade pip
2424
pip install -r requirements.txt
2525
26-
deploy_to_production:
26+
deploy-to-eks:
2727
runs-on: ubuntu-latest
2828
needs: build-and-test
2929

3030
steps:
3131
- name: Checkout code
3232
uses: actions/checkout@v2
3333

34-
- name: Set up AWS Credentials
34+
- name: Set AWS Credentials
3535
run: |
36-
echo "${{ secrets.AWS_ACCESS_KEY_ID }}" > aws_access_key_id
37-
echo "${{ secrets.AWS_SECRET_ACCESS_KEY }}" > aws_secret_access_key
3836
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
3937
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
40-
aws configure set region eu-north-1 # Change region if needed
38+
aws configure set region eu-north-1
4139
42-
- name: Install kubectl and AWS CLI
40+
- name: Install tools
4341
run: |
4442
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
45-
chmod +x ./kubectl
46-
sudo mv ./kubectl /usr/local/bin/kubectl
43+
chmod +x kubectl
44+
sudo mv kubectl /usr/local/bin/
4745
pip install awscli --upgrade
48-
aws --version
4946
50-
- name: Log in to AWS ECR
47+
- name: Log in to ECR
5148
run: |
52-
aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin ${{ secrets.IMAGE_REGISTRY }}
49+
aws ecr get-login-password --region eu-north-1 | \
50+
docker login --username AWS --password-stdin ${{ secrets.IMAGE_REGISTRY }}
5351
54-
- name: Update kubeconfig for EKS
52+
- name: Build and tag Docker image
5553
run: |
56-
aws eks update-kubeconfig --name eks-code2cloud-test --region eu-north-1 # Replace with your EKS cluster name
54+
LATEST_SHA=$(git rev-parse --short HEAD)
55+
echo "LATEST_SHA=$LATEST_SHA" >> $GITHUB_ENV
5756
58-
- name: Build Docker image
59-
run: |
60-
LATEST_SHA=$(git rev-parse HEAD)
61-
echo "LATEST_SHA: $LATEST_SHA" # Check the value of LATEST_SHA
62-
if [[ -z "$LATEST_SHA" ]]; then
63-
echo "Error: LATEST_SHA is empty, aborting."
64-
exit 1
65-
fi
6657
docker build -t ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA .
6758
docker tag ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:latest
6859
69-
- name: Push Docker image to AWS ECR
60+
- name: Push Docker image to ECR
7061
run: |
71-
echo "Pushing image to AWS ECR..."
7262
docker push ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA
7363
docker push ${{ secrets.IMAGE_REGISTRY }}:latest
7464
75-
- name: Check if the deployment exists
76-
id: deployment
65+
- name: Update kubeconfig
7766
run: |
78-
DEPLOYMENT_EXISTS=$(kubectl get deployment ${{ secrets.REPOSITORY }} -n default --ignore-not-found)
79-
echo "Deployment exists: $DEPLOYMENT_EXISTS"
80-
echo "::set-output name=exists::$DEPLOYMENT_EXISTS"
67+
aws eks update-kubeconfig --name eks-code2cloud-test --region eu-north-1
8168
82-
- name: Create or update deployment in EKS
69+
- name: Deploy to EKS
8370
run: |
84-
if [[ "${{ steps.deployment.outputs.exists }}" == "null" ]]; then
85-
echo "Deployment does not exist. Creating deployment."
86-
kubectl create deployment ${{ secrets.REPOSITORY }} --image=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA -n default
71+
DEPLOYMENT_EXISTS=$(kubectl get deployment ${{ secrets.REPOSITORY }} --namespace=default --ignore-not-found)
72+
73+
if [[ -z "$DEPLOYMENT_EXISTS" ]]; then
74+
echo "Creating new deployment"
75+
kubectl create deployment ${{ secrets.REPOSITORY }} \
76+
--image=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA --namespace=default
8777
else
88-
echo "Deployment exists. Updating deployment."
89-
kubectl set image deployment/${{ secrets.REPOSITORY }} ${{ secrets.REPOSITORY }}=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA -n default
78+
echo "Updating existing deployment"
79+
kubectl set image deployment/${{ secrets.REPOSITORY }} \
80+
${{ secrets.REPOSITORY }}=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA --namespace=default
9081
fi

0 commit comments

Comments
 (0)