Template
64 lines
2.0 KiB
YAML
64 lines
2.0 KiB
YAML
name: 'Build & Deploy'
|
|
env:
|
|
DEPLOY_DIR: /root
|
|
|
|
on:
|
|
push:
|
|
tags:
|
|
- v*
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
container:
|
|
image: shiran2488/golang-with-node:1.23
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v3
|
|
|
|
- name: Build Action
|
|
run: |
|
|
go build -ldflags="-s -w" -o server ./cmd/main_program
|
|
go build -ldflags="-s -w" -o cli ./cmd/cli_control
|
|
go build -ldflags="-s -w" -o scheduler ./cmd/scheduler
|
|
go build -ldflags="-s -w" -o initializer .
|
|
tar -czf deployment.tar.gz server cli scheduler initializer
|
|
|
|
- name: Save artifact
|
|
uses: actions/upload-artifact@v3
|
|
with:
|
|
name: build-artifacts
|
|
path: ./deployment.tar.gz
|
|
|
|
deploy:
|
|
needs: build
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Download Artifact
|
|
uses: actions/download-artifact@v3
|
|
with:
|
|
name: build-artifacts
|
|
|
|
- name: Set up SSH
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
echo "${{ secrets.PUBLICT_PRIVATE_KEY }}" > ~/.ssh/id_rsa
|
|
chmod 600 ~/.ssh/id_rsa
|
|
echo "${{ vars.DEPLOY_SERVER_LIST }}" > server_list.txt
|
|
while read -r ip; do
|
|
if [ -n "$ip" ]; then
|
|
ssh-keyscan -H "$ip" >> ~/.ssh/known_hosts
|
|
fi
|
|
done < server_list.txt
|
|
|
|
- name: Deploy to servers
|
|
run: |
|
|
while read -r ip; do
|
|
if [ -n "$ip" ]; then
|
|
echo "Deploying to $ip..."
|
|
scp -o StrictHostKeyChecking=no deployment.tar.gz ${{ vars.ROOT_USER_NAME }}@"$ip":"$DEPLOY_DIR/deployment.tar.gz"
|
|
ssh -n ${{ vars.ROOT_USER_NAME }}@"$ip" \
|
|
"set -e; bash '$DEPLOY_DIR/stop.sh'; tar -xzf '$DEPLOY_DIR/deployment.tar.gz' -C '$DEPLOY_DIR'; chmod +x '$DEPLOY_DIR/server' '$DEPLOY_DIR/cli' '$DEPLOY_DIR/scheduler' '$DEPLOY_DIR/initializer'; bash '$DEPLOY_DIR/start.sh'"
|
|
echo "Deployment to $ip completed"
|
|
fi
|
|
done < server_list.txt
|