github action connection refuse on migration step - laravel

below is my github action and it fails on the "php artisan migrate" part. The target environment is ubuntu.
on:
push:
branches:
- master
name: 🚀 Deploy website on push
jobs:
deploy_job:
runs-on: ubuntu-latest
name: deploy
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Deploy file
uses: wlixcc/SFTP-Deploy-Action#v1.2.4
with:
username: ${{ secrets.FTP_USERNAME }}
server: ${{ secrets.FTP_SERVER }}
port: ${{ secrets.FTP_PORT }}
remote_path: '/var/www/megabig.com'
sftp_only: true
password: ${{ secrets.FTP_PASSWORD }}
- name: Run Composer
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
- name: Run Migration
env:
DB_CONNECTION: mysql
DB_HOST: 127.0.0.1
DB_PORT: 3306
DB_DATABASE: ${{ secrets.DB_DATABASE }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
run: php artisan migrate --force
- name: Run Optimize
run: php artisan optimize --force
The step returns
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = *** and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
708â–• // If an exception occurs when attempting to run a query, we'll format the error
709â–• // message to include the bindings with SQL, which will make this exception a
710â–• // lot more helpful to the developer instead of just the database's errors.
711â–• catch (Exception $e) {
➜ 712▕ throw new QueryException(
713â–• $query, $this->prepareBindings($bindings), $e
714â–• );
715â–• }
716â–• }
+36 vendor frames
37 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
tried to specify the env on the yml and made sure .env on server has correct value but to no avail, migration is not working at all. Any help, suggestions, recommendations is greatly appreciated. Thank you in advance.

In your workflow, you're using the default preinstalled MySQL local instance but there's no step to start it because according to docs (MySQL):
MySQL service is disabled by default.
Use the following command as a part of your job to start the service:
'sudo systemctl start mysql.service'
So, you need to run:
sudo systemctl start mysql.service
to start the local instance before using it.

Related

PHP Fatal error on CI/CD run php artisan test

I use docker-compose with laravel and postgresql and all works fine in local system. The problem is in the CI/CD.
I have changed the CI/CD yml file over and over but I am stuck!
CI/CD
name: CI/CD
on:
pull_request:
branches: ['master']
push:
branches: ['master']
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php#v2
with:
php-version: '7.4'
- uses: actions/checkout#v2
- name: Run Containers
run: docker-compose -f docker-compose.yml -f docker-compose.dev.yml up -d
# - name: Run composer install
# run: cd companyname_app_dir && composer install
# - name: Run composer update
# run: cd companyname_app_dir&& composer update
# - name: Setup Project
# run: |
# cd companyname_app_dir
# composer update
# composer install
# php artisan config:clear
# php artisan cache:clear
- name: Run test
run: cd companyname_app_dir && php artisan test
env:
APP_KEY: base64:x06N/IsV5iJ+R6TKlr6sC6Mr4riGgl8Rg09XHHnRZQw=
APP_ENV: testing
DB_CONNECTION: companyname-postgres
DB_DATABASE: db_test
DB_USERNAME: root
DB_PASSWORD: 1234
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action#v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v2
- name: Login to Docker Hub
uses: docker/login-action#v2
with:
username: secret
password: secret
- name: Build and push
uses: docker/build-push-action#v3
with:
push: true
file: ./companyname_app_dir/Dockerfile
tags: company_image:latest
build-args: |
"NODE_ENV=production"
There are line comments, I tried using these but I couldn't run a test successfully.
docker-compose
version: '3'
networks:
companyname_network:
driver: bridge
services:
nginx:
image: nginx:stable-alpine
container_name: companyname-nginx
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
restart: always
depends_on:
- companyname_app
networks:
- companyname_network
companyname_app:
restart: 'always'
image: 'companyname_laravel'
container_name: companyname-app
build:
context: .
dockerfile: ./Dockerfile
networks:
- companyname_network
depends_on:
- companyname_db
companyname_db:
image: 'companyname_multiple_db'
container_name: companyname-postgres
build:
context: .
dockerfile: ./DockerfileDB
restart: 'always'
volumes:
- local_pgdata:/docker-entrypoint-initdb.d
environment:
- POSTGRES_MULTIPLE_DATABASES=db,db_test
- POSTGRES_USER=root
- POSTGRES_PASSWORD=1234
ports:
- 15432:5432
networks:
- companyname_network
companyname_dbadmin:
image: adminer
container_name: companyname-dbadmin
restart: 'always'
depends_on:
- companyname_db
ports:
- 5051:8080
networks:
- companyname_network
volumes:
local_pgdata:
docker-compose.dev
version: '3'
services:
nginx:
ports:
- 9000:80
companyname_app:
build:
args:
- NODE_ENV=development
volumes:
- ./companyname_app_dir:/app
- /app/vendor
With this file, I get an error:
Run cd companyname_app_dir && php artisan test
PHP Warning: require(/home/runner/work/companyname_app/companyname_app /companyname_app_dir/vendor/autoload.php): failed to open stream: No such file or directory in /home/runner/work/companyname_app/companyname_app/companyname_app_dir/artisan on line 18
PHP Fatal error: require(): Failed opening required '/home/runner/work/companyname_app/companyname_app/companyname_app_dir/vendor/autoload.php' (include_path='.:/usr/share/php') in /home/runner/work/companyname_app/companyname_app/companyname_app_dir/artisan on line 18
Error: Process completed with exit code 255.
if I use:
- name: Run composer install
run: cd companyname_app_dir && composer install
- name: Run composer update
run: cd companyname_app_dir && composer update
In CI/CD yml and remove Run Containers part, composer install and update successfully, but php artisan test throws this error:
postgresql can not connect
You must use composer install, else you will have no vendor folder at all, so you have nothing to run. That is why you are getting an error if you don't run composer install
You should not run composer update, because you are updating packages to new versions, you never do that in production, you just run composer install --no-dev
You are mixing running docker with a command OUTSIDE the docker container.
Related to point 3., if you are using docker-compose, you cannot execute:
- name: Run test
run: cd companyname_app_dir && php artisan test
env:
APP_KEY: base64:x06N/IsV5iJ+R6TKlr6sC6Mr4riGgl8Rg09XHHnRZQw=
APP_ENV: testing
DB_CONNECTION: companyname-postgres
DB_DATABASE: db_test
DB_USERNAME: root
DB_PASSWORD: 1234
Because you are outside docker, so you should execute docker-compose exec companyname_app php artisan test, that will execute the tests INSIDE the docker container, where you correctly have everything setup.
So your code (if I am not missing anything) should be:
- name: Run test
run: docker-compose exec companyname_app php artisan test
env:
APP_KEY: base64:x06N/IsV5iJ+R6TKlr6sC6Mr4riGgl8Rg09XHHnRZQw=
APP_ENV: testing
DB_CONNECTION: companyname-postgres
DB_DATABASE: db_test
DB_USERNAME: root
DB_PASSWORD: 1234
But I am not certaing what will you get back from that execution, I have no idea if the test fails, if the CI/CD (I am assuming you are using GitHub Actions or Bitbucket Pipelines), will truly identify that it has failed or not.
What I usually do, is just install everything on the machine (CI/CD machine), instead of using a docker file or docker-compose yaml. But that is my preference (at least for PHP/Laravel)

Getting a SQLSTATE[HY000] [2002] Connection refused error in github action?

This is what my actions look like, when I get to the point of php artisan test I get the error:
name: Continuous Integration
on:
push
jobs:
laravel-tests:
runs-on: ubuntu-20.04
- name: create migration
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: php artisan migrate --seed
- name: run tests
run: php artisan test
According to the documentation on steps:
Each step runs in its own process in the runner environment and has access to the workspace and filesystem. Because steps run in their own process, changes to environment variables are not preserved between steps.
So you'll need to provide the environment variables for the entire workflow, as described in the documentation for environment variables and the jobs.<job_id>.env keyword:
name: Continuous Integration
on:
push
jobs:
laravel-tests:
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
runs-on: ubuntu-20.04
steps:
...

Can someone look at my yaml file for code deployment using Bitbucket Pipelines?

This is my first attempt at setting up pipelines or even using any CI/CD tool. So, reading the documentation at Bitbucket, I added the bitbucket-pipelines.yml file in the root of my Laravel application for a build. Here is the file.
image: php:7.4-fpm
pipelines:
default:
- step:
name: Build and test
caches:
- composer
script:
- apt-get update && apt-get install -qy git curl libmcrypt-dev mariadb-client ghostscript
- yes | pecl install mcrypt-1.0.3
- docker-php-ext-install pdo_mysql bcmath exif
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --file name=composer
- composer install
- ln -f -s .env.pipelines .env
- php artisan migrate
- ./vendor/bin/phpunit
services:
- mysql
- redis
definitions:
services:
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: "laravel-pipeline"
MYSQL_RANDOM_ROOT_PASSWORD: "yes"
MYSQL_USER: "homestead"
MYSQL_PASSWORD: "secret"
redis:
image: redis
The above works fine in building the application, running tests,etc. But when I add the below to deploy, using the scp pipe, I get a notice saying either I need to include an image or at times the notice says there is a bad indentation of a mapping entry.
- step:
name: Deploy to test
deployment: test
# trigger: manual # Uncomment to make this a manual deployment.
script:
- pipe: atlassian/scp-deploy:0.3.13
variables:
USER: '${remoteUser}'
SERVER: '${server}'
REMOTE_PATH: '${remote}'
LOCAL_PATH: '${BITBUCKET_CLONE_DIR}/*'
I don't really know yaml, and this is my first time working with a CI/CD tool so I am lost. Can someone guide me in what I am doing wrong?
Your indentation for name and deployment is not the same as for the script. Try putting it all on the same indentation like this.
- step:
name: Deploy to test
deployment: test
script:
- pipe: atlassian/scp-deploy:0.3.13
variables:
USER: '${remoteUser}'
SERVER: '${server}'
REMOTE_PATH: '${remote}'
LOCAL_PATH: '${BITBUCKET_CLONE_DIR}/*'

Bitbucket Pipeline Laravel with MySQL php_network_getaddresses

I use php7.2-fpm-stretch docker image and MySQL as an attached service. Following command runs successfully:
mysql -h 127.0.0.1 -u username -ppassword
However, when composer wants to run package:discover it ends up with the following error:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
The .env file has the following configuration for the database:
DB_HOST=127.0.0.1
DB_CONNECTION=127.0.0.1
DB_DATABASE=pipeline
DB_USERNAME=username
DB_PASSWORD=password
And my yml file is as below:
image: php:7.2-fpm-stretch
pipelines:
default:
- step:
caches:
- composer
script:
- apt-get update && apt-get install -qy git unzip mysql-client
#Some other non-related configuration
- composer install
- php artisan key:generate
services:
- mysql
definitions:
services:
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: 'pipeline'
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
MYSQL_USER: 'username'
MYSQL_PASSWORD: 'password'

Running dusk after php artisan serve fails in CircleCI

I'm working on a CircleCi config file that runs unit tests and browser (dusk) tests. Tests fail in CircleCi every time on the command php artisan dusk. Php artisan serve also is 'canceled' if I add the attribute background: true.
Declaring the port php artisan serve --port=8000
Using backgroud: true and php artisan serve &
Tried running curl http://localhost:8000 (failed)
circleci config file
version: 2
jobs:
Test:
docker:
- image: circleci/php:7.2-fpm-node-browsers
- image: circleci/redis:5.0
- image: circleci/mysql:5.7
environment:
MYSQL_ROOT_HOST: '%'
MYSQL_ROOT_PASSWORD: ''
MYSQL_USER: homestead
MYSQL_PASSWORD: secret
MYSQL_DATABASE: homestead
MYSQL_ALLOW_EMPTY_PASSWORD: true
environment:
APP_ENV: testing
APP_URL: http://localhost:8000
APP_KEY: ###
DB_HOST: 127.0.0.1
DB_DATABASE: homestead
DB_USERNAME: homestead
DB_PASSWORD: secret
REDIS_HOST: 127.0.0.1
REDIS_PASSWORD: 'null'
PUSHER_APP_ID: ###
PUSHER_APP_KEY: ###
PUSHER_APP_SECRET: ###
PUSHER_APP_CLUSTER: ###
MIX_PUSHER_APP_KEY: ###
MIX_PUSHER_APP_CLUSTER: ###
working_directory: ~/workspace
steps:
- checkout
- run:
name: Prepare Environment
command: .circleci/kickstart.sh
- restore_cache:
keys:
- composer-v1-{{ checksum "composer.lock" }}
- composer-v1-
- run: composer install -n --prefer-dist
- save_cache:
key: composer-v1-{{ checksum "composer.lock" }}
paths:
- vendor
- restore_cache:
keys:
- node-v1-{{ checksum "package-lock.json" }}
- node-v1-
- run: npm install
- save_cache:
key: node-v1-{{ checksum "package-lock.json" }}
paths:
- node_modules
- run:
name: Build Artifacts
command: npm run dev
- run:
name: Waiting for MySQL to be ready
command: dockerize -wait tcp://127.0.0.1:3306 -timeout 120s
- run: php artisan config:clear
- run: php artisan config:cache
- run: php artisan migrate:refresh --seed --database=mysql --force
- run: php artisan dusk:install
- run:
name: Run PHP Unit Tests
command: ./vendor/bin/phpunit
- run:
name: Run server
command: php artisan serve --port=8000 &
# - run:
# name: Run server
# command: php artisan serve --port=8000
# background: true
- run:
name: Test connection to server
command: curl http://localhost:8000
- run:
name: Run E2E Tests
command: php artisan dusk
environment:
APP_URL: http://localhost:8000
- store_artifacts:
path: ./tests/Browser/console
destination: console
- store_artifacts:
path: ./tests/Browser/screenshots
destination: screenshots
workflows:
version: 2
Build and Test:
jobs:
- Test
curl failing
#!/bin/bash -eo pipefail
php artisan serve --port=8000 &
curl http://localhost:8000
curl: (7) Failed to connect to localhost port 8000: Connection refused
Exited with code 7
Dusk should run after the server is running

Resources