Job
To proceed with database migrations after deployment, a job is created in kubernetes that performs the relevant executions within the application pod.
The job is as follows:
warning
In order for the Job to be able to execute commands within the pod it must have a RoleBinding associated to the argoCD project service account. It should look something like the following depending on the namespace where you want to place it:
kubectl create rolebinding default-admin --clusterrole=admin --serviceaccount=osdo:default -n osdo
This will allow the service account to access the pod and execute the job commands.
apiVersion: batch/v1
kind: Job
metadata:
name: osdoapp-post-hook-job
namespace: osdo
annotations:
argocd.argoproj.io/hook: PostSync
argocd.argoproj.io/hook-delete-policy: HookSucceeded
spec:
template:
spec:
containers:
- name: kubectl-container
image: bitnami/kubectl:latest # kubectl image
command:
- "/bin/sh"
- "-c"
args:
- |
pods=$$(kubectl get pods -n osdo -o=name)
pod_name=$$(echo "$pods" | grep "osdoapp-deployment" | head -n 1 | cut -d'/' -f 2)
if [ ! -z "$pod_name" ]; then
kubectl exec -n osdo $pod_name -- php artisan migrate --force
kubectl exec -n osdo $pod_name -- php artisan db:seed --force
else
echo "The desired pod was not found."
fi
restartPolicy: Never
backoffLimit: 2 # Number of times to try to execute the job in case of failure