1+ name : FlexNetSim API Workflow
2+
3+ on :
4+ # Run on pushes to any branch
5+ push :
6+ branches :
7+ - ' **'
8+ # Run on pull requests to these branches
9+ pull_request :
10+ branches :
11+ - master
12+ - develop
13+ # Allow manual trigger
14+ workflow_dispatch :
15+
16+ # Permissions needed for GitHub Pages
17+ permissions :
18+ contents : read
19+ pages : write
20+ id-token : write
21+
22+ # Allow only one concurrent deployment
23+ concurrency :
24+ group : " pages"
25+ cancel-in-progress : false
26+
27+ jobs :
28+ # Job 1: Run tests and generate coverage report
29+ test-coverage :
30+ name : Run Tests with Coverage
31+ runs-on : ubuntu-latest
32+ steps :
33+ - name : Checkout code
34+ uses : actions/checkout@v4
35+
36+ - name : Set up Python 3.9
37+ uses : actions/setup-python@v4
38+ with :
39+ python-version : 3.9
40+
41+ - name : Install dependencies
42+ run : |
43+ python -m pip install --upgrade pip
44+ pip install -r requirements.txt
45+
46+ - name : Run tests with coverage
47+ run : |
48+ pytest --cov=backend --cov-report=html tests/
49+
50+ - name : Prepare coverage report for GitHub Pages
51+ run : |
52+ mkdir -p public/coverage
53+ mv htmlcov/* public/coverage/
54+
55+ - name : Upload static files as artifact
56+ uses : actions/upload-pages-artifact@v3
57+ with :
58+ path : ' ./public'
59+
60+ # Job 2: Deploy GitHub Pages
61+ deploy-pages :
62+ name : Deploy Coverage Report
63+ if : github.ref == 'refs/heads/master'
64+ environment :
65+ name : github-pages
66+ url : ${{ steps.deployment.outputs.page_url }}
67+ runs-on : ubuntu-latest
68+ needs : test-coverage
69+ steps :
70+ - name : Setup Pages
71+ uses : actions/configure-pages@v3
72+
73+ - name : Deploy to GitHub Pages
74+ id : deployment
75+ uses : actions/deploy-pages@v2
76+
77+ # Job 3: Build and push Docker image (master only)
78+ build-and-push-image :
79+ name : Build and Push Docker Image
80+ needs : test-coverage
81+ if : github.ref == 'refs/heads/master'
82+ runs-on : ubuntu-latest
83+ steps :
84+ - name : Checkout code
85+ uses : actions/checkout@v4
86+
87+ - name : Google Cloud Auth
88+ uses : google-github-actions/auth@v2
89+ with :
90+ credentials_json : ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
91+
92+ - name : Set up Google Cloud CLI
93+ uses : google-github-actions/setup-gcloud@v2
94+ with :
95+ version : latest
96+ project_id : ${{ secrets.GOOGLE_PROJECT }}
97+
98+ - name : Configure Docker to push to Artifact Registry
99+ run : gcloud auth configure-docker us-central1-docker.pkg.dev
100+
101+ - name : Build and Push Docker image to Artifact Registry
102+ env :
103+ GOOGLE_PROJECT : ${{ secrets.GOOGLE_PROJECT }}
104+ IMAGE_NAME : us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api
105+ IMAGE_TAG : latest
106+ run : |
107+ docker build -t $IMAGE_NAME:$IMAGE_TAG .
108+ docker push $IMAGE_NAME:$IMAGE_TAG
109+
110+ # Job 4: Deploy to Cloud Run (master only)
111+ deploy-to-cloud-run :
112+ name : Deploy to Cloud Run
113+ needs : build-and-push-image
114+ if : github.ref == 'refs/heads/master'
115+ runs-on : ubuntu-latest
116+ steps :
117+ - name : Checkout code
118+ uses : actions/checkout@v4
119+
120+ - name : Google Cloud Auth
121+ uses : google-github-actions/auth@v2
122+ with :
123+ credentials_json : ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
124+
125+ - name : Deploy to Cloud Run
126+ uses : google-github-actions/deploy-cloudrun@v1
127+ with :
128+ image : us-central1-docker.pkg.dev/${{ secrets.GOOGLE_PROJECT }}/flex-net-sim-repo/fns-api:latest
129+ service : fns-api-cloud-run
130+ region : us-central1
131+ project_id : ${{ secrets.GOOGLE_PROJECT }}
0 commit comments