|
1 | 1 | name: CI/CD |
2 | 2 |
|
3 | | -on: [push] |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main # or specify your branch here |
4 | 7 |
|
5 | 8 | jobs: |
6 | 9 | build-and-test: |
7 | 10 | runs-on: ubuntu-latest |
| 11 | + |
8 | 12 | 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 |
18 | 25 |
|
19 | 26 | deploy_to_production: |
20 | 27 | runs-on: ubuntu-latest |
21 | | - needs: [build-and-test] |
| 28 | + needs: build-and-test |
| 29 | + |
22 | 30 | steps: |
23 | | - - uses: actions/checkout@v2 |
| 31 | + - name: Checkout code |
| 32 | + uses: actions/checkout@v2 |
| 33 | + |
24 | 34 | - name: Set up AWS Credentials |
25 | 35 | run: | |
26 | 36 | echo "${{ secrets.AWS_ACCESS_KEY_ID }}" > aws_access_key_id |
27 | 37 | 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 |
32 | 43 | run: | |
33 | 44 | curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" |
34 | 45 | chmod +x ./kubectl |
35 | 46 | sudo mv ./kubectl /usr/local/bin/kubectl |
36 | 47 | pip install awscli --upgrade |
37 | 48 | 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: | |
40 | 52 | 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: | |
42 | 60 | 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: | |
45 | 66 | docker push ${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA |
46 | | - docker tag ${{ secrets.REPOSITORY }}:$LATEST_SHA ${{ secrets.IMAGE_REGISTRY }}:latest |
47 | 67 | 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: | |
49 | 72 | 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 |
51 | 79 | echo "Deployment does not exist. Creating deployment." |
52 | 80 | kubectl create deployment ${{ secrets.REPOSITORY }} --image=${{ secrets.IMAGE_REGISTRY }}:$LATEST_SHA -n default |
53 | 81 | else |
|
0 commit comments