Merge pull request #39 from codeuniversity/weather-module-refactor #47
This file contains 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: Auto Deploy to Google Cloud | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
env: | |
GOOGLE_PROJECT_ID: ${{ vars.GOOGLE_PROJECT_ID }} | |
GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS }} | |
GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL }} | |
ENVIRONMENT: ${{ vars.ENVIRONMENT }} | |
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GCP_SA_KEY }} | |
BACKEND_IMAGE_NAME: europe-west3-docker.pkg.dev/thf-climate-cloud/thf-climate/thf-climate | |
FRONTEND_IMAGE_NAME: europe-west3-docker.pkg.dev/thf-climate-cloud/thf-climate-frontend/thf-climate-frontend | |
steps: | |
- uses: actions/checkout@v3 | |
# Step 2: Decode and Write the Service Account Key to a file properly | |
- name: Set up Google Cloud credentials | |
run: | | |
echo "${{ secrets.GCP_SA_KEY_BASE64 }}" | base64 --decode > ${HOME}/gcloud-service-key.json | |
# Step 3: Set up Google Cloud SDK | |
- name: Set up Google Cloud SDK | |
uses: google-github-actions/setup-gcloud@v1 | |
with: | |
project_id: ${{ secrets.GCP_PROJECT_ID }} | |
service_account_key: ${{ secrets.GCP_SA_KEY }} | |
export_default_credentials: true | |
# Step 4: Activate Service Account Explicitly | |
- name: Activate Service Account | |
run: | | |
gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json | |
gcloud config set account $(gcloud config get-value core/account) | |
# Step 5: Configure Docker for Google Artifact Registry | |
- name: Configure Docker for Google Artifact Registry | |
run: | | |
gcloud auth configure-docker europe-west3-docker.pkg.dev | |
# Step 6: Build and the run the backend | |
- name: Build and Run the Docker image (backend) | |
working-directory: ./backend | |
run: | | |
docker buildx build --platform linux/amd64 -t $BACKEND_IMAGE_NAME:latest . | |
docker run -d -p 8000:8000 --name thf-climate-backend $BACKEND_IMAGE_NAME | |
- name: Wait for the application to start | |
working-directory: ./backend | |
run: | | |
timeout 30 bash -c 'until curl -s http://127.0.0.1:8000/; do sleep 1; done' | |
# Step 7: Run the tests against the local application | |
- name: Install dependencies and Run tests (backend) | |
working-directory: ./backend | |
env: | |
API_BASE_URL: "http://127.0.0.1:8000" | |
GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS }} | |
GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL: ${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL }} | |
ENVIRONMENT: "PRODUCTION" | |
run: | | |
python -m pip install --upgrade pip | |
pip install -r requirements-dev.txt | |
pytest | |
# Step 8: Push and Deploy the backend on Google cloud | |
- name: Push Docker image to Artifact Registry (backend) | |
working-directory: ./backend | |
run: | | |
docker push $BACKEND_IMAGE_NAME:latest | |
- name: Deploy to Cloud Run (backend) | |
working-directory: ./backend | |
run: | | |
gcloud run deploy thf-climate-run \ | |
--image=$BACKEND_IMAGE_NAME:latest \ | |
--port=8000 \ | |
--region=europe-west3 \ | |
--allow-unauthenticated \ | |
--platform=managed \ | |
--min-instances=1 \ | |
--max-instances=5 \ | |
--set-env-vars "GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS=${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_CREDENTIALS }}" \ | |
--set-env-vars "GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL=${{ secrets.GOOGLE_EARTH_ENGINE_SERVICE_ACCOUNT_EMAIL }}" \ | |
--set-env-vars "ENVIRONMENT=PRODUCTION" | |
# Step 9: Build and Deploy the frontend | |
- name: Build Docker image (frontend) | |
working-directory: ./frontend | |
run: | | |
docker buildx build --platform linux/amd64 -t $FRONTEND_IMAGE_NAME:latest . | |
- name: Push Docker image to Artifact Registry (frontend) | |
working-directory: ./frontend | |
run: | | |
docker push $FRONTEND_IMAGE_NAME:latest | |
- name: Deploy to Cloud Run (frontend) | |
working-directory: ./frontend | |
run: | | |
gcloud run deploy thf-climate-frontend-run \ | |
--image=$FRONTEND_IMAGE_NAME:latest \ | |
--port=80 \ | |
--region=europe-west3 \ | |
--allow-unauthenticated \ | |
--platform=managed \ | |
--min-instances=1 \ | |
--max-instances=5 |