惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
IT之家
IT之家
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
H
Help Net Security
V
V2EX
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
博客园 - 叶小钗
J
Java Code Geeks
博客园 - 【当耐特】
月光博客
月光博客
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件

Posts on Noah Bailey

How to turn anything into a router Deploy to Cloudfront from GitHub using OpenID Connect Backup Postgres databases with Kubernetes CronJobs The spelling error made 200 billion times a day Restarting Kubernetes pods using a CronJob You've just bought a new domain. Now what? Who Sawed My Motherboard??? Linux on the P8 Aliexpress Mini Laptop Recovering Mysql/Mariadb after a nasty crash Using EXIF data to pick my next lens Converting and developing RAW photos on Linux automatically Thank you, 2016 iPhone Don't Make It Work Self-hosted Surveillance with ZoneMinder Backups, Monitoring, and Security for small Mastodon servers Block web scanners with ipset & iptables Debian Sid on encrypted ZFS Protect your dangerously insecure redis server Debian: the luxurious boring lifestyle Monitor radiation with a Raspberry Pi Simple Linux server alerts: Know your performance, errors, security, syslog, and security NUC crashes on debian 11 - How I fixed it Basic Linux server security with fail2ban, ossec, and firewall Windows 11 will create heaps of needless trash Domesticated Kubernetes Networking The Cursed Certificate Our mostly disposable and entirely stupid world Trying out OpenBSD (as a Linux geek) Making VoIP Calls with Antique Rotary Phones Monitoring WAN speed with speedtest-cli and ElasticSearch
Executing commands over SSH with GitHub Actions
2022-09-19 · via Posts on Noah Bailey

Several admins and developers like automatically updating their servers with new builds as they become available. Commonly known as “CI/CD”, this process allows teams to iterate much faster and speed up product development.

Often, this is simply pulling from a repo and running a couple docker-compose commands, which is very easy to automate.

A bad way to do this is using a cron job that runs every 10 minutes to pull from the repository and execute any commands. While reasonably simple and secure, this can cause a large delay between code updates and the server task running.

An improvement is to connect the CI system to the hosting server. This allows the build system to automatically connect to the server using SSH to perform the update immediately, eliminating the lag from the cron-style solution.

There are potential drawbacks, however. It’s possible that the CI system could be compromised, and a credential to your server could be exposed.

Deploy user

On the server, create a new user called “deploy” who is a member of the group “docker”.

sudo useradd --create-home --user-group --shell /bin/bash --groups docker deploy

sudo usermod --lock deploy

This allows the user to update the docker config without having full root access on the server.

SSH key

On the server, change user to “deploy” and create a new ssh key:

sudo -i -u deploy

Create a new SSH key for that user:

ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -C "deploy@server"

The key should not have a passphrase in this case.

Once complete, allow the user to access the server using its own credentials:

cat .ssh/id_ed25519.pub > .ssh/authorized_keys

Save the string that begins with ssh-ed25519... for the next step.

GitHub Keys & Secrets

Both the public and private keys will need to be added to the github repository.

In your repo, go to the ‘Settings’ tab, and find the ‘Deploy keys’ tab on the left. Click “Add deploy key” and enter the content of .ssh/id_ed25519.pub into the box. It should begin with “ssh-ed25519”.

Next, click on ‘Secrets’ and ‘Actions’ on the left. Click “New repository secret”, and create three secrets:

  1. With the name “SSH_KEY”, enter the the contents of the file .ssh/id_ed25519 into the Secret field. It should begin with the line, “-----BEGIN OPENSSH PRIVATE KEY-----”.

  2. With the name “SSH_HOST”, add a secret containing the hostname or IP of your server.

  3. With the name “SSH_USER”, add the user created earlier, “deploy”, that is used to push updates to our server.

GitHub Actions Workflow

Next, a workflow file is created in the github repository. The filepath must be .github/workflows/deploy.yml

If you already have workflows, make sure this one runs at the end, or whenever in the sequence is appropriate.

The file deploy.yml:

name: Remote update execution
on:
  push:
    branches:
      - main
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - name: executing remote ssh commands using password
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.SSH_HOST }}
        username: ${{ secrets.SSH_USER }}
        key: ${{ secrets.SSH_KEY }}
        script: |
          bash /opt/git/my-repository/update.sh          

Once connected, this will execute a script on the server, for example:

#!/bin/bash
cd /opt/git/my-repository
git pull
docker-compose pull && docker-compose up -d 

This script updates its repository before pulling any changes to the docker images and recreating any containers.

On a push, this will execute the following sequence:

  1. GitHub executes a script within their system after receiving a commit
  2. A GitHub worker uses the SSH_HOST, SSH_KEY, and SSH_USER variables to connect to the server remotely
  3. After successfully authenticating, the script on the server is executed,
  4. During execution, changes to the repository are pulled and the live system is updated.

Together, this has the effect of quickly updating the server immediately after a new commit is pushed to GitHub.

There are much more complex and sophisticated ways to perform the same operation, but this is relatively simple and easy to configure.