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

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:
...

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)

github action connection refuse on migration step

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.

sh: 1: nest: Permission denied in GitHub Action

For some reason the build step for my NestJS project in my GitHub Action fails for a few days now. I use Turborepo with pnpm in a monorepo and try to run the build with turbo run build. This works flawlessly on my local machine, but somehow in GitHub it fails with sh: 1: nest: Permission denied. ELIFECYCLE  Command failed with exit code 126. I'm not sure how this is possible, since I couldn't find any meaningful change I made to the code in the meantime. It just stopped working unexpectedly. I actually think it is an issue with GH Actions, since it actually works in my local Docker build as well.
Has anyone else encountered this issue with NestJS in GH Actions?
This is my action yml:
name: Test, lint and build
on:
push:
jobs:
test-lint-build:
runs-on: ubuntu-latest
services:
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_HOST: localhost
POSTGRES_USER: test
POSTGRES_PASSWORD: docker
POSTGRES_DB: financing-database
ports:
# Maps tcp port 5432 on service container to the host
- 2345:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout
uses: actions/checkout#v3
- name: Install pnpm
uses: pnpm/action-setup#v2.2.2
with:
version: latest
- name: Install
run: pnpm i
- name: Lint
run: pnpm run lint
- name: Test
run: pnpm run test
- name: Build
run: pnpm run build
env:
VITE_SERVER_ENDPOINT: http://localhost:8000/api
- name: Test financing-server (e2e)
run: pnpm --filter #project/financing-server run test:e2e
I found out what was causing the problem. I was using node-linker = hoisted to mitigate some issues the pnpm way of linking modules was causing with my jest tests. Removing this from my project suddenly made the action work again.
I still don't know why this only broke the build recently, since I've had this option activated for some time now.

Remote postgres connection on circleci build to run laravel phpunit tests

We are using laravel 5.6, postgresql and circleci in our api production environment and still trying to implement some key unit tests to run before a commit is merged to master.
When trying to configure the remote postgresql database access on circle, there's the following problem:
Our .circleci/config.yml was supposed to pull a custom built image (edunicastro/docker:latest) and run phpunit tests in the "build" step
But we are getting the following error message:
PDOException: SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
The problem is this was supposed to connect to our remote database, but in our production environment we have the connection set up using .env and laravel.
I have tried copying the "DB_PGSQL_HOST" key to our config.yml but nothing changed, it kept trying to connect to 127.0.0.1.
Using the key "PGHOST" instead also had no effect.
This is the relevant, "build" part of our config.yml:
version: 2
jobs:
build:
docker:
- image: edunicastro/docker:latest
environment:
DB_PGSQL_CONNECTION: <prod_laravel_connection_name>
DB_PGSQL_HOST: <prod_db_host>
DB_PGSQL_PORT: 5432
DB_PGSQL_DATABASE: <prod_db_name>
working_directory: ~/repo
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "composer.json" }}
- v1-dependencies-
- run: composer install -n --prefer-dist
- run: ./vendor/bin/phpunit
- save_cache:
paths:
- ./vendor
key: v1-dependencies-{{ checksum "composer.json" }}
Okay, I was missing the command to copy the .env over, right under - checkout:
- checkout
- run: cp .env.test .env
Laravel was already configured and set to use it, so I didn't need to change anything else.

How to deploy laravel app on digitalocean after succesfull test pass on Gitalab CI

I have managed to succesfully run laravel test build on Gitlab CI using Gitlab Runner on digitaocean (With help from tutorial HOW TO: LARAVEL TESTING ON GITLAB CI WITH DOCKER)
Now I am wondering how I can deploy it after succesfull test.
This is my deploy process on my staging env:
cd MY_PROJECT_ROOT_DIR
git reset --hard HEAD
git checkout master
git pull
composer install
composer update
php artisan migrate
php artisan db:seed
How I can manage to include this deploy after test is done?
My configuration of GitLab Runner is the same as those files on this repo
This is the content of my .gitlab-ci.yml file:
before_script:
- bash .gitlab-ci.sh
variables:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: secret
phpunit:php-laravel-env:mysql:
image: woohuiren/php-laravel-env:latest
services:
- mysql:latest
script:
- php vendor/bin/phpunit --colors
How I should change this file in order to execute deploy script after test passed?
You need to use "stages"
Basically you would update your current test setup to include a stage.
stages:
- test
- deploy
phpunit:php-laravel-env:mysql:
stage: test
image: woohuiren/php-laravel-env:latest
services: ...
deploy_my_site:
stage: deploy
...
These stages will get run in sequence by GitLab but will stop if there is any error.
If you are using Forge you could use the deploy stage to trigger a script to curl the forge deploy hook.

Resources