Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add run via ssh action #81

Merged
merged 2 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/actions/run-via-ssh/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: "Run via SSH"
description: "Connects securely to a server over Tailscale and runs a shell script."
inputs:
ts_hostname:
description: "Tailscale server hostname to use"
required: true
ts_oauth_client_id:
description: "Tailscale OAuth2 client ID to use"
required: true
ts_oauth_secret:
description: "Tailscale OAuth2 secret to use"
required: true
ts_tags:
description: "Tailscale tags to use."
default: ""
required: false
ssh_username:
description: "SSH username to use."
required: true
ssh_private_key:
description: "SSH private key to use."
required: true
run:
description: "Shell script to execute"
required: true

runs:
using: "composite"
steps:
- name: Connect to Tailscale network
uses: tailscale/github-action@v2
with:
oauth-client-id: ${{ inputs.ts_oauth_client_id }}
oauth-secret: ${{ inputs.ts_oauth_secret }}
tags: ${{ inputs.ts_tags }}

- name: Install SSH key
shell: bash --noprofile --norc -eo pipefail -ux {0}
run: |
install -m 600 -D /dev/null ~/.ssh/id_rsa
echo "${{ inputs.ssh_private_key }}" > ~/.ssh/id_rsa
SERVER_IP="$(tailscale ip -6 ${{ inputs.ts_hostname }})"
ssh-keyscan $SERVER_IP > ~/.ssh/known_hosts

- name: Run script
shell: bash --noprofile --norc -eo pipefail -ux {0}
run: ssh -t ${{ inputs.ssh_username }}@${{ inputs.ts_hostname }} "${{ inputs.run }}"

- name: Nuke SSH keys
shell: bash --noprofile --norc -eo pipefail -ux {0}
run: rm -rf ~/.ssh
33 changes: 13 additions & 20 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,17 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Connect to Tailscale network
uses: tailscale/github-action@v2
- name: Deploy to production server
uses: ./.github/actions/run-via-ssh
with:
oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
tags: tag:ci

- name: Install SSH key
run: |
install -m 600 -D /dev/null ~/.ssh/id_rsa
echo "${{ secrets.CI_PRIVATE_KEY }}" > ~/.ssh/id_rsa
SERVER_IP="$(tailscale ip -6 ${{ env.SERVER_HOSTNAME }})"
ssh-keyscan $MACHINE_IP > ~/.ssh/known_hosts

- name: Connect over SSH and deploy
run: |
SERVER_IP="$(tailscale ip -6 ${{ env.SERVER_HOSTNAME }})"
ssh -t ci@$SERVER_IP "cd /app && git reset --hard && git pull && ./scripts/rebuild_docker.sh"

- name: Nuke SSH keys
run: rm -rf ~/.ssh
ts_hostname: ${{ env.SERVER_HOSTNAME }}
ts_oauth_client_id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
ts_oauth_secret: ${{ secrets.TS_OAUTH_SECRET }}
ts_tags: tag:ci
ssh_username: ci
ssh_private_key: ${{ secrets.CI_PRIVATE_KEY }}
run: |
cd /app
git reset --hard
git pull
./scripts/rebuild_docker.sh