From ffd21e62c9ed59882dbd9a2e044c3002a878b573 Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Sun, 17 Mar 2024 13:32:45 +0800 Subject: [PATCH] feat: add action workflows --- .github/workflows/container.yml | 34 +++++++++++++++++++++++++++++++++ Dockerfile | 19 ++++++++++-------- build_image.sh | 22 ++++++++++++++++++--- 3 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/container.yml diff --git a/.github/workflows/container.yml b/.github/workflows/container.yml new file mode 100644 index 0000000..1ece1b0 --- /dev/null +++ b/.github/workflows/container.yml @@ -0,0 +1,34 @@ +name: Build Container Image +on: [ push ] +jobs: + image: + runs-on: ubuntu-latest + env: + DOCKER: podman + IMAGE_PREFIX: quay.io/ldcraft + SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} + steps: + - uses: actions/checkout@v4 + - name: Upgrade Podman + uses: gacts/install-podman@v1 + # reference: https://github.com/containers/podman/discussions/17868 + - name: Tar as root + run: | + sudo mv -fv /usr/bin/tar /usr/bin/tar.orig + echo -e '#!/bin/sh\n\nsudo /usr/bin/tar.orig "$@"' | sudo tee -a /usr/bin/tar + sudo chmod +x /usr/bin/tar + - name: Cache Podman + uses: actions/cache@v4 + with: + path: | + ~/.local/share/containers + ~/.config/containers + key: ${{ runner.os }}-${{ hashFiles('**/*.Dockerfile', 'build_image.sh') }} + - name: Login to Container Registry + uses: redhat-actions/podman-login@v1 + with: + registry: quay.io + username: ${{ secrets.CONTAINER_USERNAME }} + password: ${{ secrets.CONTAINER_PASSWORD }} + - name: Build UI Image + run: ./build_image.sh rootfs diff --git a/Dockerfile b/Dockerfile index ed469bf..84feeb5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,9 @@ -# Builder -FROM docker.io/library/node:slim AS builder +# Base Env +FROM docker.io/library/node:slim AS base -RUN apt-get update && apt-get install -y --no-install-recommends \ - ca-certificates \ - curl \ - git \ - gnupg \ +RUN --mount=type=cache,target=/var/cache/apt \ + apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates curl git gnupg \ && rm -rf /var/lib/apt/lists/* ENV PNPM_HOME="/pnpm" @@ -13,11 +11,16 @@ ENV PATH="$PNPM_HOME:$PATH" RUN corepack enable WORKDIR /builder +# Builder +FROM base AS builder + COPY package.json /builder/package.json COPY pnpm-lock.yaml /builder/pnpm-lock.yaml -RUN pnpm install --frozen-lockfile +RUN --mount=type=cache,id=pnpm,target=/pnpm/store \ + pnpm install --frozen-lockfile COPY . /builder +RUN pnpm install --frozen-lockfile RUN pnpm run build # Main diff --git a/build_image.sh b/build_image.sh index 491903b..a31de35 100755 --- a/build_image.sh +++ b/build_image.sh @@ -1,10 +1,26 @@ #!/usr/bin/env bash +COLOR_RED="\e[0;31m" +COLOR_GREEN="\e[0;32m" +COLOR_NONE="\e[0m" + +function log_info() { echo -e "${COLOR_GREEN}$*${COLOR_NONE}" 1>&2; } +function log_error() { echo -e "${COLOR_RED}$*${COLOR_NONE}" 1>&2; } + DOCKER="${DOCKER:-podman}" VERSION=$(jq -r .version < package.json) IMAGE_PREFIX=${IMAGE_PREFIX:-"git.0x7f.app/woj"} -set -x +if [ -f ".env.sentry-build-plugin" ]; then source .env.sentry-build-plugin; fi +if [ -z "$SENTRY_AUTH_TOKEN" ]; then log_error "SENTRY_AUTH_TOKEN not found!"; exit 1; fi -$DOCKER build -t "${IMAGE_PREFIX}/woj-ui:${VERSION}" . -$DOCKER push "${IMAGE_PREFIX}/woj-ui:${VERSION}" +log_info "=== Configuration ===" +log_info "DOCKER: ${DOCKER}" +log_info "IMAGE: ${IMAGE_PREFIX}/woj-ui:${VERSION}" +log_info "SENTRY_AUTH_TOKEN: $(echo "$SENTRY_AUTH_TOKEN" | head -c 12)***" + +log_info "=== Build ===" +$DOCKER build -t "${IMAGE_PREFIX}/woj-ui:${VERSION}" --env "SENTRY_AUTH_TOKEN=${SENTRY_AUTH_TOKEN}" . || { log_error "Build image failed!"; exit 1; } + +log_info "=== Push ===" +$DOCKER push "${IMAGE_PREFIX}/woj-ui:${VERSION}" || { log_error "Push image failed"; exit 1; }