Skip to content

Commit 451e199

Browse files
committed
🚀 Adding image bundle script
0 parents  commit 451e199

7 files changed

+239
-0
lines changed

.gitignore

Whitespace-only changes.

README.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Deployer
2+
3+
A Laravel package to automate Docker image bundle creation for your projects.
4+
5+
## Installation
6+
7+
Install the package via Composer:
8+
9+
```bash
10+
composer require inquid/laravel-deployer
11+
```
12+
13+
## Usage
14+
You can use the script in multiple ways:
15+
16+
1. **Laravel Artisan Command**
17+
18+
Run the command from your Laravel project's root:
19+
20+
```bash
21+
php artisan deployer:create-image-bundle {project_id} {dockerfile?} {repo_url?} {branch_name?}
22+
```
23+
24+
- `project_id`: Required.
25+
- `dockerfile`: Optional.
26+
- `repo_url`: Optional.
27+
- `branch_name`: Optional.
28+
29+
*Note*: Default values for optional arguments are managed within the script, so you only need to provide them if you want to override the defaults.
30+
31+
2. **Via Vendor Bin**
32+
33+
Execute the script from vendor/bin:
34+
35+
```bash
36+
vendor/bin/create-image-bundle {project_id} {dockerfile?} {repo_url?} {branch_name?}
37+
```
38+
39+
3. **Directly from the Script**
40+
41+
Run the script directly:
42+
43+
```bash
44+
vendor/inquid/laravel-deployer/scripts/create-image-bundle.sh {project_id} {dockerfile?} {repo_url?} {branch_name?}
45+
```
46+
47+
### Default Values
48+
49+
- **Dockerfile**: 8.3.Dockerfile
50+
- **Repository URL**: https://github.com/gogl92/docker-lemp
51+
- **Branch Name**: deployer
52+
53+
These defaults are defined in the script itself to ensure there is only one source of truth for default values.
54+
55+
## Requirements
56+
57+
- PHP: >=7.4
58+
- Laravel: 8.x, 9.x, or 10.x
59+
- Docker: Installed and running
60+
- Git: Installed
61+
- Rsync: Installed
62+
63+
## Important Notes
64+
- Environment File: Ensure that you have a .env.staging file in your project's root directory.
65+
The script copies this file to .env during execution.
66+
This file should contain the necessary environment configurations for your application.
67+
68+
## License
69+
70+
This package is open-sourced software licensed under the MIT license.
71+
72+
## Author
73+
74+
Inquid SAS de CV
75+

bin/create-image-bundle

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
4+
"$DIR/scripts/create-image-bundle.sh" "$@"
5+

composer.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "inquid/laravel-deployer",
3+
"description": "A Laravel package to automate Docker image creation for projects.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Inquid SAS de CV",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": ">=7.4",
14+
"illuminate/support": "^8.0|^9.0|^10.0"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"Inquid\\Deployer\\": "src/"
19+
}
20+
},
21+
"extra": {
22+
"laravel": {
23+
"providers": [
24+
"Inquid\\Deployer\\DeployerServiceProvider"
25+
]
26+
}
27+
},
28+
"bin": [
29+
"bin/create-image-bundle"
30+
]
31+
}

scripts/create-image-bundle.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
# Assign arguments
4+
project_id="$1"
5+
dockerfile="${2:-8.3.Dockerfile}"
6+
repo_url="${3:-https://github.com/gogl92/docker-lemp}"
7+
branch_name="${4:-deployer}"
8+
9+
# Create the project directory
10+
mkdir -p "apps/$project_id/"
11+
12+
# Clone the specified branch of the specified repository
13+
git clone --single-branch --branch "$branch_name" "$repo_url" "apps/$project_id"
14+
15+
# Backup and remove the existing PHP configuration
16+
cp "apps/$project_id/php/php-fpm.ini" apps/php-fpm-tmp.ini
17+
rm -rf "apps/$project_id/php"
18+
19+
# Sync the current directory to the PHP directory, excluding specific folders and files
20+
rsync -av --exclude='apps' --exclude='.git' --exclude='vendor' ./ "apps/$project_id/php"
21+
22+
# Restore the PHP configuration
23+
cp apps/php-fpm-tmp.ini "apps/$project_id/php/php-fpm.ini"
24+
25+
# Set permissions for necessary directories
26+
mkdir -p "apps/$project_id/php/vendor"
27+
chmod -R 777 "apps/$project_id/php/vendor" "apps/$project_id/php/storage" "apps/$project_id/php/bootstrap"
28+
29+
# Install Composer dependencies using laravelsail/php83-composer
30+
docker run --rm \
31+
-u "$(id -u):$(id -g)" \
32+
-v "$(pwd)/apps/$project_id/php":/var/www/html \
33+
-w /var/www/html \
34+
laravelsail/php83-composer:latest \
35+
composer install --ignore-platform-reqs
36+
37+
# Set up the environment file
38+
cp "apps/$project_id/php/.env.staging" "apps/$project_id/php/.env"
39+
40+
# Generate a new Laravel application key
41+
docker run --rm \
42+
-u "$(id -u):$(id -g)" \
43+
-v "$(pwd)/apps/$project_id/php":/var/www/html \
44+
-w /var/www/html \
45+
laravelsail/php83-composer:latest \
46+
php /var/www/html/artisan key:generate
47+
48+
# Build the Docker image using the specified Dockerfile
49+
docker build -t "$project_id" -f "apps/$project_id/$dockerfile" "apps/$project_id"
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Inquid\Deployer\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Symfony\Component\Process\Process;
7+
8+
class CreateImageBundleCommand extends Command
9+
{
10+
protected $signature = 'deployer:create-image-bundle
11+
{project_id : The project ID}
12+
{dockerfile? : Dockerfile to use}
13+
{repo_url? : Repository URL}
14+
{branch_name? : Branch name}';
15+
16+
protected $description = 'Create a Docker image bundle for the project';
17+
18+
public function handle()
19+
{
20+
$project_id = $this->argument('project_id');
21+
$dockerfile = $this->argument('dockerfile');
22+
$repo_url = $this->argument('repo_url');
23+
$branch_name = $this->argument('branch_name');
24+
25+
$scriptPath = __DIR__ . '/../../scripts/create-image-bundle.sh';
26+
27+
// Prepare the arguments array
28+
$arguments = [$scriptPath, $project_id];
29+
30+
// Add optional arguments if they are provided
31+
if ($dockerfile) {
32+
$arguments[] = $dockerfile;
33+
}
34+
if ($repo_url) {
35+
$arguments[] = $repo_url;
36+
}
37+
if ($branch_name) {
38+
$arguments[] = $branch_name;
39+
}
40+
41+
$process = new Process($arguments, base_path());
42+
$process->setTimeout(null);
43+
44+
$process->run(function ($type, $buffer) {
45+
echo $buffer;
46+
});
47+
48+
if (!$process->isSuccessful()) {
49+
$this->error('An error occurred while creating the image bundle.');
50+
return 1;
51+
}
52+
53+
$this->info('Image bundle created successfully.');
54+
return 0;
55+
}
56+
}

src/DeployerServiceProvider.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Inquid\Deployer;
6+
7+
use Illuminate\Support\ServiceProvider;
8+
use Inquid\Deployer\Commands\CreateImageBundleCommand;
9+
10+
class DeployerServiceProvider extends ServiceProvider
11+
{
12+
/**
13+
* Register the command.
14+
*/
15+
public function register()
16+
{
17+
if ($this->app->runningInConsole()) {
18+
$this->commands([
19+
CreateImageBundleCommand::class,
20+
]);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)