Skip to content

Commit af611a8

Browse files
authored
Merge pull request #2 from korengalpalo/korengalpalo-patch-1
Update build.yml
2 parents 5272bfa + b3e755f commit af611a8

1 file changed

Lines changed: 52 additions & 24 deletions

File tree

.github/workflows/build.yml

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,81 @@
11
name: CI/CD
22

3-
on: [push]
3+
on:
4+
push:
5+
branches:
6+
- main # or specify your branch here
47

58
jobs:
69
build-and-test:
710
runs-on: ubuntu-latest
11+
812
steps:
9-
- uses: actions/checkout@v2
10-
- name: Set up Python
11-
uses: actions/setup-python@v2
12-
with:
13-
python-version: '3.8' # You can use the version you need
14-
- name: Install Dependencies
15-
run: |
16-
python -m pip install --upgrade pip
17-
pip install -r requirements.txt
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: '3.8' # Change the Python version if needed
20+
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install -r requirements.txt
1825
1926
deploy_to_production:
2027
runs-on: ubuntu-latest
21-
needs: [build-and-test]
28+
needs: build-and-test
29+
2230
steps:
23-
- uses: actions/checkout@v2
31+
- name: Checkout code
32+
uses: actions/checkout@v2
33+
2434
- name: Set up AWS Credentials
2535
run: |
2636
echo "${{ secrets.AWS_ACCESS_KEY_ID }}" > aws_access_key_id
2737
echo "${{ secrets.AWS_SECRET_ACCESS_KEY }}" > aws_secret_access_key
28-
- name: Set AWS Region
29-
run: |
30-
echo "AWS_REGION=eu-north-1" >> $GITHUB_ENV # Update region to eu-north-1
31-
- name: Deploy to EKS
38+
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
39+
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
40+
aws configure set region eu-north-1 # Change region if needed
41+
42+
- name: Install kubectl and AWS CLI
3243
run: |
3344
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
3445
chmod +x ./kubectl
3546
sudo mv ./kubectl /usr/local/bin/kubectl
3647
pip install awscli --upgrade
3748
aws --version
38-
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}
39-
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }}
49+
50+
- name: Log in to AWS ECR
51+
run: |
4052
aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin ${{ secrets.IMAGE_REGISTRY }}
41-
aws eks update-kubeconfig --name eks-code2cloud-test --region ${{ env.AWS_REGION }} # Update cluster name
53+
54+
- name: Update kubeconfig for EKS
55+
run: |
56+
aws eks update-kubeconfig --name eks-code2cloud-test --region eu-north-1 # Replace with your EKS cluster name
57+
58+
- name: Build Docker image
59+
run: |
4260
LATEST_SHA=$(git rev-parse HEAD)
43-
docker build -t ${{ secrets.REPOSITORY }}:$LATEST_SHA .
44-
docker tag ${{ secrets.REPOSITORY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA
61+
docker build -t ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA .
62+
docker tag ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:latest
63+
64+
- name: Push Docker image to AWS ECR
65+
run: |
4566
docker push ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA
46-
docker tag ${{ secrets.REPOSITORY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:latest
4767
docker push ${{ secrets.IMAGE_REGISTRY }}:latest
48-
# Check if the deployment exists
68+
69+
- name: Check if the deployment exists
70+
id: deployment
71+
run: |
4972
DEPLOYMENT_EXISTS=$(kubectl get deployment ${{ secrets.REPOSITORY }} -n default --ignore-not-found)
50-
if [ -z "$DEPLOYMENT_EXISTS" ]; then
73+
echo "Deployment exists: $DEPLOYMENT_EXISTS"
74+
echo "::set-output name=exists::$DEPLOYMENT_EXISTS"
75+
76+
- name: Create or update deployment in EKS
77+
run: |
78+
if [[ "${{ steps.deployment.outputs.exists }}" == "null" ]]; then
5179
echo "Deployment does not exist. Creating deployment."
5280
kubectl create deployment ${{ secrets.REPOSITORY }} --image=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA -n default
5381
else

0 commit comments

Comments
 (0)