/kubectl-aws-eks

A Github action for kubectl, the Kubernetes CLI

Primary LanguageShell

Docker and Github Action for Kubernetes CLI

This action provides kubectl for Github Actions.

Usage

.github/workflows/push.yml

on: push
name: deploy
jobs:
  deploy:
    name: deploy to cluster
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-2
    
    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: deploy to cluster
      uses: kodermax/kubectl-aws-eks@main
      env:
        KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: my-app
        IMAGE_TAG: ${{ github.sha }}
      with:
        args: set image deployment/$ECR_REPOSITORY $ECR_REPOSITORY=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        
    - name: verify deployment
      uses: kodermax/kubectl-aws-eks@main
      env:
        KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
      with:
        args: rollout status deployment/my-app

Secrets

KUBE_CONFIG_DATArequired: A base64-encoded kubeconfig file with credentials for Kubernetes to access the cluster. You can get it by running the following command:

Bash

cat $HOME/.kube/config | base64

PowerShell

$base64Data = [Convert]::ToBase64String([IO.File]::ReadAllBytes("$env:USERPROFILE\.kube\config"))
Write-Output $base64Data

Make sure that your $HOME/.kube/config doesn't contain a AWS_PROFILE, i.e. remove the following section if it exists before doing the base64 encoding:

env:
- name: AWS_PROFILE
    value: github-actions

Configurable Variables

KUBECTL_VERSION - optional: By default, this action pulls the latest version of kubectl. To prevent potential dependency issue, you have the option to only use specific version.

      - name: deploy to cluster
        uses: kodermax/kubectl-aws-eks@main
        env:
          KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: my-app
          IMAGE_TAG: ${{ github.sha }
          KUBECTL_VERSION: "v1.22.0"
        with:
          args: set image deployment/$ECR_REPOSITORY $ECR_REPOSITORY=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

IAM_VERSION - optional: By default, this action pulls the latest version of aws-iam-authenticator. To prevent potential dependency issue, you have the option to only use specific version.

      - name: deploy to cluster
        uses: kodermax/kubectl-aws-eks@main
        env:
          KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }}
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: my-app
          IMAGE_TAG: ${{ github.sha }
          KUBECTL_VERSION: "v1.22.0"
          IAM_VERSION: "0.5.6"
        with:
          args: set image deployment/$ECR_REPOSITORY $ECR_REPOSITORY=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

Deploying database changes with Prisma Migrate on Kubernetes

name: Deploy Database Migrations
on:
  pull_request:
    branches: [main]
    paths:
      - 'packages/database/**'
jobs:
  deploy:
    runs-on: ubuntu-latest
    services:
      db:
        image: kodermax/kubectl-aws-eks:latest
        env:
          KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA_TEST }}
          RUN_COMMAND: port-forward svc/postgresql-1697720510 5432:5432 --address='0.0.0.0'
        ports:
          - 5432:5432/tcp
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - uses: pnpm/action-setup@v2
        with:
          version: 8
      - name: Install dependencies
        run: pnpm install
      - name: Apply all pending migrations to the database
        env:
          DATABASE_URL: ${{ secrets.TEST_DATABASE_URL }}
        run: pnpm db-deploy