diff --git a/.github/workflows/docker-main.yml b/.github/workflows/docker-main.yml index 4146b84..949c2c5 100644 --- a/.github/workflows/docker-main.yml +++ b/.github/workflows/docker-main.yml @@ -13,6 +13,45 @@ env: IMAGE_NAME: ${{ github.event.repository.name }} jobs: + build-base-docker: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/v') # 只在发布新版本时构建基础镜像 + steps: + - uses: actions/checkout@v3 + - name: Login to ${{ env.REGISTRY }} + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push base image + uses: docker/build-push-action@v6 + with: + context: . + file: Dockerfile.base + push: true + tags: ${{ env.REGISTRY }}/${{ github.actor }}/${{ env.IMAGE_NAME }}:base_main + + build-devel-docker: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/v') # 只在发布新版本时构建基础镜像 + steps: + - uses: actions/checkout@v3 + - name: Login to ${{ env.REGISTRY }} + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push base image + uses: docker/build-push-action@v6 + with: + context: . + file: Dockerfile.base + push: true + tags: ${{ env.REGISTRY }}/${{ github.actor }}/${{ env.IMAGE_NAME }}:devel_main + + docker: runs-on: ubuntu-latest steps: diff --git a/Dockerfile b/Dockerfile index 69e505b..d5694ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,16 +8,16 @@ # For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html # Make sure RUBY_VERSION matches the Ruby version in .ruby-version -ARG RUBY_VERSION=3.3.5 -FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base +# ARG RUBY_VERSION=3.3.5 +# FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base +ARG BASE_IMAGE=docker.io/songtianlun/today_ai_weather:base_main +FROM ${BASE_IMAGE} AS base # Rails app lives here WORKDIR /rails # Install base packages -RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 libpq5 redis-tools && \ - rm -rf /var/lib/apt/lists /var/cache/apt/archives +# Move to Base Dockerfile. # Set production environment ENV RAILS_ENV="production" \ @@ -26,21 +26,13 @@ ENV RAILS_ENV="production" \ BUNDLE_WITHOUT="development" # Throw-away build stage to reduce size of final image -FROM base AS build +# FROM base AS build +ARG DEVEl_IMAGE=docker.io/songtianlun/today_ai_weather:devel_main +FROM ${DEVEL_IMAGE} AS build # Install packages needed to build gems and node modules -RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y build-essential git node-gyp pkg-config python-is-python3 libpq-dev && \ - rm -rf /var/lib/apt/lists /var/cache/apt/archives - # Install JavaScript dependencies -ARG NODE_VERSION=20.17.0 -ARG YARN_VERSION=1.22.22 -ENV PATH=/usr/local/node/bin:$PATH -RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ - /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ - npm install -g yarn@$YARN_VERSION && \ - rm -rf /tmp/node-build-master +# Move to Devel Dockerfile. # Install application gems COPY Gemfile Gemfile.lock ./ diff --git a/Dockerfile.base b/Dockerfile.base new file mode 100644 index 0000000..25b4ac2 --- /dev/null +++ b/Dockerfile.base @@ -0,0 +1,8 @@ +ARG RUBY_VERSION=3.3.5 +# Dockerfile.base +FROM docker.io/library/ruby:$RUBY_VERSION-slim + +# Install base packages +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 libpq5 redis-tools && \ + rm -rf /var/lib/apt/lists /var/cache/apt/archives \ No newline at end of file diff --git a/Dockerfile.build b/Dockerfile.build new file mode 100644 index 0000000..db05382 --- /dev/null +++ b/Dockerfile.build @@ -0,0 +1,30 @@ +ARG BASE_IMAGE=docker.io/your-username/today_ai_weather-base:main +FROM ${BASE_IMAGE} AS base + +# Rails app lives here +WORKDIR /rails + +# Install base packages, Move to Base Dockerfile. + +# Set production environment +ENV RAILS_ENV="production" \ + BUNDLE_DEPLOYMENT="1" \ + BUNDLE_PATH="/usr/local/bundle" \ + BUNDLE_WITHOUT="development" + +# Throw-away build stage to reduce size of final image +FROM base AS build + +# Install packages needed to build gems and node modules +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y build-essential git node-gyp pkg-config python-is-python3 libpq-dev && \ + rm -rf /var/lib/apt/lists /var/cache/apt/archives + +# Install JavaScript dependencies +ARG NODE_VERSION=20.17.0 +ARG YARN_VERSION=1.22.22 +ENV PATH=/usr/local/node/bin:$PATH +RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ + /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ + npm install -g yarn@$YARN_VERSION && \ + rm -rf /tmp/node-build-master