Github CI/CD reuse step - github

I'm kinda beginner with CI/CD, but I wrote a code that deploys Vue/Vite project to Ubuntu VPS. But, it's not as it should be. So what am I doing actually?
First as usual, installing the project and building it.
jobs:
build:
name: "Build"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Install
run: yarn
- name: Build
run: yarn build
So the problem is when that passes. I'm connecting to ssh like:
deploy:
name: "Deploy"
needs: project_setup
runs-on: ubuntu-latest
steps:
- name: Deploy to server
uses: appleboy/ssh-action#master
env:
GIT_REPO: Comet-Frontend
GIT_SSH: ${{ github.repositoryUrl }}
with:
host: ${{ secrets.VPS_IP }}
username: ${{ secrets.VPS_USER }}
password: ${{ secrets.VPS_PASSWORD }}
port: ${{ secrets.VPS_PORT }}
envs: GIT_SSH, GIT_REPO
and at the very bottom:
script: |
cd /var/www/vue
git pull
ls
yarn
yarn build
cp -R /root/Frontend/dist /var/www/vue
So I would like to define ssh connection once and run those scripts separately with different step names. Is that possible or I have to connect to ssh for every step?

If each step needs SSH to access either a remote repository URL or your VPS target server, then yes, you would need SSH to each step.
The alternative being to copy a deployment script to the server (through SSH): the steps included in that script could be executed directly on that server (where the script has been copied). No need for SSH then for that script execution, since it is already at target.

Related

Java Springboot Deployment using GitHub

I am trying to deploy my Springboot App to my Linux VM using GitHub. The deployment itself works, but the GitHub Action does not stop running since the last command executed is still running but should not be stopped. How can I solve this?
name: Backend Deployment to Linux VM
on:
push:
branches:
- main
jobs:
build-and-deploy:
name: Backend Deployment to Linux VM
runs-on: ubuntu-latest
steps:
- name: update and start project
uses: appleboy/ssh-action#master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
kill -9 $(lsof -t -i:8080)
cd /home/github_deploy_backend
cd backend-P2cinema
git pull
mvn clean package
nohup java -jar target/*.jar &

Running a Docker Image via SSH Github Actions

so I'm currently trying to make GitHub Actions/CI SSH into my VPS and run a docker image. Although the main problem is that the job doesn't finish up after running the final command.
This is my YML file:
name: SSH & Deploy Image
on:
workflow_run:
workflows: ["Timmy Docker Build"]
branches: [ main ]
types:
- completed
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Run Docker CMD
uses: appleboy/ssh-action#master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.PORT }}
script: |
docker stop ss-timmy && docker rm ss-timmy
docker pull spaceturtle0/ss-timmy:latest
docker run --env-file=Timmy-SchoolSimplified/.env spaceturtle0/ss-timmy &
Regardless of having put the & sign at the final script command, the process just hangs until the process is killed. Is there something to fix this?
You should use -d flag that means detached instead & sign for last docker command. So full command will be:
docker run -d --env-file=Timmy-SchoolSimplified/.env spaceturtle0/ss-timmy

Github Actions - copy files to VPS

I would like to solve this at a small VPS Provider:
Github worflow, when pushed files to GitHub, then "GH workflows" copies the php files to the live VPS server.
How can I copy them?
.github/workflows/vps-deploy-prod.yml:
name: vps-deploy-prod
on:
push:
branches:
- master
jobs:
build: 'VPS Run Deploy'
runs-on: ubuntu-latest
steps:
- name: 'Checkout'
uses: action/checkout#master
- name: 'Deploy'
run: make vps-run-deploy
The Makefile:
##### VPS specific target
vps-run-deploy:
?? copy files via ssh scp...?
You might consider the GitHub action appleboy/scp-action
GitHub Action for copying files and artifacts via SSH.
Here is an example in this project: renatomh/gobarber-backend/.github/workflows/main.yml:
# Copy the repository to the directory on the server/VPS used for the application
- name: Copy dist to VPS
uses: appleboy/scp-action#master
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
port: ${{ secrets.SSH_PORT }}
key: ${{ secrets.SSH_KEY }}
# Selecting all folders except "node_modules"
source: ".,!node_modules"
# The path is based on the directory where the user logged into the server starts
target: "~/app/gobarber-backend"

gh-pages deployment issue, job fails on deploy. The directory you're trying to deploy ... doesn't exist

gh-pages deployment fails with next error: My repository failed job
Checking configuration and starting deployment… 🚦
Error: The directory you're trying to deploy named /home/runner/work/azure-flask-react/azure-flask-react/dist doesn't exist. Please double check the path and any prerequisite build scripts and try again. ❗
Deployment failed! ❌
I'm trying to deploy ReactApp at Github and besides deploy Python-Flask backend hosted at Azure and back-app has its automatically generated job yml.
But for front-app I followed this answer and manually added second job in yml because I need to provide env.variables.
My backend deployment succeeds but front-app constantly fails because of duplicated path
/home/runner/work/azure-flask-react/azure-flask-react/dist
Here is my yml and package.json but there is no any extra mentioning of that directory...
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
# More GitHub Actions for Azure: https://github.com/Azure/actions
name: Build and deploy Python app to Azure Web App - first-py-app
on:
push:
branches:
- main
workflow_dispatch:
jobs:
front-build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v1
- name: Build
run: |
npm --prefix front-app install
npm --prefix front-app run-script build
env:
REACT_DEV_SERVER_URL: ${{ secrets.REACT_DEV_SERVER_URL }},
REACT_DEV_FRONT_APP_URL: ${{ secrets.REACT_DEV_FRONT_APP_URL }}
- name: Deploy
uses: JamesIves/github-pages-deploy-action#releases/v3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN_KEY }}
BRANCH: gh-pages
FOLDER: dist
back-build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: Set up Python version
uses: actions/setup-python#v1
with:
python-version: '3.8'
- name: Build using AppService-Build
uses: azure/appservice-build#v2
with:
platform: python
platform-version: '3.8'
- name: 'Deploy to Azure Web App'
uses: azure/webapps-deploy#v2
with:
app-name: 'first-py-app'
slot-name: 'production'
publish-profile: ${{ secrets.AzureAppService_PublishProfile_7edcdecca83a4354a87943f94bb32fca }}
{
...
"homepage": "https://nikonov91-dev.github.io/azure-flask-react",
"scripts": {
...
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
}
and my file structure
azure-proj
|-front-app (containing reactjs)
|-package.json
|-node_modules
|-src
|-app.py (python-flask application which deploys successfully)
I misunderstood the issue message, the problem was not duplicating the problem was the missed inner path passed in FOLDER in gh-pages YML settings
There was a hint in BUILD step
And one more thing: do not forget to get and add to GH your personal-access-token

Github Workflow Actions And EC2: Error Loading Key Invalid Format

I am trying to set up CI for my nodejs server. I would like to use github actions to ssh into my ec2 instance, where I can then git clone/pull my updated repo.
I can ssh into my ec2 instance on my local machine w no issues. I just do something like: "ssh -i keypar.pem username#some-ip.region.compute.amazonaws.com" and it connects. However, I can't seem to get a connection working on the worflow/actions script. Here is what I have in my workflow yml file:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Connect
env:
DEPLOY_KEY: ${{ secrets.EC2 }}
run: |
eval `ssh-agent`
ssh-add - <<< "${DEPLOY_KEY}"
ssh ec2-user#ec2-instance-ip-here.us-east-2.compute.amazonaws.com
This script gets me the error "Error loading key "(stdin)": invalid format". Also when I look at the deploy key section under repo settings, it says the key has never been used.
(Obviously I would need to install, clone, and perform other steps in addition to what is listed above.)
In summary:
1 how to I fix the invalid format error?
2 how do I load and reference the key pair?
There is a better way to perform SSH commands in a EC2:
name: CI
on: [push, pull_request]
jobs:
# test:
# ...
deploy:
name: "Deploy to staging"
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
# needs: test
steps:
- name: Configure SSH
run: |
mkdir -p ~/.ssh/
echo "$SSH_KEY" > ~/.ssh/staging.key
chmod 600 ~/.ssh/staging.key
cat >>~/.ssh/config <<END
Host staging
HostName $SSH_HOST
User $SSH_USER
IdentityFile ~/.ssh/staging.key
StrictHostKeyChecking no
END
env:
SSH_USER: ${{ secrets.STAGING_SSH_USER }}
SSH_KEY: ${{ secrets.STAGING_SSH_KEY }}
SSH_HOST: ${{ secrets.STAGING_SSH_HOST }}
- name: Stop the server
run: ssh staging 'sudo systemctl stop my-application'
- name: Check out the source
run: ssh staging 'cd my-application && git fetch && git reset --hard origin/master'
- name: Start the server
if: ${{ always() }}
run: ssh staging 'sudo systemctl start my-application'
Credit: GitHub Actions: How to run SSH commands (without third-party actions)