From 0b1e6336e578f3b8f0c76b83f5c45244db9d08b5 Mon Sep 17 00:00:00 2001 From: zhengqi zhang <770166635@qq.com> Date: Tue, 23 Nov 2021 14:27:11 +0800 Subject: [PATCH] add deploy shell --- .github/workflows/build_deploy_manual.yml | 83 ++++++++++++++++++ docker/Dockerfile | 4 + docker/default.conf | 46 ++++++++++ docker/fluidex-web.sh | 100 ++++++++++++++++++++++ 4 files changed, 233 insertions(+) create mode 100644 .github/workflows/build_deploy_manual.yml create mode 100644 docker/Dockerfile create mode 100644 docker/default.conf create mode 100644 docker/fluidex-web.sh diff --git a/.github/workflows/build_deploy_manual.yml b/.github/workflows/build_deploy_manual.yml new file mode 100644 index 0000000..570328b --- /dev/null +++ b/.github/workflows/build_deploy_manual.yml @@ -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 diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..de249e4 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx + +ADD ./build/exchange /usr/share/nginx/html +ADD ./docker/default.conf /etc/nginx/conf.d/default.conf diff --git a/docker/default.conf b/docker/default.conf new file mode 100644 index 0000000..29f3fb8 --- /dev/null +++ b/docker/default.conf @@ -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; + #} +} diff --git a/docker/fluidex-web.sh b/docker/fluidex-web.sh new file mode 100644 index 0000000..24cc4c7 --- /dev/null +++ b/docker/fluidex-web.sh @@ -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