Home > Net >  What is the right way to source .sh file after auto deployment to azure linux webapp?
What is the right way to source .sh file after auto deployment to azure linux webapp?

Time:11-17

I have a laravel application on my azure webapp (linux). I am trying to run some php commands after deploying.

I am using Local Git to deploy to my azure's webapp.

and as per this guide, i set the "POST_BUILD_SCRIPT_PATH" to run a script named "postbuild.sh"

(Note that i am using a .sh file since i am in a Linux based webapp. )

The problem is my postbuild.sh location is in "wwwroot" (where my laravel root application is). but i keep getting the

Could not open input file: artisan

message when i try to run any artisan command like

php artisan cache:clear

After looking up, i found that the script is being executed from a tmp location.

and i should use sourcing for running the script in the right location but it is still doesn't work.

so i created another .sh file in 'wwwroot' as well named 'clearcache.sh' and tried to source it in 'postbuild.sh' but still not working.

My postbuild.sh is

echo "Post Build .sh"
echo "Script executed from: ${PWD}"
BASEDIR=$(dirname $0)
echo "Script location: ${BASEDIR}"
echo "===================="
source ./home/site/wwwroot/refresh.sh

My refresh.sh is

echo "Refresh script"
echo "Script executed from: ${PWD}"
BASEDIR=$(dirname $0)
echo "Script location: ${BASEDIR}"
php artisan cache:clear

and the errors i am having when pushing to azure repo is:

remote: Detecting platforms...
remote: Detected following platforms:
remote:   php: 7.4.24
remote:
remote: Using intermediate directory '/tmp/8d9a71074a40f5b'.
remote:
remote: Copying files to the intermediate directory...
remote: Done in 0 sec(s).
remote: /home/site/wwwroot/postbuild.sh: line 6: ./home/site/wwwroot/refresh.sh
remote:
remote: : No such file or directory
remote: Source directory     : /tmp/8d9a71074a40f5b
remote: Destination directory: /home/site/wwwroot
remote:
remote: Executing pre-build command...
remote: Pre Build
remote: Finished executing pre-build command.
remote: PHP executable: /tmp/oryx/platforms/php/7.4.24/bin/php
remote: No 'composer.json' file found; not running 'composer install'.
remote:
remote: Executing post-build command...
remote: Post Build .sh
remote: Script executed from: /tmp/8d9a71074a40f5b
remote: Script location: /home/site/wwwroot
remote:
remote: ====================

Please note the following:

  • Laravel root directory doesn't contain 'index.php' file, but i added a dummy 'index.php' file anyway because it was needed by Azure to validate the build.
  • I removed the 'composer.json' file from the local git because i didn't need to run composer install. i just wanted to copy app & routes folder from the repo folder to 'wwwroot'

CodePudding user response:

You never actually switch to the BASEDIR by doing e.g. cd $BASEDIR so your working directory never actually changes so here's one way around this:

echo "Refresh script"
echo "Script executed from: ${PWD}"
BASEDIR=$(dirname $0)
echo "Script location: ${BASEDIR}"
$(
   cd $BASEDIR
   php artisan cache:clear
)

Note the multiline $(...) is to execute the contents but preserve the original working directory when complete.

Alternatively you can do:

echo "Refresh script"
echo "Script executed from: ${PWD}"
BASEDIR=$(dirname $0)
echo "Script location: ${BASEDIR}"
php $BASEDIR/artisan cache:clear

CodePudding user response:

I achieved what i was looking for using a different approach.

I used the Custom Deployment Script. (as per this link)

The Custom Deployment let you totally handle the deployment process yourself. So i wrote a script to copy the files from the repo to the wwwroot.

But i faced another issue which is the php-cli that the kudu runs doesn't include any database modules as per this issue i found at the kudu's repo.

So, i came with a solution to run my artisan commands(which are mainly cache refreshing) programtically using Laravel Controller. and curl HTTP Request to call this link to execute the artisan commands using this controller.

Not the ideal solution. but it works

Here is my .deployment file

[config]
command = ./deploy.sh

and here is my deploy.sh

rm -rf $DEPLOYMENT_TARGET/app
rm -rf $DEPLOYMENT_TARGET/resources
rm -rf $DEPLOYMENT_TARGET/routes
rm -rf $DEPLOYMENT_TARGET/public/*.js
rm -rf $DEPLOYMENT_TARGET/public/*.svg
rm -rf $DEPLOYMENT_TARGET/public/*.eot
rm -rf $DEPLOYMENT_TARGET/public/*.css
rm -rf $DEPLOYMENT_TARGET/public/*.ico
rm -rf $DEPLOYMENT_TARGET/public/*.woff2
rm -rf $DEPLOYMENT_TARGET/public/*.woff
rm -rf $DEPLOYMENT_TARGET/public/*.ttf
\cp -RTf  $DEPLOYMENT_SOURCE $DEPLOYMENT_TARGET
rm -rf $DEPLOYMENT_SOURCE/app
rm -rf $DEPLOYMENT_SOURCE/resources
rm -rf $DEPLOYMENT_SOURCE/routes
rm -rf $DEPLOYMENT_SOURCE/public
php /home/site/wwwroot/refresh-app.php

You can call the link to refresh the artisan commands inside refresh-app.php using curl or any other favorite way.

  • Related