Pipeline #176
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: Pipeline | |
on: | |
# push: | |
# branches: | |
# - main | |
workflow_dispatch: | |
inputs: | |
custom-name: | |
description: 'Enter a name for the workflow run' | |
required: false | |
env: | |
PROJECT_ID: named-signal-392608 | |
BUCKET_NAME: chart-packages | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Login to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Create Docker Env | |
run: | | |
echo "API_KEY=${{ secrets.API_KEY }}" > ./app/.env | |
- name: Define TAG and HELM_PACKAGE | |
run: | | |
TAG=$(bash versioning.sh) | |
HELM_PACKAGE=nasa-app-${TAG}.tgz | |
echo "TAG=${TAG}" >> $GITHUB_ENV | |
echo "HELM_PACKAGE=${HELM_PACKAGE}" >> $GITHUB_ENV | |
- name: Build and Push Docker Image | |
uses: docker/build-push-action@v2 | |
with: | |
context: ./app | |
file: ./app/Dockerfile | |
push: true | |
tags: ${{ secrets.DOCKER_USERNAME }}/gha-pipeline:${{ env.TAG }} | |
- name: Create Helm Chart files | |
run: | | |
echo "apiVersion: v2" > ./chart/Chart.yaml | |
echo "name: nasa-app" >> ./chart/Chart.yaml | |
echo "version: ${{ env.TAG }}" >> ./chart/Chart.yaml | |
echo "description: A Helm chart for deploying Flask and Redis" >> ./chart/Chart.yaml | |
echo "TAG: ${{ env.TAG }}" > ./chart/values.yaml | |
- name: Package Helm Chart | |
run: | | |
helm package ./chart -d ./chart | |
- name: Set up Google Cloud SDK | |
uses: google-github-actions/setup-gcloud@v0.3.0 | |
with: | |
project_id: ${{ env.PROJECT_ID }} | |
service_account_key: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }} | |
export_default_credentials: true | |
- name: Upload Helm Chart to GCP | |
run: | | |
gsutil cp ./chart/${{ env.HELM_PACKAGE }} gs://${{ env.BUCKET_NAME }} | |
test: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Define TAG and HELM_PACKAGE | |
run: | | |
TAG=$(bash versioning.sh --no-rename) | |
HELM_PACKAGE=nasa-app-${TAG}.tgz | |
echo "TAG=${TAG}" >> $GITHUB_ENV | |
echo "HELM_PACKAGE=${HELM_PACKAGE}" >> $GITHUB_ENV | |
- name: Set up Google Cloud SDK | |
uses: google-github-actions/setup-gcloud@v0.3.0 | |
with: | |
project_id: ${{ env.PROJECT_ID }} | |
service_account_key: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }} | |
export_default_credentials: true | |
- name: Get GKE plugin | |
run: | | |
gcloud components install gke-gcloud-auth-plugin | |
- name: Configure kubectl | |
run: | | |
gcloud container clusters get-credentials test-cluster --region us-central1-a | |
- name: Download Helm chart from GCP bucket | |
run: | | |
gsutil cp gs://${{ env.BUCKET_NAME }}/${{ env.HELM_PACKAGE }} . | |
- name: Deploy Helm chart | |
run: | | |
kubectl get namespace app >/dev/null 2>&1 && echo "Namespace 'app' already exists." || kubectl create namespace app && echo "Namespace 'app' created." | |
helm upgrade --install nasa-app ${{ env.HELM_PACKAGE }} --namespace app | |
- name: Run Helm Tests | |
run: | | |
helm test nasa-app --namespace app || (curl -X POST https://us-central1-named-signal-392608.cloudfunctions.net/mark_failures && exit 1) | |
deploy: | |
needs: test | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Define TAG and HELM_PACKAGE | |
run: | | |
TAG=$(bash versioning.sh --no-rename) | |
HELM_PACKAGE=nasa-app-${TAG}.tgz | |
echo "TAG=${TAG}" >> $GITHUB_ENV | |
echo "HELM_PACKAGE=${HELM_PACKAGE}" >> $GITHUB_ENV | |
- name: Set up Google Cloud SDK | |
uses: google-github-actions/setup-gcloud@v0.3.0 | |
with: | |
project_id: ${{ env.PROJECT_ID }} | |
service_account_key: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }} | |
export_default_credentials: true | |
- name: Get GKE plugin | |
run: | | |
gcloud components install gke-gcloud-auth-plugin | |
- name: Configure kubectl | |
run: | | |
gcloud container clusters get-credentials prod-cluster --region us-central1-a | |
- name: Download Helm chart from GCP bucket | |
run: | | |
gsutil cp gs://${{ env.BUCKET_NAME }}/${{ env.HELM_PACKAGE }} . | |
- name: Deploy Helm chart | |
run: | | |
kubectl get namespace app >/dev/null 2>&1 && echo "Namespace 'app' already exists." || kubectl create namespace app && echo "Namespace 'app' created." | |
helm upgrade --install nasa-app ${{ env.HELM_PACKAGE }} --namespace app | |
monitor: | |
needs: deploy | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Google Cloud SDK | |
uses: google-github-actions/setup-gcloud@v0.3.0 | |
with: | |
project_id: ${{ env.PROJECT_ID }} | |
service_account_key: ${{ secrets.GCP_SERVICE_ACCOUNT_KEY }} | |
export_default_credentials: true | |
- name: Get GKE plugin | |
run: | | |
gcloud components install gke-gcloud-auth-plugin | |
- name: Configure kubectl | |
run: | | |
gcloud container clusters get-credentials prod-cluster --region us-central1-a | |
- name: Check and Install Prometheus and Grafana | |
run: | | |
if helm list -n monitoring | grep -q "prometheus"; then | |
echo "Prometheus is already installed." | |
else | |
kubectl create namespace monitoring | |
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts | |
helm repo update | |
helm install prometheus prometheus-community/kube-prometheus-stack --namespace monitoring \ | |
--set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false \ | |
--set prometheus.prometheusSpec.serviceMonitorNamespaceSelector.matchNames=app echo "Prometheus installed." | |
echo "Prometheus installed." | |
kubectl patch svc prometheus-grafana -n monitoring -p '{"spec": {"type": "LoadBalancer"}}' | |
fi | |
cleanups: | |
needs: deploy | |
runs-on: ubuntu-latest | |
steps: | |
- name: Clean failed packages if there are more than 5 | |
run: | | |
curl https://us-central1-named-signal-392608.cloudfunctions.net/failures_handling | |
- name: Clean Bucket packages if there are more than 20 | |
run: | | |
curl https://us-central1-named-signal-392608.cloudfunctions.net/bucket_handling | |
- name: Clean images in DockerHub if there are more than 20 | |
run: | | |
curl https://us-central1-named-signal-392608.cloudfunctions.net/docker_registry_handling |