Fix Terraform configuration for CI/CD compatibility #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| name: DevOps Project Pipeline | |
| on: | |
| push: | |
| branches: [ main, develop ] | |
| pull_request: | |
| branches: [ main ] | |
| env: | |
| DOCKER_IMAGE: ozzyi/devops-project | |
| REGISTRY: docker.io | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.9' | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install flask pytest | |
| - name: Test Flask app | |
| run: | | |
| python -c " | |
| import sys | |
| sys.path.append('.') | |
| from app import app | |
| with app.test_client() as client: | |
| response = client.get('/') | |
| assert response.status_code == 200 | |
| print('✅ Flask app test passed') | |
| " | |
| build_and_push: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set Short SHA | |
| run: | | |
| echo "SHORT_SHA=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_ENV | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Build Docker Image | |
| run: | | |
| docker build -t ${{ env.DOCKER_IMAGE }}:${{ env.SHORT_SHA }} . | |
| docker build -t ${{ env.DOCKER_IMAGE }}:latest . | |
| - name: Test Docker Image | |
| run: | | |
| docker run -d -p 8080:8080 --name test-container ${{ env.DOCKER_IMAGE }}:${{ env.SHORT_SHA }} | |
| sleep 5 | |
| curl -f http://localhost:8080 || exit 1 | |
| docker stop test-container | |
| docker rm test-container | |
| echo "✅ Docker image test passed" | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Push Docker Image | |
| run: | | |
| docker push ${{ env.DOCKER_IMAGE }}:${{ env.SHORT_SHA }} | |
| docker push ${{ env.DOCKER_IMAGE }}:latest | |
| terraform: | |
| needs: [test, build_and_push] | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_version: ~1.0 | |
| - name: Terraform Format Check | |
| run: terraform fmt -check | |
| - name: Terraform Init | |
| run: terraform init | |
| - name: Terraform Validate | |
| run: terraform validate | |
| - name: Terraform Plan | |
| run: terraform plan -var="create_k8s_resources=false" | |
| env: | |
| TF_VAR_environment: "dev" | |
| TF_VAR_create_k8s_resources: "false" |