trenc is deploying to LOCAL... #104
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to LOCAL | |
run-name: ${{ github.actor }} is deploying to LOCAL... | |
on: | |
workflow_dispatch: | |
env: | |
HOST: ${{ vars.LCL_HOST }} | |
PORT: ${{ vars.LCL_PORT }} | |
REMOTE_PATH: ${{ vars.LCL_REMOTE_PATH }} | |
REMOTE_FOLDER: ${{ vars.LCL_REMOTE_FOLDER }} | |
LOCAL_PATH: src | |
SSH_USER: ${{secrets.LOCAL_SSH_USER}} | |
SSH_PRIVATE_KEY: ${{secrets.LOCAL_SSH_PRIVATE_KEY}} | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Setup environment vars | |
run: | | |
echo "now=$(date +%s)" >> $GITHUB_ENV | |
echo "remote_folder=$REMOTE_FOLDER" >> $GITHUB_ENV | |
echo "remote_path=$REMOTE_PATH" >> $GITHUB_ENV | |
echo "local_path=$LOCAL_PATH" >> $GITHUB_ENV | |
- name: Install OpenVPN | |
run: | | |
sudo apt-get update | |
sudo apt-get --assume-yes --no-install-recommends install openvpn | |
- name: Setup VPN config | |
run: | | |
echo "${{ secrets.CLIENT_OVPN }}" > client.ovpn | |
echo "${{ secrets.CLIENT_P12 }}" | base64 --decode > client.p12 | |
echo "${{ secrets.CLIENT_PASS }}" > client.pass | |
- name: Connect VPN | |
run: sudo openvpn --config "client.ovpn" --log "vpn.log" --daemon | |
- name: Wait for a VPN connection | |
timeout-minutes: 1 | |
run: until ping -c1 "$HOST"; do sleep 2; done | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Prepare files | |
run: | | |
mkdir ~/.ssh | |
echo "$SSH_PRIVATE_KEY" > ~/.ssh/local_key | |
chmod 600 ~/.ssh/local_key | |
echo "deployfile=deploy.${{ env.now }}.tar.gz" >> $GITHUB_ENV | |
echo "${{ secrets.LOCAL_ENV }}" > ${{ env.local_path }}/.env | |
mv "${{ env.local_path }}" "${{ env.remote_folder }}.${{ env.now }}" | |
- name: Upload files | |
run: | | |
tar -czf "${{ env.deployfile }}" "${{ env.remote_folder }}.${{ env.now }}" | |
scp -i ~/.ssh/local_key -o StrictHostKeyChecking=no -P "$PORT" "${{ env.deployfile }}" "$SSH_USER@$HOST:${{ env.remote_path}}" | |
- name: Move and extract files | |
run: | | |
ssh -tt -i ~/.ssh/local_key -o StrictHostKeyChecking=no -p "$PORT" "$SSH_USER@$HOST" "sudo -i <<EOF | |
set -e | |
cd ${{ env.remote_path }} | |
tar -xf ${{ env.deployfile }} | |
chown -R www-data:www-data ${{ env.remote_folder }}.${{ env.now }} | |
cd ${{ env.remote_folder}}.${{ env.now }} | |
sudo -u www-data php composer.phar install --optimize-autoloader --no-dev | |
php artisan optimize:clear | |
php artisan config:cache | |
php artisan route:cache | |
php artisan view:cache | |
php artisan migrate --force --no-interaction | |
cd ${{ env.remote_path }} | |
unlink ${{ env.remote_folder }} | |
ln -s ${{ env.remote_folder }}.${{ env.now }} ${{ env.remote_folder }} | |
rm ${{ env.deployfile }} | |
service php8.0-fpm restart | |
service apache2 graceful | |
exit | |
EOF" | |
- name: Kill VPN connection | |
if: always() | |
run: | | |
sudo killall openvpn |