diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..04fca25 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Seto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index c364a08..9bcd1ee 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,58 @@ -# gapush-to-deploy +# garust-debian -GitHub Action to deploy main branch to production a la Heroku. \ No newline at end of file +GitHub Action to running Rust binary on Debian Bulls Eye using SSH `rsync`. + +## Supported runner + +`ubuntu-20.04` + +[What is my Debian in my Ubuntu?](https://askubuntu.com/a/445496/513710). + +## Input + +```yaml + working-directory: + description: "Working directory for the build." + required: true + binary-name: + description: "Name of the binary to run. Usually the [[bin]] value in the Cargo.toml." + required: true + ssh-user: + description: "SSH user." + required: true + ssh-host: + description: "SSH host." + required: true + ssh-private-key: + description: "SSH private key." + required: true + ssh-known-hosts: + description: "SSH known hosts." + required: true +``` + +## Example + +```yaml +name: "Build and release" + +on: + push: + branches: + - main + +jobs: + build-and-release: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + - uses: setoelkahfi/garust-debian@v1 + with: Install Rust stable + ssh-key: ${{ secrets.SSH_PRIVATE_KEY }} + ssh-known-hosts: ${{ secrets.SSH_KNOWN_HOSTS }} + ssh-user: ${{ secrets.SSH_USER }} + ssh-host: ${{ secrets.SSH_HOST }} + project-directory: ${{ secrets.PROJECT_DIRECTORY }} + project-name: ${{ secrets.PROJECT_NAME }} + +``` \ No newline at end of file diff --git a/action.yml b/action.yml index f589223..ae06341 100644 --- a/action.yml +++ b/action.yml @@ -1,14 +1,17 @@ -name: "GitHub Action Push-to-deploy" -description: "GitHub Action to deploy main branch to production a la Heroku." +name: "GitHub Action Rust-on-Debian" +description: "GitHub Action to running Rust binary on Debian Bulls Eye." inputs: - git-directory: - description: "Your git bare repo directory." + project-directory: + description: "Working directory for the build." required: true - git-user: - description: "Git user. Usually deploy." + project-name: + description: "Name of the binary to run." required: true - git-host: - description: "Git host." + ssh-user: + description: "SSH user." + required: true + ssh-host: + description: "SSH host." required: true ssh-private-key: description: "SSH private key." @@ -20,22 +23,19 @@ inputs: runs: using: "composite" steps: - - name: Checkout - uses: actions/checkout@v4 - with: - depth: 0 - - name: Setup remote git - run: | - git config --global user.email "${{ inputs.git-user }}@${{ inputs.git-host }}" - git config --global user.name "${{ inputs.git-user }}" - git remote add prod ${{ inputs.git-user }}@${{ inputs.git-host }}:${{ inputs.git-directory }} + - name: Install Rust stable + uses: dtolnay/rust-toolchain@stable + - name: Build for release + run: cargo build --release shell: bash - name: Install SSH key uses: shimataro/ssh-key-action@v2 with: key: ${{ inputs.ssh-private-key }} known_hosts: ${{ inputs.ssh-known-hosts }} - - name: Push-to-deploy - run: | - git push prod HEAD:main - shell: bash \ No newline at end of file + - name: rsync over SSH + run: rsync -r ./target/release/ ${{ inputs.ssh-user }}@${{ inputs.ssh-host }}:${{ inputs.project-directory }} + shell: bash + - name: Start process + run: ssh ${{ inputs.ssh-user }}@${{ inputs.ssh-host }} 'bash -s' < start.sh ./${{ inputs.project-directory }} ${{ inputs.project-name }} + shell: bash diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..908fb51 --- /dev/null +++ b/start.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +echo "------------------------ STARTING DEPLOYMENT ------------------------" + +# Check if the first argument is a directory +if [ ! -d "$1" ] +then + echo "๐Ÿ˜ฉ $1 is not a directory." + echo "๐Ÿ›‘ Deployment failed." + exit 1 +fi + +# Change to working directory +cd $1 + +# Check if process is running + +# Check if the second argument is a process +if [ -z "$2" ] +then + echo "๐Ÿ˜ฉ No process name provided." + echo "๐Ÿ›‘ Deployment failed." + exit 1 +fi +process_name=$2 + +# Check if binary exists +if [ ! -f "$process_name" ] +then + echo "๐Ÿ˜ฉ $process_name does not exist." + echo "๐Ÿ›‘ Deployment failed." + exit 1 +fi + +echo "โœ… Arguments are valid." + +# Check if process is running +echo "๐Ÿ” Looking for $process_name process." +PID=$(pidof $process_name) + +echo "๐Ÿคจ Killing or not killing $process_name." + +if [ ! -z "$PID" ] +then + echo "โœ… $process_name is running as $PID." + echo "โณ Killing it." + kill -9 $PID + echo "โœ… Killed $process_name." +else + echo "$process_name is not running." + echo "No killing needed." +fi + +echo "๐Ÿš€ Starting $process_name." + +./$process_name > /dev/null 2>&1 & + +echo "------------------------ DEPLOYMENT COMPLETED ------------------------" + +exit 0 \ No newline at end of file