From c3fda4c8b541e538a53cdc12a07d7d0940cc5c05 Mon Sep 17 00:00:00 2001 From: chenwj113 Date: Sun, 2 Jun 2024 12:01:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Egitea=20action?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/build_app.yaml | 39 +++++++++++++++++++++++++++++ .gitea/workflows/build_runtime.yaml | 36 ++++++++++++++++++++++++++ .gitlab-ci.yml | 2 +- Dockerfile | 29 ++++----------------- RuntimeDockerfile | 31 +++++++++++++++++++++++ 5 files changed, 112 insertions(+), 25 deletions(-) create mode 100644 .gitea/workflows/build_app.yaml create mode 100644 .gitea/workflows/build_runtime.yaml create mode 100644 RuntimeDockerfile diff --git a/.gitea/workflows/build_app.yaml b/.gitea/workflows/build_app.yaml new file mode 100644 index 0000000..d6c7199 --- /dev/null +++ b/.gitea/workflows/build_app.yaml @@ -0,0 +1,39 @@ +name: Gitea Action for docker build +run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 +on: + push: + branches: [master] + paths-ignore: + - 'requirements.txt' + - 'RuntimeDockerfile' + - '.gitignore' + - 'README.md' + - '.gitea/workflows/**' + + +jobs: + build-app: + runs-on: ubuntu-latest + steps: + - run: echo "This job is now running on a ${{ runner.os }} server hosted by Gitea!" + - name: Check out repository code + uses: https://git.airpig.cn/actions/checkout@v4 + - name: Login to hub.airpig.cn + uses: https://git.airpig.cn/actions/login-action@v3 + with: + registry: hub.airpig.cn + username: admin + password: Chenweijia113! + - run: | + var=${{ gitea.repository }} + repo=${var##*/} + url="https://hub.airpig.cn/api/v2.0/projects/library/repositories/${repo}/artifacts?page=1&page_size=10&with_tag=true&with_label=false&with_scan_overview=false&with_signature=false&with_immutable_status=false&with_accessory=false" + version=$(curl -X 'GET' $url \ + -H 'accept: application/json' \ + -H 'X-Accept-Vulnerabilities: application/vnd.security.vulnerability.report; version=1.1, application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0' \ + -u 'admin:Chenweijia!' | jq '.[0].tags[0].name') + new_version=$(echo $version | sed 's/\"//g' | awk -F. '{$NF = $NF + 1;} 1' OFS=.) + docker build -t hub.airpig.cn/library/$repo:$new_version . + docker push hub.airpig.cn/library/$repo:$new_version + docker rmi hub.airpig.cn/library/$repo:$new_version + - run: echo "This job's status is ${{ job.status }}." \ No newline at end of file diff --git a/.gitea/workflows/build_runtime.yaml b/.gitea/workflows/build_runtime.yaml new file mode 100644 index 0000000..ecb87e1 --- /dev/null +++ b/.gitea/workflows/build_runtime.yaml @@ -0,0 +1,36 @@ +name: Gitea Action for docker build runtime +run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 +on: + push: + branches: [master] + paths: + - 'requirements.txt' + - 'RuntimeDockerfile' + +jobs: + build-runtime: + runs-on: ubuntu-latest + steps: + - run: echo "This job is now running on a ${{ runner.os }} server hosted by Gitea!" + - name: Check out repository code + uses: https://git.airpig.cn/actions/checkout@v4 + - name: Login to hub.airpig.cn + uses: https://git.airpig.cn/actions/login-action@v3 + with: + registry: hub.airpig.cn + username: admin + password: Chenweijia113! + - run: | + var=${{ gitea.repository }} + repo=${var##*/}"-runtime" + echo $repo + url="https://hub.airpig.cn/api/v2.0/projects/library/repositories/${repo}/artifacts?page=1&page_size=10&with_tag=true&with_label=false&with_scan_overview=false&with_signature=false&with_immutable_status=false&with_accessory=false" + version=$(curl -X 'GET' $url \ + -H 'accept: application/json' \ + -H 'X-Accept-Vulnerabilities: application/vnd.security.vulnerability.report; version=1.1, application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0' \ + -u 'admin:Chenweijia!' | jq '.[0].tags[0].name') + new_version=$(echo $version | sed 's/\"//g' | awk -F. '{$NF = $NF + 1;} 1' OFS=.) + docker build -t hub.airpig.cn/library/$repo:$new_version -f RuntimeDockerfile . + docker push hub.airpig.cn/library/$repo:$new_version + docker rmi hub.airpig.cn/library/$repo:$new_version + - run: echo "This job's status is ${{ job.status }}." \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6835404..1dfe533 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,6 @@ variables: PROJECT_NAME: fastapi_app_template - DOCKER_IMAGE_DOMAIN: 192.168.2.237:8088 + DOCKER_IMAGE_DOMAIN: hub.airpig.cn LATEST_VERSION: latest K8S_NS: default DEPLOYMENT_NAME: fastapi-app-template diff --git a/Dockerfile b/Dockerfile index 4739c3f..c686cbc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,10 @@ -# 安装依赖阶段 -FROM python:3.8.6-slim as build +FROM busybox -RUN mkdir /install +RUN mkdir -p /data /app -WORKDIR /install +WORKDIR /data -COPY requirements.txt . +COPY . /data -RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list +ENTRYPOINT [ "/bin/cp", "-r", "/data/*", "/app"] -RUN apt-get update \ - && apt-get install gcc -y \ - && apt-get clean - -RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ --prefix=/install - -# 应用启动 -FROM python:3.8.6-slim - -COPY --from=build /install /usr/local - -COPY . /app - -WORKDIR /app - -ENV FAST_API_ENV=dev - -CMD ["/usr/local/bin/uvicorn", "main:fast_api_app", "--reload", "--host", "0.0.0.0", "--port", "21021"] diff --git a/RuntimeDockerfile b/RuntimeDockerfile new file mode 100644 index 0000000..8539914 --- /dev/null +++ b/RuntimeDockerfile @@ -0,0 +1,31 @@ +# 安装依赖阶段 +FROM python:3.11-slim as build + +RUN mkdir /install + +WORKDIR /install + +COPY requirements.txt . + +RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list + +RUN apt-get update \ + && apt-get install gcc -y \ + && apt-get clean + +RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ --prefix=/install + +# 应用启动 +FROM python:3.11-slim + +COPY --from=build /install /usr/local + +RUN mkdir -p /app + +# COPY . /app + +WORKDIR /app + +ENV FAST_API_ENV=prod + +CMD ["/usr/local/bin/uvicorn", "main:fast_api_app", "--reload", "--host", "0.0.0.0", "--port", "80"]