docs: deploy README #14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Deploy to AWS Dev | |
on: | |
push: | |
branches: ["dev", "feature/**", "fix/**", "main"] | |
workflow_dispatch: | |
inputs: | |
image_tag: | |
description: "镜像版本号" | |
required: false | |
default: "v1.0.0" | |
registry: | |
description: "镜像仓库地址(不填则不构建镜像)" | |
required: false | |
default: "" | |
deploy: | |
description: "是否部署" | |
required: false | |
type: boolean | |
default: false | |
gateway: | |
description: "是否构建gateway镜像" | |
required: false | |
type: boolean | |
default: false | |
user: | |
description: "是否构建user镜像" | |
required: false | |
type: boolean | |
default: false | |
env: | |
IMAGE_TAG: ${{ inputs.image_tag }} | |
REGISTRY: ${{ inputs.registry }} | |
DEPLOY: ${{ inputs.deploy }} | |
jobs: | |
setup-build-publish-deploy: | |
name: Setup, Build, Publish, and Deploy | |
runs-on: ubuntu-latest | |
permissions: | |
contents: "read" | |
id-token: "write" | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Get commit message | |
id: commit | |
run: |- | |
commit=$(git log --pretty=format:"%B" ${{ github.sha }} -1 | head -1) | |
commit=${commit//"/\\\\\""} | |
servicesstring=$(git log --pretty=format:"%B" ${{ github.sha }} -1 | head -1 | grep ": \[.*\]" -o || true) | |
servicelist="" | |
if [ "${{ inputs.user }}" = "true" ] ; then | |
servicelist=${servicelist},user | |
fi | |
if [ "${{ inputs.gateway }}" = "true" ] ; then | |
servicelist=${servicelist},gateway | |
fi | |
if [ -z "$servicelist" ] ; then | |
if [ ! -z "$servicesstring" ] ; then | |
servicelist=${servicesstring:3:-1} | |
fi | |
fi | |
echo "servicelist=$servicelist" >> $GITHUB_OUTPUT | |
- name: Setup Go | |
uses: actions/setup-go@v5 | |
if: steps.commit.outputs.servicelist != '' | |
with: | |
go-version-file: "go.mod" | |
cache-dependency-path: "**/*.sum" | |
- name: Build, tag, and push docker image | |
id: build | |
if: steps.commit.outputs.servicelist != '' | |
run: |- | |
build_time=$(TZ='Asia/Shanghai' date '+%Y-%m-%d %H:%M:%S') | |
rpclist=$(find rpc -mindepth 1 -maxdepth 1 -type d ! -name model -exec basename {} \;| paste -sd, -) | |
apilist=$(find api -mindepth 1 -maxdepth 1 -type d -exec basename {} \;| paste -sd, -) | |
if [ "${{ steps.commit.outputs.servicelist }}" = "*" ] ; then | |
servicelist=${rpclist},${apilist} | |
else | |
servicelist=${{ steps.commit.outputs.servicelist }} | |
fi | |
echo "servicelist=$servicelist" | |
for element in $(awk -F ',' '{ for (i=1; i<=NF; i++) print $i }' <<< $servicelist );do | |
if echo $rpclist | grep "$element" ; then | |
service_type="rpc" | |
elif echo $apilist | grep "$element" ; then | |
service_type="api" | |
else | |
servicelist=$element | |
break | |
fi | |
if ! (CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o $element $service_type/$element/$element.go) then | |
echo "build $element failed" | |
exit 1 | |
fi | |
if [ -z "${{ env.REGISTRY }}" ] ; then | |
echo "REGISTRY is not set, skip build and push image" | |
exit 0 | |
fi | |
if ! ( | |
docker build --tag "${{ env.REGISTRY }}/service/$element:${{ env.IMAGE_TAG }}" --build-arg GITHUB_SHA="$GITHUB_SHA" --build-arg GITHUB_REF="$GITHUB_REF" -f $service_type/$element/Dockerfile . && \ | |
docker push "${{ env.REGISTRY }}/service/$element:${{ env.IMAGE_TAG }}" && \ | |
echo deploy $element; | |
) then | |
servicelist=$element | |
exit 1 | |
fi | |
buildlist=${buildlist},${element} | |
done | |
echo "buildlist=$buildlist" >> $GITHUB_OUTPUT | |
# https://github.com/appleboy/ssh-action | |
- name: Deploy to server | |
if: steps.commit.outputs.servicelist != '' && env.DEPLOY == 'true' | |
uses: appleboy/ssh-action@v1 | |
with: | |
# host: ${{ secrets.HOST }} | |
# username: ${{ secrets.USERNAME }} | |
# key: ${{ secrets.KEY }} | |
# port: ${{ secrets.PORT }} | |
script: | | |
# TODO |