最近写了几篇文章,想对自己写的每篇文档做版本控制。想到Github,把所有文档备份到Github。我不想每次都手动运行hugocompile和release脚本,所以我有以下GithubActions工作流程。使用GitHubActions自动部署博客文章。Repository首先,我们在Github上创建一个仓库来存放博客数据。静态博客推荐使用Hugo,非常简单。使用Markdown语法,官方提供了很多不错的主题模板。这里我使用trunk主分支来存放项目的代码,另外创建一个单独的分支来存放编译好的静态HTML资源文件。我们换个分支看看:在工作面板中新建一个工作板来自定义状态,在这里简单记录下每一个博客优化需求。这里的每一个任务都可以转化为一个issue,提交的代码可以关联到对应的issue。使用Github操作作为构建的CI/CD集成和发布。设置在主分支提交代码构建。name:githubpageson:push:branches:[main]查看主分支代码,删除pages-git分支(该分支存放静态文件,需要经常更新)。工作:部署:运行:ubuntu-18.04步骤:-使用:actions/checkout@v2with:submodules:true#FetchHugothemes(trueORrecursive)fetch-depth:0#Fetchallhistoryfor.GitInfoand.Lastmod-name:Deletebranchuses:dawidd6/action-delete-branch@v3with:github_token:${{secrets.GIT_TOKEN}}branches:"pages-git"hugo--minify在公共目录下生成静态文件。-name:SetupHugouses:peaceiris/actions-hugo@v2with:hugo-version:'0.74.2'#extended:true-name:Buildrun:hugo--minifylspublictarzcf${version}-public.tar.gzpublicls删除当前目录除All公共目录之外的文件,然后将公共目录文件移动到当前目录。-name:commitrun:|rm-frarchetypesrm-f??rcontentrm-frdemorm-frstaticrm-frthemesrm-f??r.DS_Storerm-fr1.1.0-public.tar.gzrm-frREADME.mdrm-frconfig.tolmvpublic/*./sleep3ls-lrm-frpublicgitconfig--globaluser.emailxxx@qq.comgitconfig--globaluser.nameccccgitadd.gitcommit-m"update"-a将当前工作目录提交到pages-git分支。(pages-git分支已经存储了更新后的静态文件)-name:Pushchangesuses:ad-m/github-push-action@masterwith:github_token:${{secrets.GIT_TOKEN}}branch:"pages-git"的访问到githubpages太慢了,打算用国内的giteepages服务。在gitee中创建一个仓库。最后一段代码是将当前pages-git分支代码镜像到gitee项目的pages-git分支。-name:'getcode'使用:actions/checkout@v2with:submodules:true#FetchHugothemes(trueORrecursive)fetch-depth:0#Fetchallhistoryfor.GitInfoand.Lastmodref:"pages-git"-name:'Mirrortogitee'使用:pixta-dev/repository-mirroring-action@v1with:target_repo_url:git@gitee.com:devopsgo/devopsgo.gitssh_private_key:${{secrets.GIT_PRIVATE_KEY}}至此代码已经同步到GiteePages了。接下来触发GiteePages服务更新。(指定静态目录分支部署)浏览器可以访问devopsgo.gitee.io。发布现已完成。工作流已经创建好了,最后每次发布博文。使用vscode编写markdown文档,然后本地hugoserve调试。完成后提交到主分支,GitHubActions会自动运行CI/CD并发布。扩展参考:SSH发布到云主机#-name:copyfileviasshpassword#uses:appleboy/scp-action@master#with:#host:${{secrets.SSH_HOST}}#username:${{secrets.SSH_USER}}#password:${{secrets.SSH_PASSWD}}#port:22#source:"./${{env.version}}-public.tar.gz"#target:"/opt/"#-name:executingremotesshcommandsusingpassword#uses:appleboy/ssh-action@master#with:#host:${{secrets.SSH_HOST}}#username:${{secrets.SSH_USER}}#password:${{secrets.SSH_PASSWD}}#port:22#script:|#rm-fr/var/www/newdevops/*#mv/opt/${{env.version}}-public.tar.gz/var/www/newdevops/#cd/var/www/newdevops/&&tarzxf${{env.version}}-public.tar.gz#mvpublic/*./&&rm-frpublic#chownnginx:nginx/var/www/-R#systemctlreloadnginx到这里,整个工作流程就完成了。希望能帮到你!
