Skip to content

Instantly share code, notes, and snippets.

@mavazca
Last active September 14, 2021 22:43
Show Gist options
  • Save mavazca/226bcc1ca00f258c7f66396739f246a0 to your computer and use it in GitHub Desktop.
Save mavazca/226bcc1ca00f258c7f66396739f246a0 to your computer and use it in GitHub Desktop.
Github Action

Github Action Testes e Deploy no Servidor de Produção

Diretório de pastas do repositório para o github action

.
├── ...
├── .github
│   └── workflows
│       ├── deployment.yml
│       └── tests.yml
└── ...

Configurando o GitHub

Personal access tokens

Para gerar um personal access tokens da sua conta do Github para o github action poder fazer alterações no repositório no caminho Settings / Developer settings / Personal access tokens

-[x] repo -[x] repo:status -[x] repo_deployment -[x] public_repo -[x] repo:invite -[x] security_events -[x] workflow

Secrets

Para gerar as secrets do projeto acessar o repositorio do projeto no github e ir settings / secrets e clicar no botão New repository secret e criar as secrets com os nomes abaixo:

  • PUSH_TOKEN
    • Hash do Personal access tokens
  • HOST
    • IP do Servidor VPS
  • USERNAME
    • Usuário ssh para acessar a VPS
  • SSH_PASSWORD
    • Chave ssh privada da sua maquina para acessar a VPS
  • PORT
    • Porta para acessar a VPS

Script shell

Criar um shell script com o nome server_deploy.sh na raiz do usuário utilizado com permissão de execução para fazer o deploy, esse shell script está configurado com os containers do laradock.

sudo chmod 755 ~/server_deploy.sh
name: Deploy
on:
push:
tags:
- 'v*'
jobs:
tests:
name: PHP ${{ matrix.php }}
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '7.4', '8.0' ]
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
token: ${{ secrets.PUSH_TOKEN }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, dom, fileinfo, mysql, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
coverage: xdebug
- name: Install composer dependencies
run: composer install --ignore-platform-reqs --no-progress --prefer-dist --optimize-autoloader
- name: Prepare the application
run: |
php -r "file_exists('.env') || copy('.env.example', '.env');"
php artisan key:generate
chmod -R 777 storage bootstrap/cache
php artisan config:clear
- name: Creating database to run the tests
run: |
mkdir -p database
touch database/database.sqlite
- name: Run Migration
run: php artisan migrate -v
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
- name: Run PHP Codesniffer
run: vendor/bin/phpcs
- name: Run tests via PHPUnit
run: vendor/bin/phpunit --verbose --coverage-text
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
build-js-css:
name: Build JS/CSS
runs-on: ubuntu-latest
needs: [ tests ]
steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.PUSH_TOKEN }}
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: '14'
- run: npm install
- run: npm run production
- name: Commit assets in git
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git checkout -B deploy
git add -f public/
git commit -m "Add assets front-end"
git push -f origin deploy
- name: Deploy to the Production Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PASSWORD }}
port: ${{ secrets.PORT }}
script: |
cd ~/
./server_deploy.sh
remove-branch:
name: Remove the deploy branch
runs-on: ubuntu-latest
needs: [ build-js-css ]
steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.PUSH_TOKEN }}
- name: Removing
run: git push origin --delete deploy
#!/bin/sh
echo ""
echo "--------------"
echo " Deploy "
echo "--------------"
echo ""
# Enter maintenance mode
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && php artisan down"
# Clear cache
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && php artisan optimize:clear"
# Checkout
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && git checkout deploy --force"
# Update codebase
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && git fetch origin deploy"
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && git reset --hard origin/deploy"
# Install dependencies based on lock file
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev"
# Migrate database
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && php artisan migrate --force"
# Clear AND Cache Configs and Routers
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && php artisan optimize"
# Permissões de pasta
docker exec -i laradock_workspace_1 /bin/bash -c "chown -R laradock.laradock laravel-app"
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && find . -type f -exec chmod 644 {} \;"
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && find . -type d -exec chmod 755 {} \;"
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && chmod -R 755 ./storage"
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && chmod -R 755 ./bootstrap/cache"
# Exit maintenance mode
docker exec -i laradock_workspace_1 /bin/bash -c "cd laravel-app && php artisan up"
echo ""
echo "------------------"
echo " Fim Deploy "
echo "------------------"
name: Tests
on:
push:
branches-ignore:
- deploy
tags-ignore:
- 'v*'
jobs:
tests:
name: PHP ${{ matrix.php }}
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '7.4', '8.0' ]
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
token: ${{ secrets.PUSH_TOKEN }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, dom, fileinfo, mysql, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv
coverage: xdebug
- name: Install composer dependencies
run: composer install --ignore-platform-reqs --no-progress --prefer-dist --optimize-autoloader
- name: Prepare the application
run: |
php -r "file_exists('.env') || copy('.env.example', '.env');"
php artisan key:generate
chmod -R 777 storage bootstrap/cache
php artisan config:clear
- name: Creating database to run the tests
run: |
mkdir -p database
touch database/database.sqlite
- name: Run Migration
run: php artisan migrate -v
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
- name: Run PHP Codesniffer
run: vendor/bin/phpcs
- name: Run tests via PHPUnit
run: vendor/bin/phpunit --verbose --coverage-text
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment