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?
Related
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.
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
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
I'm struggling with this functionality of github action . I'm new with this and I'm using Laravel7.30 /php 7.4 and vue ... . I was thinking about a general code to do easy actions for basics necessities . I'm new and I only requires the normal workflow , Can you help me or provide a script ? please. I'm lost. I'd like that you guys could share me a good tutorial, I've read some tutorials but I'm still confused and help me please . thanks.
name: tests
on:
push:
branches:
- development
pull_request:
branches:
- development
schedule:
- cron: '0 0 * * *'
jobs:
tests:
runs-on: ubuntu-20.04
strategy:
fail-fast: true
matrix:
php: [7.4]
laravel: [^7.0]
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Setup PHP
uses: shivammathur/setup-php#v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
- name: Install dependencies
run: |
composer require "illuminate/contracts=${{ matrix.laravel }}" --no-update
composer update --prefer-dist --no-interaction --no-progress
- name: Generate Key
run: php artisan key:generate
- name: Execute tests
run: vendor/bin/phpunit --verbose
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');"
[...]