Sign inSign up

sinkcup/laravel-demo

By sinkcup

Updated over 5 years ago

Image
0

1.3K

sinkcup/laravel-demo repository overview

Laravel 6 Demo

CircleCI Docker Cloud Build Status codecov coverage SwaggerHub

CODING CI(Jenkins)构建状态 CODING 测试覆盖率

This project provides CI, Docker, Lint, Tests for Laravel.

Read this in these languages: English, 汉字.

Docker

This project build two Dockers for production and development.

build steps:

# default production
docker build -t laravel-demo:6 .

# development
docker build -t laravel-demo:6-dev --build-arg APP_ENV=local .

# speed up docker build by change apt, composer, npm mirror
docker build -t laravel-demo:6-dev --build-arg APP_ENV=local --build-arg SPEED=up .

build result on hub.docker.com:

docker pull sinkcup/laravel-demo:6

It has 3 roles, you can switch by CONTAINER_ROLE: app/scheduler/queue:

# default web app
docker run --rm --name laravel_demo -e DB_CONNECTION=sqlite -t sinkcup/laravel-demo:6

# scheduler(cron)
docker run --rm --name laravel_demo_scheduler -e DB_CONNECTION=sqlite -e CONTAINER_ROLE=scheduler -t sinkcup/laravel-demo:6

# queue
docker run --rm --name laravel_demo_queue -e DB_CONNECTION=sqlite -e CONTAINER_ROLE=queue -t sinkcup/laravel-demo:6

docker run

detail:

  • Laravel default log config is saving to file, it's wrong in Docker, should output to stdout, so I have changed config/logging.php.
  • Laravel default routes can't be cached, it has error: "Unable to prepare route [/] for serialization. Uses Closure.", so I have changed routes/web.php and routes/api.php.
  • Docker can run cron in the foreground, should never run it in background.

IDE Helper

ide-helper is only required in dev, how to run composer script only in dev?

If you add script to run it in composer.json, it will crash when to deploy using composer install --optimize-autoloader --no-dev.

the best way is:

"scripts":{
    "post-autoload-dump": [
        "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
        "@php artisan package:discover --ansi"
        "if (php artisan | grep 'ide-helper:generate'); then php artisan ide-helper:generate; fi",
        "if (php artisan | grep 'ide-helper:meta'); then php artisan ide-helper:meta; fi"
    ]
},

Lint

This project use PSR12 coding standard, you can find rules in phpcs.xml.

When you run composer install, the .git-pre-commit will be copyed to .git/hooks/pre-commit, so it will check locally when you commit codes.

If there are some format errors, you could try to fix automatically:

./lint.sh --fix

You can find lint.sh run in .circleci/config.yml and Jenkinsfile, so it will check coding standard when codes pushed or a PR created.

OpenAPI 3 Docs(Swagger)

This project generate OpenAPI 3 Docs from code.

local:

php artisan serve
google-chrome http://127.0.0.1:8000/api/documentation

online(swaggerhub.com):

php artisan l5-swagger:generate
curl -X POST "https://api.swaggerhub.com/apis/sinkcup/laravel-demo?openapi=3.0.0" \
     -H "Authorization: your-api-key" \
     -H "Content-Type: application/json" \
     --data-binary @storage/api-docs/api-docs.json
google-chrome https://app.swaggerhub.com/apis-docs/sinkcup/laravel-demo/1.0.0

docs: https://swagger.io/blog/api-development/integrating-with-the-swaggerhub-api/

Tests

When you run ./phpunit.sh, it will generate:

  • clover.xml: coverage report in Clover XML format, you can register for a TOKEN on codecov.io, so CI can upload the report to it.
  • storage/app/public/coverage: coverage report in HTML format, you can access by http://localhost:8000/storage/coverage/
  • junit.xml: test execution in JUnit XML format, it can be collected by Jenkins.

Continuous integration(CI)

CircleCI

Environment Variables:

NameValue
CODECOV_TOKENoptional, get from codecov.io

CircleCI Environment Variables

Jenkins(CODING.net free CI)

Environment Variables:

NameValue
DOCKER_USERoptional, Docker username, set this if you want to use private docker image
DOCKER_PASSWORDoptional, Docker password
DOCKER_SERVERoptional, Docker server
DOCKER_PATH_PREFIXoptional, Docker path prefix
SPEEDoptional, change mirror to speed up docker build, values: up/down/keep

CODING CI Environment Variables

Tag summary

Content type

Image

Digest

Size

189.8 MB

Last updated

over 5 years ago

docker pull sinkcup/laravel-demo