Skip to content

Build Container & Deploy to K8s #144

Build Container & Deploy to K8s

Build Container & Deploy to K8s #144

Workflow file for this run

name: Build Container & Deploy to K8s
on:
push:
branches:
- main
paths:
- bin/**
- site/**
- Containerfile
- .github/workflows/deploy.yml
# Schedule workflow run at 00:00 on Sunday
schedule:
- cron: '0 0 * * 0'
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
# Build the container
build-container:
name: Build Container
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
# list of Docker images to use as base name for tags
images: |
ghcr.io/kenmoini/kenmoini.com
# generate Docker tags based on the following events/attributes
tags: |
type=ref,event=branch
type=ref,event=tag
type=sha,prefix=,suffix=,format=short
type=sha,prefix=,suffix=,format=long
type=raw,value=latest
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: "${{ secrets.REGISTRY_USERNAME }}"
password: "${{ secrets.REGISTRY_TOKEN }}"
#- name: Install doctl
# uses: digitalocean/action-doctl@v2
# with:
# token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
file: ./Containerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: pass environment
uses: appleboy/[email protected]
env:
KUBECONFIG: "/home/cicd/.kube/config"
GIT_SHA: ${{ github.sha }}
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
envs: KUBECONFIG,GIT_SHA
script: |
cd /opt/kenmoini.com/deploy/k3s
git pull || true
cp overlays/image-patch/kustomization.yml overlays/image-patch/kustomization.yml.bak
TAG=$(echo $GIT_SHA | head -c7) && sed -i 's|PATCH_ME|'${TAG}'|' overlays/image-patch/kustomization.yml
export KUBECONFIG="/home/cicd/.kube/config"
kubectl apply -k overlays/image-patch
mv overlays/image-patch/kustomization.yml.bak overlays/image-patch/kustomization.yml
#- name: Save DigitalOcean kubeconfig with short-lived credentials
# run: doctl kubernetes cluster kubeconfig save --expiry-seconds 600 ${{ secrets.DO_CLUSTER_ID }}
#- name: Update deployment file
# run: TAG=$(echo $GITHUB_SHA | head -c7) && sed -i 's|:latest|:'${TAG}'|' $GITHUB_WORKSPACE/deploy/k8s/02-deployment.yaml
#- name: Deploy to DigitalOcean Kubernetes
# run: kubectl apply -f $GITHUB_WORKSPACE/deploy/k8s/