Docker内推送文件及带文字GIF上传GitHub的凭证配置咨询
解决方案:Docker内生成可推送文件并上传GIF到GitHub仓库
一、配置GitHub凭证
在Docker容器内或本地环境,先完成Git基础配置:
git config --global user.name "你的GitHub用户名" git config --global user.email "你的GitHub绑定邮箱"
若用SSH免密推送,需将本地SSH密钥挂载到容器的/root/.ssh目录(或对应用户的.ssh目录),并将密钥文件权限设为600。
二、Docker环境内创建可推送文件
确保文件生成路径可被Git追踪
运行Docker容器时,挂载本地GitHub仓库目录到容器内:docker run -v /本地/github仓库路径:/app/repo -it 你的镜像名 bash调整代码的输出路径,让生成的GIF直接写入挂载的仓库目录,方便后续推送。
调整GIF生成代码
修改后的代码如下(指定输出到Git仓库目录):const TextOnGif = require('text-on-gif'); const path = require('path'); let req = 'test'; // 对应Docker挂载的仓库目录路径 const outputPath = path.join('/app/repo', 'gif-with-text.gif'); (async function () { let v = `https://media0.giphy.com/media/Ju7l5y9osyymQ/giphy.gif`; console.log(`${v}`); var gif = new TextOnGif({ file_path: v, }); gif.font_color = 'white'; gif.font_size = `${25+(2.12*req.length)}px`; gif.stroke_color = 'black'; gif.stroke_width = 5; await gif.textOnGif({ text: `${req}`, get_as_buffer: false, write_path: outputPath }); console.log(`GIF已生成到:${outputPath}`); })();
三、上传GIF到GitHub仓库
方式1:Git命令行实现
生成GIF后,在容器内执行以下命令:
cd /app/repo git add gif-with-text.gif git commit -m "Add generated GIF with text: test" git push origin main # 分支名根据仓库实际主分支调整,如master
可将这些命令写入Shell脚本,和Node.js脚本联动执行。
方式2:Node.js代码内完成推送
先安装simple-git库:
npm install simple-git
在原代码末尾添加推送逻辑:
const simpleGit = require('simple-git'); const git = simpleGit('/app/repo'); async function pushToGitHub() { try { await git.add('gif-with-text.gif'); await git.commit(`Add generated GIF with text: ${req}`); await git.push('origin', 'main'); console.log('GIF已成功推送到GitHub仓库'); } catch (err) { console.error('推送失败:', err); } } // 生成GIF后执行推送 await pushToGitHub();
四、Docker镜像配置(可选)
若要打包整个流程到镜像,Dockerfile示例:
FROM node:18-alpine # 安装Git依赖 RUN apk add --no-cache git WORKDIR /app COPY package*.json ./ RUN npm install COPY . . # 可在此提前配置Git凭证,或运行容器时动态设置 # RUN git config --global user.name "你的用户名" # RUN git config --global user.email "你的邮箱" CMD ["node", "你的脚本文件名.js"]
内容的提问来源于stack exchange,提问作者Cost
相关产品推荐
相关产品推荐

