1+ # Workflow alternativo para casos de emergencia o testing
2+ name : CI/CD Pipeline - Fixed
3+
4+ on :
5+ workflow_dispatch :
6+ inputs :
7+ skip_tests :
8+ description : ' ¿Saltar tests? (solo para emergencias)'
9+ required : true
10+ default : ' false'
11+ type : choice
12+ options :
13+ - ' true'
14+ - ' false'
15+ force_deploy :
16+ description : ' ¿Forzar deployment?'
17+ required : true
18+ default : ' false'
19+ type : choice
20+ options :
21+ - ' true'
22+ - ' false'
23+
24+ # Permisos necesarios para AWS OIDC
25+ permissions :
26+ id-token : write # Para AWS OIDC authentication
27+ contents : read # Para hacer checkout del código
28+
29+ env :
30+ AWS_REGION : eu-west-1
31+ ECR_REPOSITORY : neurobank-fastapi
32+ AWS_ACCOUNT_ID : 120242956739
33+ AWS_ROLE_ARN : arn:aws:iam::120242956739:role/GitHubActionsOIDCRole
34+
35+ jobs :
36+ test :
37+ runs-on : ubuntu-latest
38+ if : github.event.inputs.skip_tests != 'true'
39+ steps :
40+ - uses : actions/checkout@v4
41+
42+ - name : Set up Python
43+ uses : actions/setup-python@v5
44+ with :
45+ python-version : ' 3.11'
46+
47+ - name : Install dependencies
48+ run : |
49+ python -m pip install --upgrade pip
50+ pip install -r requirements.txt
51+
52+ - name : Run tests with coverage
53+ run : |
54+ python -m pytest --cov=app --cov-report=xml --cov-report=html
55+
56+ - name : Upload coverage reports
57+ uses : actions/upload-artifact@v4
58+ if : always()
59+ with :
60+ name : coverage-reports
61+ path : |
62+ coverage.xml
63+ htmlcov/
64+
65+ security :
66+ runs-on : ubuntu-latest
67+ if : github.event.inputs.skip_tests != 'true'
68+ steps :
69+ - uses : actions/checkout@v4
70+
71+ - name : Set up Python
72+ uses : actions/setup-python@v5
73+ with :
74+ python-version : ' 3.11'
75+
76+ - name : Install dependencies
77+ run : |
78+ python -m pip install --upgrade pip
79+ pip install -r requirements.txt
80+ pip install bandit safety
81+
82+ - name : Run Bandit security scan
83+ run : |
84+ bandit -r app/ -f json -o bandit-report.json --skip B101 || true
85+
86+ - name : Run Safety vulnerability scan
87+ run : |
88+ pip freeze > current-requirements.txt
89+ safety scan --json --output safety-report.json --continue-on-error || true
90+
91+ - name : Upload security reports
92+ uses : actions/upload-artifact@v4
93+ if : always()
94+ with :
95+ name : security-reports-fixed
96+ path : |
97+ bandit-report.json
98+ safety-report.json
99+
100+ build-and-deploy :
101+ needs : [test, security]
102+ runs-on : ubuntu-latest
103+ if : |
104+ always() &&
105+ github.event.inputs.force_deploy == 'true' &&
106+ (github.event.inputs.skip_tests == 'true' ||
107+ (needs.test.result == 'success' && needs.security.result == 'success'))
108+
109+ steps :
110+ - name : Checkout
111+ uses : actions/checkout@v4
112+
113+ - name : Emergency deployment warning
114+ if : github.event.inputs.skip_tests == 'true'
115+ run : |
116+ echo "⚠️ WARNING: EMERGENCY DEPLOYMENT MODE"
117+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
118+ echo "🚨 Tests have been SKIPPED!"
119+ echo "🚨 This should only be used in emergency situations!"
120+ echo "🚨 Make sure to run full testing after deployment!"
121+ echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
122+
123+ - name : Set up Python
124+ uses : actions/setup-python@v5
125+ with :
126+ python-version : ' 3.11'
127+
128+ - name : Configure AWS credentials via OIDC
129+ uses : aws-actions/configure-aws-credentials@v4
130+ with :
131+ role-to-assume : ${{ env.AWS_ROLE_ARN }}
132+ aws-region : ${{ env.AWS_REGION }}
133+ role-session-name : GitHubActions-Fixed-${{ github.run_id }}
134+
135+ - name : Verify AWS connection
136+ run : |
137+ echo "🔍 Verifying AWS OIDC connection..."
138+ aws sts get-caller-identity
139+ echo "✅ AWS connection verified!"
140+
141+ - name : Setup SAM CLI
142+ uses : aws-actions/setup-sam@v2
143+ with :
144+ use-installer : true
145+
146+ - name : Create ECR repository if not exists
147+ run : |
148+ aws ecr describe-repositories --repository-names ${{ env.ECR_REPOSITORY }} --region ${{ env.AWS_REGION }} || \
149+ aws ecr create-repository --repository-name ${{ env.ECR_REPOSITORY }} --region ${{ env.AWS_REGION }}
150+
151+ - name : Login to Amazon ECR
152+ id : login-ecr
153+ uses : aws-actions/amazon-ecr-login@v2
154+
155+ - name : Build and push Docker image
156+ env :
157+ ECR_REGISTRY : ${{ steps.login-ecr.outputs.registry }}
158+ IMAGE_TAG : fixed-${{ github.sha }}
159+ run : |
160+ docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
161+ docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
162+ docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest
163+ docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
164+
165+ - name : Deploy to AWS Lambda
166+ run : |
167+ sam build --region ${{ env.AWS_REGION }}
168+ sam deploy --no-confirm-changeset --no-fail-on-empty-changeset \
169+ --stack-name neurobank-api-fixed \
170+ --capabilities CAPABILITY_IAM \
171+ --region ${{ env.AWS_REGION }} \
172+ --parameter-overrides ApiKey=${{ secrets.API_KEY || 'emergency-deploy-key' }}
173+ echo "🎉 Emergency deployment completed!"
0 commit comments