Merge branch 'main' of https://github.com/ozzyib/devops-project #17
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('src') | |
| 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 }} -f src/Dockerfile src/ | |
| docker build -t ${{ env.DOCKER_IMAGE }}:latest -f src/Dockerfile src/ | |
| - 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 | |
| - name: Configure Git Author | |
| run: | | |
| git config --local user.email "hello@ozzyi.com" | |
| git config --local user.name "ozzyi0b" | |
| - name: Update Helm Chart with new image tag | |
| run: | | |
| sed -i.bak "s|tag: .*|tag: \"${{ env.SHORT_SHA }}\"|g" k8s/helm/devops-app/values.yaml | |
| rm k8s/helm/devops-app/values.yaml.bak | |
| git add k8s/helm/devops-app/values.yaml | |
| git commit -m "Update Helm chart with new image tag ${{ env.SHORT_SHA }}" | |
| git push | |
| 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.5.7 | |
| - name: Terraform Format Check | |
| run: | | |
| cd infrastructure | |
| terraform fmt -check | |
| - name: Terraform Init | |
| run: | | |
| cd infrastructure | |
| terraform init | |
| - name: Terraform Validate | |
| run: | | |
| cd infrastructure | |
| terraform validate | |
| - name: Terraform Plan | |
| run: | | |
| cd infrastructure | |
| terraform plan -var="create_k8s_resources=false" | |
| env: | |
| TF_VAR_environment: "dev" | |
| TF_VAR_create_k8s_resources: "false" |