mrengy / vanguard-history

History site for the Santa Clara Vanguard
GNU General Public License v3.0
0 stars 0 forks source link

Set up auto deploys from main branch to stage environment #28

Open mrengy opened 2 years ago

mrengy commented 2 years ago

https://github.com/marketplace/actions/branch-deploy

mrengy commented 1 year ago

Asked question in Stack Overflow

shekharkhedekar commented 1 year ago

I think you might be able to use one of these https://github.com/marketplace?type=actions&query=ssh+

The workflow might look something like this:

name: Deploy latest main to stage
on:
  push:
    branches:
      - main

jobs:
  build:
    name: Deploy to Stage
    runs-on: ubuntu-latest
    steps:
    - name: executing remote ssh commands using password
      # https://github.com/marketplace/actions/ssh-remote-commands
      uses: appleboy/ssh-action@v0.1.6
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        password: ${{ secrets.PASSWORD }}
        port: ${{ secrets.PORT }}
        # You might have to cd to the repo and pull here
        script: git pull
mrengy commented 1 year ago

Ah, yes. Thanks! I think this will get me a lot closer than what I was looking at originally.

Note to self: I may want to generate ssh keys https://help.dreamhost.com/hc/en-us/articles/216499537-How-to-configure-passwordless-login-in-Mac-OS-X-and-Linux and use that rather than my ssh password.

-- Mike Eng (he/him) mike-eng.com linkedin.com/in/mikeeng401

On Jan 2, 2023, at 2:38 PM, Shekhar Khedekar @.***> wrote:

 I think you might be able to use one of these https://github.com/marketplace?type=actions&query=ssh+

The workflow might look something like this:

name: Deploy latest main to stage on: push: branches:

  • main

jobs: build: name: Deploy to Stage runs-on: ubuntu-latest steps:

  • name: executing remote ssh commands using password

    https://github.com/marketplace/actions/ssh-remote-commands

    uses: @.*** with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} password: ${{ secrets.PASSWORD }} port: ${{ secrets.PORT }}

    You might have to cd to the repo and pull here

    script: git pull

    — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were assigned.

mrengy commented 1 year ago

I tried to do this using an SSH key rather than username / password, but I'm stuck. I generated an id_ed25519 keypair on the server and saved secrets in Github according to these instructions but with a couple of modifications:

  1. I tried saving the private key to the repository secret first, got permission denied errors, then changed the secret to use the public key instead, which sounded like the right thing to do? (still got permission denied errors)
  2. Step 4: I'm using a different github actions file - it's in the branch attached to this issue - https://github.com/mrengy/vanguard-history/tree/28-set-up-auto-deploys-from-main-branch-to-dev-environment
  3. I didn't do anything in Step 5 and beyond since it didn't seem relevant.

With each push, when I look at the action in Github, I'm getting:

Run ssh server pwd ssh server pwd shell: /usr/bin/bash -e {0} Load key "/home/runner/.ssh/server.key": error in libcrypto Permission denied, please try again. Permission denied, please try again. @: Permission denied (publickey,password). Error: Process completed with exit code 255.

I have the following secrets set at the repo level

Screen Shot 2023-01-24 at 12 10 35 AM

I think all the repository secrets are correct. To show what these variables are, when I SSH into the server using Terminal, I use the command "ssh SSH_USER@SSH_HOST".

And SSH_KEY is the public key I generated on the server - the contents of the file id_ed25519

Also (I didn't use these in the actions file yet, but thought we'd need them for the future) I made a couple of environments and these environment variables.

Screen Shot 2023-01-24 at 12 15 08 AM

@shekharkhedekar if you think it's worth the effort, feel free to try and sort this out and let me know if you need anything else. Honestly, I'm not sure if it's worth the effort ;)

shekharkhedekar commented 1 year ago

You'll need to create a public/private key pair, add the private key to github, and the public key to the remote host - see here https://docs.oracle.com/cd/E19683-01/806-4078/6jd6cjru7/index.html

mrengy commented 1 year ago

I got the SSH key to work - swapped them as you recommended. However, the commands I'm trying to run in the action to cd to the stage directory (where I would later pull to main) aren't working. I fiddled with it for a while but couldn't get it right. Seems when it first connects via SSH it's going to the directory /home/runner/work/vanguard-history/vanguard-history, and trying to change directories to /home/mrengyscv/stage.historyscv is giving an error. You can see some of the results under various Actions.

I probably spent more time than I should have on this. ;)

For now, once you merge https://github.com/mrengy/vanguard-history/pull/97 , I'll set up a cron job to automatically pull main to stage every 15 mins or so.

mrengy commented 1 year ago

I set up "git pull" to run on dev and stage every 5 and 15 mins, respectively. However, the worfklow to compile CSS is running into "npm command not found" on the cronjob. It's weird because it should be using the same user that I use when I SSH in manually - where the compile CSS workflow works. I emailed dreamhost support about this.

shekharkhedekar commented 1 year ago

Check this out https://stackoverflow.com/questions/54557146/not-able-to-use-node-js-and-crontab