1+ ---
2+
3+ name : DevOps Project Pipeline
4+
5+ on :
6+ push :
7+ branches : [ main, develop ]
8+ pull_request :
9+ branches : [ main ]
10+
11+ env :
12+ DOCKER_IMAGE : thedevopsdue/devops-project
13+ REGISTRY : docker.io
14+
15+ jobs :
16+ test :
17+ runs-on : ubuntu-latest
18+
19+ steps :
20+ - name : Checkout code
21+ uses : actions/checkout@v4
22+
23+ - name : Set up Python
24+ uses : actions/setup-python@v4
25+ with :
26+ python-version : ' 3.9'
27+
28+ - name : Install dependencies
29+ run : |
30+ python -m pip install --upgrade pip
31+ pip install flask pytest
32+
33+ - name : Test Flask app
34+ run : |
35+ python -c "
36+ import sys
37+ sys.path.append('.')
38+ from app import app
39+ with app.test_client() as client:
40+ response = client.get('/')
41+ assert response.status_code == 200
42+ print('✅ Flask app test passed')
43+ "
44+
45+ build_and_push :
46+ needs : test
47+ runs-on : ubuntu-latest
48+ if : github.ref == 'refs/heads/main'
49+
50+ steps :
51+ - name : Checkout code
52+ uses : actions/checkout@v4
53+
54+ - name : Set Short SHA
55+ run : |
56+ echo "SHORT_SHA=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_ENV
57+
58+ - name : Set up Docker Buildx
59+ uses : docker/setup-buildx-action@v3
60+
61+ - name : Build Docker Image
62+ run : |
63+ docker build -t ${{ env.DOCKER_IMAGE }}:${{ env.SHORT_SHA }} .
64+ docker build -t ${{ env.DOCKER_IMAGE }}:latest .
65+
66+ - name : Test Docker Image
67+ run : |
68+ docker run -d -p 8080:8080 --name test-container ${{ env.DOCKER_IMAGE }}:${{ env.SHORT_SHA }}
69+ sleep 5
70+ curl -f http://localhost:8080 || exit 1
71+ docker stop test-container
72+ docker rm test-container
73+ echo "✅ Docker image test passed"
74+
75+ # Uncomment when you want to push to Docker Hub
76+ # - name: Login to Docker Hub
77+ # uses: docker/login-action@v3
78+ # with:
79+ # username: ${{ secrets.DOCKER_USERNAME }}
80+ # password: ${{ secrets.DOCKER_PASSWORD }}
81+ #
82+ # - name: Push Docker Image
83+ # run: |
84+ # docker push ${{ env.DOCKER_IMAGE }}:${{ env.SHORT_SHA }}
85+ # docker push ${{ env.DOCKER_IMAGE }}:latest
86+
87+ terraform :
88+ needs : [test, build_and_push]
89+ runs-on : ubuntu-latest
90+ if : github.ref == 'refs/heads/main'
91+
92+ steps :
93+ - name : Checkout code
94+ uses : actions/checkout@v4
95+
96+ - name : Setup Terraform
97+ uses : hashicorp/setup-terraform@v3
98+ with :
99+ terraform_version : ~1.0
100+
101+ - name : Terraform Format Check
102+ run : terraform fmt -check
103+
104+ - name : Terraform Init
105+ run : terraform init
106+
107+ - name : Terraform Validate
108+ run : terraform validate
109+
110+ - name : Terraform Plan
111+ run : terraform plan
112+ env :
113+ TF_VAR_environment : " dev"
0 commit comments