Skip to content

Commit

Permalink
add deploy shell
Browse files Browse the repository at this point in the history
  • Loading branch information
zzq0826 committed Nov 23, 2021
1 parent 74a49fc commit 0b1e633
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/build_deploy_manual.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# This workflow will build and push a new container image to Amazon ECR

on:
workflow_dispatch:
inputs:
version:
description: "deploy version"
required: true
env:
description: "env to deploy, default is dev"
required: false
default: "dev"
user:
description: "user who exec this workflow"
required: true
default: "zhengqi zhang"
change_desciption:
description: "brief description of changes"
required: true
default: "some cool feature"

name: build and deploy

jobs:
build_push_image2acr:
name: build push image
runs-on: ubuntu-latest
steps:
- name: checkout and get new tag
uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16.5.0"
- name: Login to TCR
uses: aliyun/acr-login@v1
with:
login-server: ccr.ccs.tencentyun.com/fluidex/fluidex-web
username: ${{ secrets.TENCENTYUN_USER }}
password: ${{ secrets.TENCENTYUN_PWD }}
- name: Build and push image
env:
# IMAGE_TAG: ${{ github.sha }}
IMAGE_TAG: ${{ github.event.inputs.version }}
PRIVATE_ACCESS_TOKEN: ${{ secrets.PRIVATE_ACCESS_TOKEN }}
ssh_identity_key: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
eval $(ssh-agent -s)
echo "$ssh_identity_key" | tr -d '\r' | ssh-add -
mkdir -p ~/.ssh && chmod 700 ~/.ssh
ssh-add -l
yarn install
git submodule update --init --recursive
yarn compilecontract
make sync-kline
yarn run build
docker build -t ccr.ccs.tencentyun.com/fluidex/fluidex-web:$IMAGE_TAG -f ./docker/Dockerfile .
docker push ccr.ccs.tencentyun.com/fluidex/fluidex-web:$IMAGE_TAG
echo "::set-output name=image::ccr.ccs.tencentyun.com/fluidex/fluidex-web:$IMAGE_TAG"
deploy_to_ec2:
needs: build_push_image2acr
name: deploy
runs-on: ubuntu-latest
steps:
- name: checkout and get new tag
uses: actions/checkout@v2
- name: Make shell executable
run: chmod +x ./docker/fluidex-web.sh

- name: connect server
env:
ssh_identity_key: ${{ secrets.DEV_PEM }}
version: ${{ github.event.inputs.version }}
env: ${{ github.event.inputs.env }}
run: |
eval $(ssh-agent -s)
echo "$ssh_identity_key" | tr -d '\r' | ssh-add -
mkdir -p ~/.ssh && chmod 700 ~/.ssh
ssh-add -l
./docker/fluidex-web.sh deploy "$version" "$env"
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
4 changes: 4 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx

ADD ./build/exchange /usr/share/nginx/html
ADD ./docker/default.conf /etc/nginx/conf.d/default.conf
46 changes: 46 additions & 0 deletions docker/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
server {
listen 80;
listen [::]:80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
100 changes: 100 additions & 0 deletions docker/fluidex-web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash
server="18.180.248.43"
server_arr_dev=('18.182.40.214')
server_arr_prod=('18.180.248.43')

set -ex
IMAGE_TAG=$2
DOCKER_PORT="8080"
if [ -z "$IMAGE_TAG" ]; then
IMAGE_TAG=$(git rev-parse --verify HEAD)
fi

ENV=$3

if [ -z "$ENV" ]; then
ENV="dev"
fi

if [ $ENV = prod ]; then
DOCKER_PORT="8081"
fi

ECR_REGISTRY="ccr.ccs.tencentyun.com"
ECR_REPO="fluidex"
NAME=fluidex-web
CONTAINER_NAME=${NAME}-${ENV}
IMAGE=${ECR_REGISTRY}/${ECR_REPO}/${NAME}:$IMAGE_TAG

echo "$1"
#IMAGE=$IMAGE-web
echo image: "$IMAGE"

servers=`eval echo '$'{server_arr_"$ENV"[@]}`

function build() {
docker build -t "$IMAGE" -f Dockerfile ..
}

function pull() {
docker pull "$IMAGE"
}

function push() {
docker push "$IMAGE"
}

function run() {
docker rm -f $CONTAINER_NAME
docker run -d --restart=always -p $DOCKER_PORT:80 --name $CONTAINER_NAME $IMAGE
}

function pull_run() {
pull
run
}

function build_push() {
build
push
}

function deploy() {
for server in ${servers[@]}
do
ssh-keyscan ${server} >> ~/.ssh/known_hosts &&
echo deploy $ENV on "$server" start
scp ./docker/fluidex-web.sh ubuntu@${server}:~/
ssh ubuntu@${server} "
./fluidex-web.sh pull_run ${IMAGE_TAG} ${ENV}
"
echo deploy $ENV on "$server" finish
done
}

case $1 in
"run")
run
;;
"build")
build
;;
"pull")
pull
;;
"push")
push
;;
"pull_run")
pull_run
;;
"build_push")
build_push
;;
"deploy")
deploy
;;
"notify")
notify "$@"
;;
esac

0 comments on commit 0b1e633

Please sign in to comment.