<p>I want its a good practice to use the folowing script to clone code from remote git repo and install dependencies :</p>
<pre><code>    #!/usr/bin/env bash

    set -e

    WORKING_DIR=&quot;where to clone code&quot;
    RELEASE_BRANCH=&quot;develop&quot; # can be configured in configMap and mounted as volume in the pod
    GIT_REPOSITORY=&quot;git@github.com:xxxx/xxxx.git&quot;

    if \[ ! -d &quot;${WORKING_DIR}.git&quot; \]; then
    git clone $GIT_REPOSITORY $WORKING_DIR
    fi

    cd $WORKING_DIR

    git fetch --all || { echo &quot;Error: git fetch failed !&quot;; exit 1; }
    git checkout $RELEASE_BRANCH || { echo &quot;Error: git checkout failed !&quot;; exit 1; }
    git pull || { echo &quot;Error: git pull failed !&quot;; exit 1; }

    # additional scripts here like composer install / clear cache
</code></pre>
<p>Is this solution can affect my pod (restartPolicy) ? how can i check that all my code and all command are executed and not skinping or silent ?</p>
<p>Code tested with simple php project</p>

Jozeph B.
<p>No, this isn't best practice in a production setting.</p>
<p>Instead of running scripts to clone code and install dependencies on-the-fly inside the Kubernetes pod container, you should build a docker image with the cloned code and its dependencies pre-installed. You can simply just have your Kubernetes Deployment run the image with no additional scripts required.</p>
<p>Below will build code into Docker image and push to example AWS Registry (ECR).</p>
<pre><code>#!/usr/bin/env bash

set -e

WORKING_DIR=&quot;where_to_clone_code&quot;
RELEASE_BRANCH=&quot;develop&quot; # can be configured in configMap and mounted as volume in the pod
GIT_REPOSITORY=&quot;git@github.com:xxxx/xxxx.git&quot;
DOCKER_IMAGE_NAME=&quot;your-image-name&quot;
DOCKER_IMAGE_TAG=&quot;your-image-tag&quot;
ECR_REPO_URI=&quot;your-ecr-repo-uri&quot;

if [ ! -d &quot;${WORKING_DIR}/.git&quot; ]; then
  git clone $GIT_REPOSITORY $WORKING_DIR
fi

cd $WORKING_DIR

git fetch --all || { echo &quot;Error: git fetch failed !&quot;; exit 1; }
git checkout $RELEASE_BRANCH || { echo &quot;Error: git checkout failed !&quot;; exit 1; }
git pull || { echo &quot;Error: git pull failed !&quot;; exit 1; }

# Additional scripts here like composer install / clear cache

# Build Docker image
docker build -t $DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG .

# Authenticate Docker with ECR
$(aws ecr get-login --no-include-email --region your-region)

# Push Docker image to ECR
docker tag $DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG $ECR_REPO_URI:$DOCKER_IMAGE_TAG
docker push $ECR_REPO_URI:$DOCKER_IMAGE_TAG
</code></pre>
<p>Your original script itself won't directly affect the <code>restartPolicy</code> of your Kubernetes pod. However, if the commands within the script fail, it might impact the overall behavior of the application running in the pod, which could lead to the pod being restarted depending on your application's behavior and Kubernetes configuration.</p>
<p>Your script uses set -e which will cause the script to exit immediately if any command exits with a non-zero status. While this can be helpful to catch errors, it might also lead to unexpected behavior if not all errors are properly handled.</p>
