1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.5 KiB
YAML

kind: pipeline
type: docker
name: default
environment:
APP_NAME: example-app
GCP_PROJECT: <GCP_PROJECT_ID>
GCP_REGISTRY: <GCP_REGISTRY_URL>
GCP_REGION: <GCP_REGION>
GCP_KUBE_CLUSTER: <GCP_GKE_CLUSTER_NAME>
GCP_DOMAIN: <GCP_DOMAIN_NAME>
steps:
- name: google authentication & bastion
image: google/cloud-sdk
environment:
GCP_CREDENTIALS:
from_secret: gcp-credentials
commands:
- mkdir .credentials
- echo -n "$GCP_CREDENTIALS" > .credentials/gcp-key.json
- gcloud auth activate-service-account --key-file .credentials/gcp-key.json
- gcloud --quiet config set project $GCP_PROJECT
- gcloud config set compute/region $GCP_REGION
- gcloud --quiet container clusters get-credentials $GCP_KUBE_CLUSTER
- apt-get install -y jq
- bash .drone-kube/start_bastion.sh
when:
branch:
- feature/*
- fix/*
- refactor/*
- intg/*
- trunk
event:
- push
- name: build image
image: docker:dind
volumes:
- name: dockersock
path: /var/run/docker.sock
environment:
DOCKER_HOST: unix:///var/run/docker.sock
commands:
- export VERSION=$(echo $DRONE_BRANCH | sed "s/\\//-/g"):latest
- export VERSION_SHA=$(echo $DRONE_BRANCH | sed "s/\\//-/g"):$DRONE_COMMIT_SHA
- docker build . -t $GCP_REGISTRY/$APP_NAME/$VERSION
- docker tag $GCP_REGISTRY/$APP_NAME/$VERSION $GCP_REGISTRY/$APP_NAME/$VERSION_SHA
when:
branch:
- feature/*
- fix/*
- refactor/*
- intg/*
- trunk
event:
- push
- name: push to GCP Artifact registry
image: google/cloud-sdk
volumes:
- name: dockersock
path: /var/run/docker.sock
environment:
DOCKER_HOST: unix:///var/run/docker.sock
commands:
- gcloud auth activate-service-account --key-file .credentials/gcp-key.json
- gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://$GCP_REGION-docker.pkg.dev
- export VERSION=$(echo $DRONE_BRANCH | sed "s/\\//-/g"):latest
- export VERSION_SHA=$(echo $DRONE_BRANCH | sed "s/\\//-/g"):$DRONE_COMMIT_SHA
- docker push $GCP_REGISTRY/$APP_NAME/$VERSION
- docker push $GCP_REGISTRY/$APP_NAME/$VERSION_SHA
when:
branch:
- feature/*
- fix/*
- refactor/*
- intg/*
- trunk
event:
- push
- name: deploy review to gke cluster
image: google/cloud-sdk
commands:
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- apt-get update && apt-get install gettext-base -y
- gcloud auth activate-service-account --key-file .credentials/gcp-key.json
- gcloud config set project $GCP_PROJECT
- gcloud container clusters get-credentials $GCP_KUBE_CLUSTER --region=$GCP_REGION
- gcloud compute ssh bob@$GCP_KUBE_CLUSTER-bastion --zone $GCP_REGION-a -- -4 -L8888:127.0.0.1:8888 -fN -f /dev/null
- export INSTANCE_NAME=$(echo $DRONE_BRANCH | sed "s/\\//-/g" | sed "s/feature-//g" | sed "s/^fix-//g" | sed "s/^refactor-//g" | sed "s/^intg-//g")
- export IMAGE=$GCP_REGISTRY/$APP_NAME/feature-$INSTANCE_NAME:$DRONE_COMMIT_SHA
- export HTTPS_PROXY=127.0.0.1:8888
- echo $IMAGE
- kubectl create namespace $APP_NAME || echo "Namespace $APP_NAME exists"
- envsubst < .drone-kube/deployment.yml > deployment.yml
- kubectl apply -f deployment.yml
- echo "The application is available at https://$INSTANCE_NAME-$APP_NAME.$GCP_DOMAIN"
when:
branch:
- feature/*
- fix/*
- refactor/*
- intg/*
event: push
- name: destroy review app in gke cluster
image: google/cloud-sdk
commands:
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- apt-get update && apt-get install gettext-base -y
- gcloud config set project $GCP_PROJECT
- gcloud auth activate-service-account --key-file .credentials/gcp-key.json
- gcloud container clusters get-credentials $GCP_KUBE_CLUSTER --region=$GCP_REGION
- gcloud compute ssh bob@$GCP_KUBE_CLUSTER-bastion --zone $GCP_REGION-a -- -4 -L8888:127.0.0.1:8888 -fN -f /dev/null
- export HTTPS_PROXY=127.0.0.1:8888
- bash .drone-kube/cleanup.sh
when:
branch:
- trunk
event:
- push
volumes:
- name: cache
host:
path: /tmp/drone/cache/bundle
- name: cache-deploy
host:
path: /tmp/drone/cache-deploy/bundle
- name: dockersock
host:
path: /var/run/docker.sock