|
| 1 | +name: Build DC-analytics image, deploy it to GCR. Run GKE. Run DC-analytics in GKE |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | + |
| 9 | +# Environment variables. |
| 10 | +# ${{ secrets }} are taken from GitHub -> Settings -> Secrets |
| 11 | +# ${{ github.sha }} is the commit hash |
| 12 | +env: |
| 13 | + PROJECT_ID: iris-community-demos |
| 14 | + SERVICE_ACCOUNT_KEY: ${{ secrets.SERVICE_ACCOUNT_KEY }} |
| 15 | + GOOGLE_CREDENTIALS: ${{ secrets.TF_SERVICE_ACCOUNT_KEY }} |
| 16 | + GITHUB_SHA: ${{ github.sha }} |
| 17 | + GCR_LOCATION: eu.gcr.io |
| 18 | + IMAGE_NAME: dc-analytics-image |
| 19 | + GKE_CLUSTER: dc-analytics-cluster |
| 20 | + GKE_ZONE: europe-west1-b |
| 21 | + K8S_NAMESPACE: iris |
| 22 | + STATEFULSET_NAME: dc-analytics |
| 23 | + |
| 24 | +jobs: |
| 25 | + gcloud-setup-and-build-and-publish-to-GCR: |
| 26 | + name: Setup gcloud utility, Build DC-ANALYTICS image and Publish it to Container Registry |
| 27 | + runs-on: ubuntu-18.04 |
| 28 | + steps: |
| 29 | + - name: Checkout |
| 30 | + uses: actions/checkout@v2 |
| 31 | + |
| 32 | + - name: Setup gcloud cli |
| 33 | + uses: GoogleCloudPlatform/github-actions/setup-gcloud@master |
| 34 | + with: |
| 35 | + version: '275.0.0' |
| 36 | + service_account_key: ${{ secrets.SERVICE_ACCOUNT_KEY }} |
| 37 | + |
| 38 | + - name: Configure docker to use the gcloud as a credential helper |
| 39 | + run: | |
| 40 | + gcloud auth configure-docker |
| 41 | +
|
| 42 | + - name: Build DC-ANALYTICS image |
| 43 | + run: | |
| 44 | + docker build -t ${GCR_LOCATION}/${PROJECT_ID}/${IMAGE_NAME}:${GITHUB_SHA} . |
| 45 | +
|
| 46 | + - name: Publish DC-ANALYTICS image to Google Container Registry |
| 47 | + run: | |
| 48 | + docker push ${GCR_LOCATION}/${PROJECT_ID}/${IMAGE_NAME}:${GITHUB_SHA} |
| 49 | +
|
| 50 | + gke-provisioner: |
| 51 | + # Inspired by: |
| 52 | + ## https://www.terraform.io/docs/github-actions/getting-started.html |
| 53 | + ## https://github.com/hashicorp/terraform-github-actions |
| 54 | + name: Provision GKE cluster |
| 55 | + runs-on: ubuntu-18.04 |
| 56 | + steps: |
| 57 | + - name: Checkout |
| 58 | + uses: actions/checkout@v2 |
| 59 | + |
| 60 | + - name: Terraform init |
| 61 | + uses: hashicorp/terraform-github-actions@master |
| 62 | + with: |
| 63 | + tf_actions_version: 0.12.17 |
| 64 | + tf_actions_subcommand: 'init' |
| 65 | + tf_actions_working_dir: 'terraform' |
| 66 | + |
| 67 | + - name: Terraform validate |
| 68 | + uses: hashicorp/terraform-github-actions@master |
| 69 | + with: |
| 70 | + tf_actions_version: 0.12.17 |
| 71 | + tf_actions_subcommand: 'validate' |
| 72 | + tf_actions_working_dir: 'terraform' |
| 73 | + |
| 74 | + - name: Terraform plan |
| 75 | + uses: hashicorp/terraform-github-actions@master |
| 76 | + with: |
| 77 | + tf_actions_version: 0.12.17 |
| 78 | + tf_actions_subcommand: 'plan' |
| 79 | + tf_actions_working_dir: 'terraform' |
| 80 | + |
| 81 | + - name: Terraform apply |
| 82 | + uses: hashicorp/terraform-github-actions@master |
| 83 | + with: |
| 84 | + tf_actions_version: 0.12.17 |
| 85 | + tf_actions_subcommand: 'apply' |
| 86 | + tf_actions_working_dir: 'terraform' |
| 87 | + |
| 88 | + kubernetes-deploy: |
| 89 | + name: Deploy Kubernetes manifests to GKE cluster |
| 90 | + needs: |
| 91 | + - gcloud-setup-and-build-and-publish-to-GCR |
| 92 | + - gke-provisioner |
| 93 | + runs-on: ubuntu-18.04 |
| 94 | + steps: |
| 95 | + - name: Checkout |
| 96 | + uses: actions/checkout@v2 |
| 97 | + |
| 98 | + - name: Replace placeholders with values in statefulset template |
| 99 | + working-directory: ./k8s/ |
| 100 | + run: | |
| 101 | + cat statefulset.tpl |\ |
| 102 | + sed "s|DOCKER_REPO_NAME|${GCR_LOCATION}/${PROJECT_ID}/${IMAGE_NAME}|" |\ |
| 103 | + sed "s|DOCKER_IMAGE_TAG|${GITHUB_SHA}|" > statefulset.yaml |
| 104 | + cat statefulset.yaml |
| 105 | +
|
| 106 | + - name: Setup gcloud cli |
| 107 | + uses: GoogleCloudPlatform/github-actions/setup-gcloud@master |
| 108 | + with: |
| 109 | + version: '275.0.0' |
| 110 | + service_account_key: ${{ secrets.SERVICE_ACCOUNT_KEY }} |
| 111 | + |
| 112 | + - name: Apply Kubernetes manifests |
| 113 | + working-directory: ./k8s/ |
| 114 | + run: | |
| 115 | + gcloud container clusters get-credentials ${GKE_CLUSTER} --zone ${GKE_ZONE} --project ${PROJECT_ID} |
| 116 | + kubectl apply -f namespace.yaml |
| 117 | + kubectl apply -f service.yaml |
| 118 | + kubectl apply -f statefulset.yaml |
| 119 | + kubectl -n ${K8S_NAMESPACE} rollout status statefulset/${STATEFULSET_NAME} |
0 commit comments