Skip to content

Merge pull request #1430 from ThingEngineering/main #1098

Merge pull request #1430 from ThingEngineering/main

Merge pull request #1430 from ThingEngineering/main #1098

name: Build and Deploy
on:
push:
branches:
- live
# Allow job to be manually started
workflow_dispatch:
jobs:
find-changes:
name: Find Changes
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Work out which projects changed in this push
- uses: dorny/[email protected]
id: filter
if: github.event_name != 'workflow_dispatch'
with:
base: live
filters: |
backend:
- 'apps/backend/**'
- 'packages/csharp-lib/**'
- 'Directory.Build.props'
web:
- 'apps/frontend/**'
- 'apps/web/**'
- 'packages/csharp-lib/**'
- 'Directory.Build.props'
- 'package.json'
- 'package-lock.json'
- 'tsconfig.base.json'
- name: Automatic run
if: github.event_name != 'workflow_dispatch'
run: |
echo 'PROJECTS=${{ steps.filter.outputs.changes }}' >> $GITHUB_ENV
# Deploy everything for manually triggered workflow
- name: Manual run
if: github.event_name == 'workflow_dispatch'
run: |
echo 'PROJECTS=["backend", "web"]' >> $GITHUB_ENV
outputs:
projects: ${{ env.PROJECTS }}
build:
name: Build
needs: find-changes
runs-on: ubuntu-latest
if: needs.find-changes.outputs.projects != '[]'
strategy:
matrix:
project: ${{ fromJSON(needs.find-changes.outputs.projects) }}
steps:
- uses: actions/checkout@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
# We need a docker-container builder to export cache to registry
# https://github.com/docker/buildx/blob/master/docs/reference/buildx_build.md#-use-an-external-cache-source-for-a-build---cache-from
- name: Setup docker
run: |
docker buildx create --name sighbuilder --use
- name: Build and push container image
run: |
IMAGE_URL="ghcr.io/thingengineering/wowthing-again/${{ matrix.project }}"
docker buildx build \
--progress=plain \
--rm \
--cache-from=type=registry,ref=$IMAGE_URL:cache \
--cache-to=type=registry,ref=$IMAGE_URL:cache,mode=max \
--output type=image,\"name=$IMAGE_URL:$GITHUB_SHA,$IMAGE_URL:latest\",push=true \
--file=./apps/${{ matrix.project }}/Dockerfile \
.
deploy:
name: Deploy
needs: [find-changes, build]
runs-on: ubuntu-latest
strategy:
matrix:
project: ${{ fromJSON(needs.find-changes.outputs.projects) }}
steps:
- uses: actions/checkout@v2
- uses: azure/setup-kubectl@v3
- name: Setup kubectl
run: |
kubectl config set-cluster k8s --server="${{ secrets.KUBE_URL }}"
kubectl config set clusters.k8s.certificate-authority-data ${{ secrets.KUBE_CA_DATA }}
kubectl config set-credentials github --token="${{ secrets.KUBE_TOKEN }}"
kubectl config set-context default --cluster=k8s --user=github --namespace=things
kubectl config use-context default
- name: Deploy to k8s
run: |
DEPLOYMENT_FILE="_magic/k8s-deployment-${{ matrix.project }}.yml"
sed -i "s/_VERSION_/$GITHUB_SHA/g" $DEPLOYMENT_FILE
kubectl apply -f $DEPLOYMENT_FILE