From ef65994a82a82c3c3752440d39bfbba8cf40c7b2 Mon Sep 17 00:00:00 2001 From: imnyang Date: Tue, 20 May 2025 06:09:16 +0900 Subject: [PATCH] feat: Update Dockerfile and scripts for improved build process and cron job functionality --- .gitignore | 26 +++++++++++- Dockerfile | 35 ++++++++++++--- app/index.ts | 117 ++++++++++++++++++++++++++++----------------------- app/run.sh | 2 +- cron | 1 + 5 files changed, 119 insertions(+), 62 deletions(-) diff --git a/.gitignore b/.gitignore index 2eea525..c119010 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,25 @@ -.env \ No newline at end of file +.env +# General +.DS_Store +.AppleDouble +.LSOverride +Icon[] + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index fde585d..e59bf60 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,6 @@ -FROM oven/bun:alpine +FROM oven/bun:alpine as build LABEL maintainer="@imnya" -# Set timezone to Asia/Seoul -RUN apk add --no-cache tzdata \ - && cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime \ - && echo "Asia/Seoul" > /etc/timezone \ - && apk del tzdata - # Set the working directory COPY app /code/app COPY .env /code/app/.env @@ -19,11 +13,38 @@ RUN mkdir -p /code/app/temp # Install dependencies RUN bun install +# Build the project +RUN bun build index.ts --compile --minify --outfile ./run + +FROM oven/bun:alpine as runner +LABEL maintainer="@imnya" + +# Set the working directory +WORKDIR /code +RUN mkdir -p /code/app + +# Copy the built files from the build stage +COPY --from=build /code/app/run /code/app/run +COPY --from=build /code/app/template /code/app/template +COPY --from=build /code/app/.env /code/app/.env +COPY --from=build /code/app/cron /code/app/cron +COPY --from=build /code/app/run.sh /code/app/run.sh + +RUN mkdir -p /code/app/temp + +# Set timezone to Asia/Seoul +RUN apk add --no-cache tzdata \ + && cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime \ + && echo "Asia/Seoul" > /etc/timezone \ + && apk del tzdata + # Cron job RUN apk add --no-cache curl RUN curl -Lo /code/app/supercronic https://github.com/aptible/supercronic/releases/latest/download/supercronic-linux-amd64 \ && chmod +x /code/app/supercronic +RUN curl -o /code/app/temp/vts.xlsx https://f.imnya.ng/.today.isangjeong/vts.xlsx + COPY cron /code/app/cron RUN chmod +x /code/app/cron diff --git a/app/index.ts b/app/index.ts index 9f41391..50535cc 100644 --- a/app/index.ts +++ b/app/index.ts @@ -1,67 +1,78 @@ import { CreateImage } from "./lib/image"; import { Login, Upload } from "./lib/instagram"; -console.time("πŸŽ‰ | Done") +console.time("πŸŽ‰ | Done"); -console.time("πŸ”“ | Instagram login"); +async function main() { + console.time("πŸ”“ | Instagram login"); -await Login() - .then((data) => { + try { + const data = await Login(); console.log("✨ | Instagram login successful"); console.log(`πŸ€” | Login as ${data.currentUser.full_name}`); - }) - .catch((error) => { + } catch (error) { console.error("❌ | Instagram login failed:", error); - }) - .finally(() => { + } finally { console.timeEnd("πŸ”“ | Instagram login"); - }); + } + let YYMMDD = ""; -let YYMMDD = ""; + // μ‹€ν–‰ν•  λ•Œ --today μ˜΅μ…˜μ„ μ£Όλ©΄ 였늘 λ‚ μ§œλ‘œ λ°”λ€œ + const args = process.argv.slice(2); + if (args.includes("--today")) { + console.log("πŸ“… | --today option detected, using today's date"); + YYMMDD = new Date().toISOString().slice(0, 10).replace(/-/g, "").toString(); + console.log("πŸ“… | Using today's date:", YYMMDD); + } + // Tomorrow is 1st of the month + const tomorrow = new Date(); + tomorrow.setDate(tomorrow.getDate() + 1); + YYMMDD = tomorrow.toISOString().slice(0, 10).replace(/-/g, "").toString(); -// μ‹€ν–‰ν•  λ•Œ --today μ˜΅μ…˜μ„ μ£Όλ©΄ 였늘 λ‚ μ§œλ‘œ λ°”λ€œ -const args = process.argv.slice(2); -if (args.includes("--today")) { - console.log("πŸ“… | --today option detected, using today's date"); - YYMMDD = new Date().toISOString().slice(0, 10).replace(/-/g, "").toString(); - console.log("πŸ“… | Using today's date:", YYMMDD); + console.log("πŸ“… | date:", YYMMDD); + + console.log(tomorrow.getDate()); + if (tomorrow.getDate() === 1) { + console.log("πŸ“… | Tomorrow is the 1st of the month, fetching schedule..."); + console.time("πŸ“… | Create Post Schedule"); + await CreateImage.PostSchedule(); + console.timeEnd("πŸ“… | Create Post Schedule"); + + console.time("πŸ“€ | Upload Post Schedule"); + await Upload.Post( + `./temp/schedule-${new Date().getFullYear()}-${ + new Date().getMonth() + 1 + }.png`, + `#μΈμ²œμƒμ •μ€‘ν•™κ΅ #상정쀑학ꡐ #학사일정 \n${new Date().getFullYear()}년도 ${ + new Date().getMonth() + 1 + }μ›” 학사 일정` + ); + } + try { + console.time("πŸ“· | Create Post Image"); + await CreateImage.PostMeal(YYMMDD); + console.timeEnd("πŸ“· | Create Post Image"); + + console.time("πŸ“± | Create Story Image"); + await CreateImage.ConvertToStory(`./temp/${YYMMDD}.png`); + console.timeEnd("πŸ“± | Create Story Image"); + + console.time("πŸ“€ | Upload Post"); + await Upload.Post( + `./temp/${YYMMDD}.png`, + `#μΈμ²œμƒμ •μ€‘ν•™κ΅ #상정쀑학ꡐ #급식 \n${YYMMDD}일자 급식` + ); + console.timeEnd("πŸ“€ | Upload Post"); + + console.time("πŸ“€ | Upload Story"); + await Upload.Story(`./temp/${YYMMDD}-story.png`, YYMMDD); + console.timeEnd("πŸ“€ | Upload Story"); + } catch (error) { + console.error("❌ | Error during image creation or upload:", error); + } finally { + console.timeEnd("πŸŽ‰ | Done"); + } } -// Tomorrow is 1st of the month -const tomorrow = new Date(); -tomorrow.setDate(tomorrow.getDate() + 1); -YYMMDD = tomorrow.toISOString().slice(0, 10).replace(/-/g, "").toString(); -console.log("πŸ“… | date:", YYMMDD); - -console.log(tomorrow.getDate()); -if (tomorrow.getDate() === 1) { - console.log("πŸ“… | Tomorrow is the 1st of the month, fetching schedule..."); - console.time("πŸ“… | Create Post Schedule"); - await CreateImage.PostSchedule(); - console.timeEnd("πŸ“… | Create Post Schedule"); - - console.time("πŸ“€ | Upload Post Schedule"); - await Upload.Post(`./temp/schedule-${new Date().getFullYear()}-${new Date().getMonth() + 1}.png`, `#μΈμ²œμƒμ •μ€‘ν•™κ΅ #상정쀑학ꡐ #학사일정 \n${new Date().getFullYear()}년도 ${new Date().getMonth() + 1}μ›” 학사 일정`); -} -try { - console.time("πŸ“· | Create Post Image"); - await CreateImage.PostMeal(YYMMDD); - console.timeEnd("πŸ“· | Create Post Image"); - - console.time("πŸ“± | Create Story Image"); - await CreateImage.ConvertToStory(`./temp/${YYMMDD}.png`); - console.timeEnd("πŸ“± | Create Story Image"); - - console.time("πŸ“€ | Upload Post"); - await Upload.Post(`./temp/${YYMMDD}.png`, `#μΈμ²œμƒμ •μ€‘ν•™κ΅ #상정쀑학ꡐ #급식 \n${YYMMDD}일자 급식`); - console.timeEnd("πŸ“€ | Upload Post"); - - console.time("πŸ“€ | Upload Story"); - await Upload.Story(`./temp/${YYMMDD}-story.png`, YYMMDD); - console.timeEnd("πŸ“€ | Upload Story"); -} catch (error) { - console.error("❌ | Error during image creation or upload:", error); -} finally { - console.timeEnd("πŸŽ‰ | Done"); -} +main(); diff --git a/app/run.sh b/app/run.sh index 5865a9a..e2ddac2 100644 --- a/app/run.sh +++ b/app/run.sh @@ -1,3 +1,3 @@ cd /code/app -bun ./index.ts \ No newline at end of file +./run \ No newline at end of file diff --git a/cron b/cron index 915d68e..b2f6e7e 100644 --- a/cron +++ b/cron @@ -1 +1,2 @@ 0 22 * * * /code/app/run.sh >> /code/app/temp/run-$(date +%Y%m%d-%H%M%S).log 2>&1 +0 23 * * 0 curl -o /code/app/temp/vts.xlsx https://f.imnya.ng/.today.isangjeong/vts.xlsx \ No newline at end of file