create deployment workflow #7
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
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
name: Go | ||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.20' | ||
- name: Build | ||
run: go build -v ./... | ||
- name: Package | ||
run: go build && tar czvf hkgi.tar.gz hkgi data migrations | ||
- name: Upload Artifact | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: hkgi.tar.gz | ||
path: hkgi.tar.gz | ||
deploy: | ||
runs-on: ubuntu-latest | ||
environment: production | ||
if: | ||
contains('refs/heads/master', github.ref) | ||
steps: | ||
- name: Download latest package | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: hkgi.tar.gz | ||
- name: Deploy with SCP | ||
uses: i3h/deploy-with-scp@v1 | ||
with: | ||
src: ./hkgi.tar.gz | ||
dest: ${{ secrets.SSH_DIR }}/hkgi-new.tar.gz | ||
username: ${{ secrets.SSH_USER }} | ||
server-ip: ${{ secrets.SSH_HOST }} | ||
ssh-key: ${{ secrets.SSH_KEY }} | ||
- name: Configure SSH | ||
run: | | ||
mkdir -p ~/.ssh | ||
echo "$SSH_KEY" >> ~/.ssh/id_rsa | ||
chmod 0600 ~/.ssh/id_rsa | ||
cat >>~/.ssh/config <<END | ||
Host production | ||
Hostname $SSH_HOST | ||
User $SSH_USER | ||
IdentityFile ~/.ssh/id_rsa | ||
StrictHostKeyChecking no | ||
END | ||
env: | ||
SSH_USER: ${{ secrets.SSH_USER }} | ||
SSH_HOST: ${{ secrets.SSH_HOST }} | ||
SSH_KEY: ${{ secrest.SSH_KEY }} | ||
- name: Extract tarball on production host | ||
run: ssh staging tar xzvf "$SSH_DIR/hkgi-new.tar.gz" -C "$SSH_DIR" | ||
- name: Restart service on production host | ||
run: ssh staging /usr/bin/sudo /usr/bin/systemctl restart hkgi |