130 lines
4.3 KiB
YAML
130 lines
4.3 KiB
YAML
# Ref: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
push:
|
|
branches:
|
|
- dev
|
|
paths:
|
|
- backend/**
|
|
|
|
name: Build and deploy workers
|
|
|
|
jobs:
|
|
deploy:
|
|
name: Deploy
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v2
|
|
with:
|
|
# We need to diff with old commit
|
|
# to see which workers got changed.
|
|
fetch-depth: 2
|
|
# ref: staging
|
|
|
|
- name: Docker login
|
|
run: |
|
|
docker login ${{ secrets.OSS_REGISTRY_URL }} -u ${{ secrets.OSS_DOCKER_USERNAME }} -p "${{ secrets.OSS_REGISTRY_TOKEN }}"
|
|
|
|
- uses: azure/k8s-set-context@v1
|
|
with:
|
|
method: kubeconfig
|
|
kubeconfig: ${{ secrets.OSS_KUBECONFIG }} # Use content of kubeconfig in secret.
|
|
id: setcontext
|
|
|
|
# Caching docker images
|
|
- uses: satackey/action-docker-layer-caching@v0.0.11
|
|
# Ignore the failure of a step and avoid terminating the job.
|
|
continue-on-error: true
|
|
|
|
- name: Build, tag
|
|
id: build-image
|
|
env:
|
|
DOCKER_REPO: ${{ secrets.OSS_REGISTRY_URL }}
|
|
IMAGE_TAG: ${{ github.sha }}
|
|
ENVIRONMENT: staging
|
|
run: |
|
|
#
|
|
# TODO: Check the container tags are same, then skip the build and deployment.
|
|
#
|
|
# Build a docker container and push it to Docker Registry so that it can be deployed to Kubernetes cluster.
|
|
#
|
|
# Getting the images to build
|
|
#
|
|
set -x
|
|
{
|
|
git diff --name-only HEAD HEAD~1 | grep -E "backend/cmd|backend/services" | grep -vE ^ee/ | cut -d '/' -f3
|
|
|
|
git diff --name-only HEAD HEAD~1 | grep -E "backend/pkg|backend/internal" | grep -vE ^ee/ | cut -d '/' -f3 | uniq | while read -r pkg_name ; do
|
|
grep -rl "pkg/$pkg_name" backend/services backend/cmd | cut -d '/' -f3
|
|
done
|
|
} | uniq > /tmp/images_to_build.txt
|
|
|
|
[[ $(cat /tmp/images_to_build.txt) != "" ]] || (echo "Nothing to build here"; exit 0)
|
|
#
|
|
# Pushing image to registry
|
|
#
|
|
cd backend
|
|
for image in $(cat /tmp/images_to_build.txt);
|
|
do
|
|
echo "Bulding $image"
|
|
PUSH_IMAGE=1 bash -x ./build.sh skip $image
|
|
echo "::set-output name=image::$DOCKER_REPO/$image:$IMAGE_TAG"
|
|
done
|
|
|
|
- name: Creating old image input
|
|
env:
|
|
IMAGE_TAG: ${{ github.sha }}
|
|
run: |
|
|
#
|
|
# Create yaml with existing image tags
|
|
#
|
|
kubectl get pods -n app -o jsonpath="{.items[*].spec.containers[*].image}" |\
|
|
tr -s '[[:space:]]' '\n' | sort | uniq -c | grep '/foss/' | cut -d '/' -f3 > /tmp/image_tag.txt
|
|
|
|
echo > /tmp/image_override.yaml
|
|
|
|
for line in `cat /tmp/image_tag.txt`;
|
|
do
|
|
image_array=($(echo "$line" | tr ':' '\n'))
|
|
cat <<EOF >> /tmp/image_override.yaml
|
|
${image_array[0]}:
|
|
image:
|
|
tag: ${image_array[1]}
|
|
EOF
|
|
done
|
|
|
|
- name: Deploying to kuberntes
|
|
env:
|
|
IMAGE_TAG: ${{ github.sha }}
|
|
run: |
|
|
#
|
|
# Deploying image to environment.
|
|
#
|
|
cd scripts/helmcharts/
|
|
|
|
## Update secerts
|
|
sed -i "s/postgresqlPassword: \"changeMePassword\"/postgresqlPassword: \"${{ secrets.OSS_PG_PASSWORD }}\"/g" vars.yaml
|
|
sed -i "s/accessKey: \"changeMeMinioAccessKey\"/accessKey: \"${{ secrets.OSS_MINIO_ACCESS_KEY }}\"/g" vars.yaml
|
|
sed -i "s/secretKey: \"changeMeMinioPassword\"/secretKey: \"${{ secrets.OSS_MINIO_SECRET_KEY }}\"/g" vars.yaml
|
|
sed -i "s/jwt_secret: \"SetARandomStringHere\"/jwt_secret: \"${{ secrets.OSS_JWT_SECRET }}\"/g" vars.yaml
|
|
sed -i "s/domainName: \"\"/domainName: \"${{ secrets.OSS_DOMAIN_NAME }}\"/g" vars.yaml
|
|
## Update images
|
|
for image in $(cat /tmp/images_to_build.txt);
|
|
do
|
|
sed -i "/${image}/{n;n;s/.*/ tag: ${IMAGE_TAG}/}" /tmp/image_override.yaml
|
|
done
|
|
|
|
# Deploy command
|
|
helm upgrade --install openreplay -n app openreplay -f vars.yaml -f /tmp/image_override.yaml
|
|
|
|
# - name: Debug Job
|
|
# if: ${{ failure() }}
|
|
# uses: mxschmitt/action-tmate@v3
|
|
# env:
|
|
# DOCKER_REPO: ${{ secrets.OSS_REGISTRY_URL }}
|
|
# IMAGE_TAG: ${{ github.sha }}
|
|
# ENVIRONMENT: staging
|
|
#
|