Skip to content

Commit

Permalink
Updata action
Browse files Browse the repository at this point in the history
  • Loading branch information
yankewei committed Aug 9, 2022
1 parent 11fdce8 commit 90ffbd0
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ jobs:
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
password: ${{ secrets.SSH_PASSWORD }}
app_env_data: ${{ secrets.APP_ENV_DATA }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
6 changes: 0 additions & 6 deletions Envoy.blade.php

This file was deleted.

35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
# laravel-deploy-action
Automatically deploy your Laravel app to remote server

### Step one
You need to create four environments that we need to connect the remote server, then create your workflow like below
```yaml
name: Deoloy
on: [push]

jobs:
deploy:
runs-on: ubuntu-latest
name: A Laravel deploy action example
steps:
- name: test
uses: yankewei/laravel-deploy-action@main
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
app_env_data: ${{ secrets.APP_ENV_DATA }}
```
Below is the environment description
```yaml
host:
description: 'The remote server ip, support IPV4 only'
required: true
username:
description: 'The ssh login user name'
required: true
password:
description: 'The ssh login password'
required: true
app_env_data:
description: 'The environment variable for application, will be written into .env file'
required: true
```
25 changes: 14 additions & 11 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ inputs:
username:
description: 'The ssh login user name'
required: true
ssh_private_key:
description: 'The ssh private key, will be stored into .ssh/github_action'
requierd: true
password:
description: 'The ssh login password'
required: true
app_env_data:
description: 'The environment variable for application, will be written into .env file'
required: true

runs:
using: 'composite'
Expand All @@ -20,13 +23,13 @@ runs:
with:
php-version: '8.1'
tools: composer:v2
extensions: ssh2

- name: deploy your application
- name: Deploy
env:
HOST: ${{ inputs.host }}
USERNAME: ${{ inputs.username }}
PASSWORD: ${{ inputs.password }}
APP_ENV_DATA: ${{ inputs.app_env_data }}
shell: bash
run: |
mkdir ~/.ssh
touch ~/.ssh/github_action
echo '${{ inputs.ssh_private_key }}' >> ~/.ssh/github_action
cd ${{ github.workspace }}
composer require "laravel/envoy:^2.0"
cat Envoy.blade.php
run: php ${{ github.workspace }}/deploy.php
38 changes: 38 additions & 0 deletions deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

// Get the required information when connect remote server
$host = getenv('HOST');
$username = getenv('USERNAME');
$password = getenv('PASSWORD');

// Create a session between with the remote server
$ssh_session = ssh2_connect($host);

// Use password to connect server
if (!ssh2_auth_password($ssh_session, $username, $password)) {
exit('Failed to connect the remote server');
}

// Get the application environment variable from github action env
$env = getenv('APP_ENV_DATA');
$env_array = explode("\n", $env);

// Put the env data into .env file
$cmd = 'cd /var/www/site;cat /dev/null > .env;';
foreach ($env_array as $value) {
$cmd .= "echo $value >> .env;";
}

// Run command and deploy application
$cmd .= 'php artisan down;';
$cmd .= 'git pull origin main;';
$cmd .= 'composer install --no-dev;';
$cmd .= 'php artisan migrate --force;';
$cmd .= 'php artisan optimize;';
$cmd .= 'php artisan up;';

$stream = ssh2_exec($ssh_session, $cmd);

echo stream_get_contents($stream);

ssh2_disconnect($ssh_session);

0 comments on commit 90ffbd0

Please sign in to comment.