GitHub Actions for GKE

GitHub Actions enables you to create custom software development life cycle (SDLC) workflows directly in your GitHub repository.

You can write individual tasks, called actions, and combine them to create a custom workflow. Workflows are custom automated processes that you can set up in your repository to build, test, package, release, or deploy any code project on GitHub.

With GitHub Actions you can build end-to-end continuous integration (CI) and continuous deployment (CD) capabilities directly in your repository.

Workflows run in Linux, macOS, Windows, and containers on GitHub-hosted machines, called ‘runners’. Alternatively, you can also host your own runners to run workflows on machines you own or manage.

Before you start this procedure, you must have:

  • A running GKE cluster with Manifests files for your demo/webapps

Demo workflow file for GKE .github/workflows/gke.yaml

---
name: Build and Deploy to GKE
on:  # yamllint disable-line
  # Trigger the workflow on push or pull request,
  # but only for the master branch
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

# Environment variables available to all jobs and steps in this workflow
env:
  GITHUB_SHA: ${{ github.sha }}

jobs:
  setup-build-publish-deploy:
    name: Setup, Build, Publish, and Deploy
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.6]
        node-version: [9.8]

    steps:
    - name: Checkout  # yamllint disable-line
      uses: actions/checkout@v2

    - uses: actions/cache@v1
      id: Linux-pip-cache
      with:
        path: ~/.cache/pip
        key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
        restore-keys: |
          ${{ runner.os }}-pip-

    - uses: actions/cache@v1
      id: npm-cache
      with:
        path: ~/.npm
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-node-

    - uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}

    - uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}

    # Setup gcloud CLI
    - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
      with:
        version: '285.0.0'
        project_id: ${{ secrets.GKE_PROJECT }}
        service_account_email: ${{ secrets.GKE_EMAIL }}
        service_account_key: ${{ secrets.GKE_KEY }}
    - run: gcloud info

    # Configure docker to use the gcloud command-line tool as a credential helper
    - run: |
        # Set up docker to authenticate
        # via gcloud command-line tool.
        gcloud auth configure-docker

     # Build the Docker image
    - name: Build
       run: |
         export NODE_ENV=production
         make install
         docker build -t=$REGISTRY_HOSTNAME/$GKE_PROJECT/$IMAGE:${GITHUB_SHA} .

       env:
         USERNAME: ${{ secrets.USERNAME }}
         PASSWORD: ${{ secrets.PASSWORD }}
         URL: ${{ secrets.URL }}

     # Build the Docker image
    - name: Publish
      run: |
        docker push $REGISTRY_HOSTNAME/$GKE_PROJECT/$IMAGE:${GITHUB_SHA}

    # Set up kustomize
    - name: Set up Kustomize and kexpand
      run: |
        curl -o kustomize --location https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64
        chmod u+x ./kustomize
        sudo mv ./kustomize /usr/bin/kustomize

    # Update the Docker image to the GKE cluster
    - name: Deploy
      run: |
        gcloud container clusters get-credentials $GKE_CLUSTER --zone $GKE_ZONE --project $GKE_PROJECT
        kustomize edit set image $REGISTRY_HOSTNAME/$GKE_PROJECT/$IMAGE:${GITHUB_SHA}
        kustomize build . | kubectl apply -f -
        kustomize build manifests/stg/reporting | kubectl apply -f -
        kubectl rollout status deployment/demo