Default .yml file from GitHub from does not work - laravel

I'm trying to run phpunit tests through GitHub actions.
I use .yml file that GitHub creates by default for Laravel projects but it does not seem worked.
Here it is (looks good):
name: Laravel
on:
push:
branches: [ master ]
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Setup
uses: shivammathur/setup-php#15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.1'
- name: Copy.env
run: |
php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
But when I do git push I got:
Any ideas? Thanks

I found a solution.
I compared my script with the default one by GitHub and found that somehow I missed 1 step: uses: actions/checkout#v2.
That's why the script could not find .env.example file in the directory.
Now everything works fine.

Related

How to work with Github Actions without .env file

I'm trying to perform deploy Laravel app to AWS Elastic Beanstalk with Github Actions but I had questions on running Github actions because currently I'm try to avoid upload .env file to my repository.
Below provided the default workflows which generated by Github
name: Laravel
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
laravel-tests:
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php#15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.0'
- uses: actions/checkout#v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
The generated file showing error
There was 1 failure:
1) Tests\Feature\ExampleTest::test_the_application_returns_a_successful_response
Expected response status code [200] but received 500.
Failed asserting that 200 is identical to 500.
The following exception occurred during the last request:
PDOException: SQLSTATE[HY000]: General error: 1 no such table: users in /home/runner/work/sampleSearch/sampleSearch/vendor/laravel/framework/src/Illuminate/Database/Connection.php:414
So may I know how can I solve this connection issues without upload .env file?

Deploy my laravel project to shared hosting with github actions

please I added GitHub action main.yml to my Laravel project to the shared hosting for automated deployment. The deployment worked okay but the project was placed outside public_html because of the path I specified - server_dir: /home5/app/public_html/, I want the project files to be put into the public_html folder. Below is the code:
name: Glory Deploy Production
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
web-deploy:
name: Deploying...
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php#15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.0'
- uses: actions/checkout#v2.3.2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer update --ignore-platform-reqs
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: 📂 Sync files
uses: SamKirkland/FTP-Deploy-Action#4.0.0
with:
server: ${{ secrets.server }}
username: ${{ secrets.username}}
password: ${{ secrets.password}}
server_dir: /home5/app/public_html/
Please can I modify the path pointing to the public_html? How?
I am considering your ftp user is a default user that has root path
server_dir: "public_html/"
If your user not has the root path you must consider its path until public_html

How do I prevent GitHub actions from replacing some files

I want to do a simple CD/CD to do automatic deployments for my Laravel project but turns out my .env file is always replaced. How do I make sure it's not always replaced
Here is my action file
name: Laravel
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
laravel-tests:
runs-on: self-hosted
steps:
- uses: shivammathur/setup-php#b7d1d9c9a92d8d8463ce36d7f60da34d461724f8
with:
php-version: '7.4'
- uses: actions/checkout#v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
Each time your workflow run you will get a new machine. Thus there is no files you created them on previous run. As this if you need to have some file created at runtime you need to repeat this step each time.
Please check it here to understand better github ations basics.
You don't have much options to share this file accros run, as keeping secrets in artifacts is bad choice. So you need to recreate this file each time you need it based on the secret which you may keep in secrets context. Please check this link:
- name: Create env file
run: |
cat << EOF >> .env
API_ENDPOINT="https://xxx.execute-api.us-west-2.amazonaws.com"
API_KEY=${{ secrets.API_KEY }}
EOF
The github action works on the files on your git repo.
Your .env is not and should not be in the repo, because it contains your credentials/secrets.
You may have it on your local computer, but it's included in the gitignore so git (an github as a consequence) doesn't track it.
As a consequence no, you don't have the .env file at each action run. The most straightforward way to do it is:
create a .env.production file that is gitted and committed. Place there your main .env variables that aren't secrets/sensitive, such as:
APP_NAME=YourAppName
APP_ENV=production
APP_DEBUG=false
APP_LOG_LEVEL=warning
APP_URL=https://your-url.com
CACHE_DRIVER=redis
SESSION_DRIVER=file
QUEUE_CONNECTION=redis
... etc ...
DO NOT INCLUDE KEYS, PASSWORDS OR SECRETS THOUGH..
Now copy that file as your default .env file in one of your steps:
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.production', '.env');"
Now it's time to handle your secrets. You should add them as github secret of your repo, and included using the env directive of your github actions:
env:
APP_KEY: ${{ secrets.APP_KEY }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
....
This last step may need to vary a bit depending on how/where you do the actual deploy, but that should be the gist of it: you pull them from the github secrets space and you add them to the production environment

Github workflow to deploy a Laravel application

Git\Github beginner here.
After much googling, I managed to make the following Github-action\workflow to deploy a Laravel app on a live testing server.
I just basically need the workflow to update the app files on the testing live server with the latest versions in the repository.
The code seems to work fine, but there are a couple of lines I don't fully understand.
1) What is the purpose of the Checkout step (the one using actions/checkout#v2) ?
2) What is the purpose of "git checkout -f" in the deployment script ? Is it needed ?
3) Is there something I'm missing \ should add in the deployment script ?
4) Another thing I'd like to do, would be to "copy" the code that is being pushed to the live server into another repository branch, to keep track of what is currently on the live server. How would I do that in this same action ? (If that makes sense and\or it is even possible)
name: DEV Deploy
on:
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Deployment
uses: appleboy/ssh-action#master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
script: |
cd /home/app/public_html
php artisan down
git checkout -f
git branch -u origin/dev
git pull
composer install --optimize-autoloader --no-dev
npm install
npm update
npm run production
php artisan cache:clear
php artisan view:clear
php artisan config:cache
php artisan up
Thanks.

How do you set the PHP version with this GitHub Actions workflow?

I have been using a GitHub Actions workflow for a while now:
name: Laravel
on: [push]
jobs:
laravel-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: |
rm -rf vendor
composer install
- name: Generate key
run: php artisan key:generate
- name: Link Storage
run: php artisan storage:link
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit --stop-on-failure
It worked until 8.0 came out, then composer exploded with a bunch of errors that this and that package, which I update regularly, are not compatible with 8.0.
Anywho, I need to set this to use 7.4 for now.
I understand the:
with:
php-version: '7.4'
and I know I can use a matrix, but where am I calling the whole "use this php version" with in this script to make the tests run against 7.4?
Use the setup-php action:
name: Laravel
on: [push]
jobs:
laravel-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Setup PHP
uses: shivammathur/setup-php#v2
with:
php-version: 7.4
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
[...]

Resources