[{"data":1,"prerenderedAt":1384},["ShallowReactive",2],{"article-20210808-gitlab-cd-rsync-deployment":3},{"article":4,"surround":1373},{"id":5,"title":6,"authorKey":7,"body":8,"codeLinks":1330,"createdAt":1334,"description":1335,"excerpt":1336,"extension":1358,"image":1359,"meta":1360,"navigation":608,"path":1361,"published":1362,"readingTime":270,"revisedAt":1363,"seo":1364,"stem":1365,"tags":1366,"__hash__":1372},"articles\u002Farticles\u002F20210808-gitlab-cd-rsync-deployment.md","GitLab CI\u002FCD of a Nuxt.js frontend over SSH\u002Frsync","philip",{"type":9,"value":10,"toc":1321},"minimark",[11,38,53,58,61,111,115,281,304,310,335,346,350,373,378,391,429,435,450,459,505,519,541,547,567,587,961,993,1231,1240,1269,1280,1290,1298,1308,1314,1317],[12,13,14,15,19,20,23,24,31,32,37],"p",{},"Continuous deployment is great and a must for every modern web app. Forget about the times when you had to constantly log into your production server over SSH to run some ",[16,17,18],"code",{},"git pull"," based deployment and cumbersome and error-prone build tasks, or building your project locally and then deploying it with ",[16,21,22],{},"rsync"," to production, which is not that sexy either. It is all doable and scriptable, but we want to have the whole process automated without any manual work involved. I much prefer to use ",[25,26,30],"a",{"href":27,"rel":28},"https:\u002F\u002Fdocs.gitlab.com\u002Fee\u002Fci\u002F",[29],"nofollow","GitLab CI\u002FCD"," over ",[25,33,36],{"href":34,"rel":35},"https:\u002F\u002Fgithub.com\u002Ffeatures\u002Factions",[29],"GitHub Actions"," - ok, mainly just because I am more into it and prefer to run a self-hosted GitLab instance. GitLab just gets the job done very well!",[12,39,40,41,46,47,52],{},"We want the whole process to be straightforward without any fancy extras. It should be a matter of 15mins to set it up on every new project and I don't like to introduce any extra dependencies. I am just talking about deployment of a static site \u002F SPA, a ",[25,42,45],{"href":43,"rel":44},"https:\u002F\u002Fvuejs.org\u002F",[29],"Vue.js"," based frontend that is generated by ",[25,48,51],{"href":49,"rel":50},"https:\u002F\u002Fnuxtjs.org\u002F",[29],"Nuxt.js",". So let's keep it simple here! I am going to present you the solution I am using to deploy this TechBlog.",[54,55,57],"h2",{"id":56},"requirements","Requirements",[12,59,60],{},"OK, don't do it in the most simple way, we still have some basic requirements aside an apple a day (which keeps the doctor away):",[62,63,64,78,84,101],"ul",{},[65,66,67,68,74,75],"li",{},"Building should be done by ",[69,70,71],"strong",{},[25,72,30],{"href":27,"rel":73},[29]," in ",[69,76,77],{},"Docker executor",[65,79,80,81],{},"Generated static site is uploaded to production server via ",[69,82,83],{},"SSH\u002Frsync",[65,85,86,87,94,95,100],{},"We want ",[69,88,89,90,93],{},"basic versioning with ",[16,91,92],{},"current"," symlink",", keeping the last 5 releases. No fat dependencies like ",[25,96,99],{"href":97,"rel":98},"https:\u002F\u002Fcapistranorb.com\u002F",[29],"Capistrano",", please!",[65,102,103,110],{},[69,104,105,106,109],{},"Only deploy when pushed to the default (",[16,107,108],{},"main",") branch"," (or any other branch that fits your workflow, but don't deploy on every branch!)",[54,112,114],{"id":113},"manual-deployment","Manual Deployment",[116,117,118,139,174,188],"blockquote",{},[12,119,120,123,124,127,128,131,132,31,135,138],{},[69,121,122],{},"Remember:"," Building the application, generate every route as a HTML file and statically export to ",[16,125,126],{},"dist\u002F"," directory on a ",[25,129,51],{"href":49,"rel":130},[29]," project goes like this (I am preferring ",[16,133,134],{},"yarn",[16,136,137],{},"npm",", but basically the same):",[140,141,146],"pre",{"className":142,"code":143,"language":144,"meta":145,"style":145},"language-bash shiki shiki-themes github-light-default github-light-default github-dark-default","$ yarn install\n$ yarn generate\n","bash","",[16,147,148,164],{"__ignoreMap":145},[149,150,153,157,161],"span",{"class":151,"line":152},"line",1,[149,154,156],{"class":155},"sQJ0q","$",[149,158,160],{"class":159},"s-BGO"," yarn",[149,162,163],{"class":159}," install\n",[149,165,167,169,171],{"class":151,"line":166},2,[149,168,156],{"class":155},[149,170,160],{"class":159},[149,172,173],{"class":159}," generate\n",[12,175,176,179,180,183,184,187],{},[16,177,178],{},"yarn generate"," maps to ",[16,181,182],{},"nuxt generate",", as we have defined the following in ",[16,185,186],{},"package.json",":",[140,189,193],{"className":190,"code":191,"language":192,"meta":145,"style":145},"language-json shiki shiki-themes github-light-default github-light-default github-dark-default","{\n     \"scripts\": {\n       \"dev\": \"nuxt\",\n       \"build\": \"nuxt build\",\n       \"start\": \"nuxt start\",\n       \"generate\": \"nuxt generate\"\n     },\n     \u002F\u002F ...\n}\n","json",[16,194,195,201,210,225,238,251,262,268,275],{"__ignoreMap":145},[149,196,197],{"class":151,"line":152},[149,198,200],{"class":199},"s_bOF","{\n",[149,202,203,207],{"class":151,"line":166},[149,204,206],{"class":205},"sfEsd","     \"scripts\"",[149,208,209],{"class":199},": {\n",[149,211,213,216,219,222],{"class":151,"line":212},3,[149,214,215],{"class":205},"       \"dev\"",[149,217,218],{"class":199},": ",[149,220,221],{"class":159},"\"nuxt\"",[149,223,224],{"class":199},",\n",[149,226,228,231,233,236],{"class":151,"line":227},4,[149,229,230],{"class":205},"       \"build\"",[149,232,218],{"class":199},[149,234,235],{"class":159},"\"nuxt build\"",[149,237,224],{"class":199},[149,239,241,244,246,249],{"class":151,"line":240},5,[149,242,243],{"class":205},"       \"start\"",[149,245,218],{"class":199},[149,247,248],{"class":159},"\"nuxt start\"",[149,250,224],{"class":199},[149,252,254,257,259],{"class":151,"line":253},6,[149,255,256],{"class":205},"       \"generate\"",[149,258,218],{"class":199},[149,260,261],{"class":159},"\"nuxt generate\"\n",[149,263,265],{"class":151,"line":264},7,[149,266,267],{"class":199},"     },\n",[149,269,271],{"class":151,"line":270},8,[149,272,274],{"class":273},"s9BHP","     \u002F\u002F ...\n",[149,276,278],{"class":151,"line":277},9,[149,279,280],{"class":199},"}\n",[12,282,283,284,289,290,74,293,296,297,299,300,303],{},"If a local build does not yet work for you (maybe producing some HTML\u002FCSS minification errors...), fix it first locally and study ",[25,285,288],{"href":286,"rel":287},"https:\u002F\u002Fnuxtjs.org\u002Fdocs\u002F2.x\u002Fget-started\u002Fcommands",[29],"Nuxt.js Commands and Deployment",". In this example and on this TechBlog deployment, we are using ",[16,291,292],{},"target: 'static'",[16,294,295],{},"nuxt.config.js",", that's why we build the site with ",[16,298,182],{}," instead of ",[16,301,302],{},"nuxt build",".",[12,305,306,307,309],{},"On a manual deployment, you would then rsync the whole ",[16,308,126],{}," content to your production server, e.g.:",[140,311,313],{"className":142,"code":312,"language":144,"meta":145,"style":145},"$ rsync -aHv --delete dist\u002F user@example.com:public_html\u002Fblog\n",[16,314,315],{"__ignoreMap":145},[149,316,317,319,322,326,329,332],{"class":151,"line":152},[149,318,156],{"class":155},[149,320,321],{"class":159}," rsync",[149,323,325],{"class":324},"sDkKp"," -aHv",[149,327,328],{"class":324}," --delete",[149,330,331],{"class":159}," dist\u002F",[149,333,334],{"class":159}," user@example.com:public_html\u002Fblog\n",[12,336,337,338,341,342,345],{},"On production server, you would then point the site root to ",[16,339,340],{},"public_html\u002Fblog",", but with the automated CD solution below, you would point it to ",[16,343,344],{},"public_html\u002Fblog\u002Fcurrent",", as we're going to create a symlink to the current release.",[54,347,349],{"id":348},"gitlab-cicd-setup","GitLab CI\u002FCD Setup",[62,351,352,359,366],{},[65,353,354],{},[25,355,358],{"href":356,"rel":357},"https:\u002F\u002Fdocs.gitlab.com\u002Fee\u002Fci\u002Fssh_keys\u002F#ssh-keys-when-using-the-docker-executor",[29],"GitLab CI\u002FCD: SSH keys when using the Docker executor",[65,360,361],{},[25,362,365],{"href":363,"rel":364},"https:\u002F\u002Fdocs.gitlab.com\u002Fee\u002Fssh\u002Findex.html#generate-an-ssh-key-pair",[29],"GitLab: Generate an SSH key pair",[65,367,368],{},[25,369,372],{"href":370,"rel":371},"https:\u002F\u002Fdocs.gitlab.com\u002Fee\u002Fci\u002Fssh_keys\u002F#verifying-the-ssh-host-keys",[29],"GitLab: Verifying the SSH host keys",[374,375,377],"h3",{"id":376},"set-up-cicd-variables","Set up CI\u002FCD Variables",[12,379,380,381,384,385,387,388,187],{},"Building and deployment is done automatically via GitLab CI\u002FCD, once you have ",[16,382,383],{},".gitlab-ci.yml"," (see below) added to your project root. A deployment is always triggered when you push to ",[16,386,108],{}," branch. But first, set up those CI\u002FCD variables in your GitLab project under ",[69,389,390],{},"Settings > CI\u002FCD > Variables",[62,392,393,399,409,415],{},[65,394,395,398],{},[16,396,397],{},"DEPLOY_HOST"," (Variable): The production host we want to deploy to.",[65,400,401,404,405,408],{},[16,402,403],{},"SSH_KNOWN_HOSTS"," (Variable): ",[16,406,407],{},"~\u002F.ssh\u002Fknown_hosts"," lines on deployment container (Docker)",[65,410,411,414],{},[16,412,413],{},"SSH_USER"," (Variable): The user to connect to the production host.",[65,416,417,420,421,424,425,428],{},[16,418,419],{},"SSH_KEY"," (",[69,422,423],{},"File","): The deployment SSH private key (copy-paste the whole key starting with ",[16,426,427],{},"-----BEGIN OPENSSH PRIVATE KEY-----\\n...",").",[12,430,431,432,434],{},"To get the correct line(s) for ",[16,433,403],{}," variable, run this from a trusted host:",[140,436,438],{"className":142,"code":437,"language":144,"meta":145,"style":145},"$ ssh-keyscan DEPLOY_HOST\n",[16,439,440],{"__ignoreMap":145},[149,441,442,444,447],{"class":151,"line":152},[149,443,156],{"class":155},[149,445,446],{"class":159}," ssh-keyscan",[149,448,449],{"class":159}," DEPLOY_HOST\n",[12,451,452,453,458],{},"Initially, you create a SSH keypair as follows (ED25519 is the first-preference signature algorithm since ",[25,454,457],{"href":455,"rel":456},"https:\u002F\u002Fwww.openssh.com\u002Ftxt\u002Frelease-8.5",[29],"OpenSSH 8.5"," and has been supported since OpenSSH 6.5):",[140,460,462],{"className":142,"code":461,"language":144,"meta":145,"style":145},"$ ssh-keygen -t ed25519 -f ~\u002F.ssh\u002Fdeploy_id_ed25519 -N \"\" -C \"gitlab-ci-deployer\"\n$ cat ~\u002F.ssh\u002Fdeploy_id_ed25519.pub\n",[16,463,464,495],{"__ignoreMap":145},[149,465,466,468,471,474,477,480,483,486,489,492],{"class":151,"line":152},[149,467,156],{"class":155},[149,469,470],{"class":159}," ssh-keygen",[149,472,473],{"class":324}," -t",[149,475,476],{"class":159}," ed25519",[149,478,479],{"class":324}," -f",[149,481,482],{"class":159}," ~\u002F.ssh\u002Fdeploy_id_ed25519",[149,484,485],{"class":324}," -N",[149,487,488],{"class":159}," \"\"",[149,490,491],{"class":324}," -C",[149,493,494],{"class":159}," \"gitlab-ci-deployer\"\n",[149,496,497,499,502],{"class":151,"line":166},[149,498,156],{"class":155},[149,500,501],{"class":159}," cat",[149,503,504],{"class":159}," ~\u002F.ssh\u002Fdeploy_id_ed25519.pub\n",[12,506,507,508,511,512,515,516,187],{},"Don't store the private key anywhere else than in ",[16,509,510],{},"$SSH_KEY","! Copy the public key (",[16,513,514],{},"deploy_id_ed25519.pub",") to the deploy host's ",[16,517,518],{},"authorized_keys",[140,520,522],{"className":142,"code":521,"language":144,"meta":145,"style":145},"$ echo \"ssh-ed25519 AAAAC3N... gitlab-ci-deployer\" >> ~\u002F.ssh\u002Fauthorized_keys\n",[16,523,524],{"__ignoreMap":145},[149,525,526,528,531,534,538],{"class":151,"line":152},[149,527,156],{"class":155},[149,529,530],{"class":159}," echo",[149,532,533],{"class":159}," \"ssh-ed25519 AAAAC3N... gitlab-ci-deployer\"",[149,535,537],{"class":536},"s5nWI"," >>",[149,539,540],{"class":159}," ~\u002F.ssh\u002Fauthorized_keys\n",[374,542,544,545],{"id":543},"create-gitlab-ciyml","Create ",[16,546,383],{},[12,548,549,550,555,556,559,560,563,564,566],{},"Ready for the real show? We're using official ",[25,551,554],{"href":552,"rel":553},"https:\u002F\u002Fhub.docker.com\u002F_\u002Fnode",[29],"node Docker Image",". ",[16,557,558],{},"node:current"," is based on Debian Buster (as of Aug 2021), so we can also use ",[16,561,562],{},"apt-get"," inside the container to install our extras, which is actually just ",[16,565,22],{},", nothing more.",[12,568,569,570,572,573,575,576,578,579,582,583,586],{},"Create the following ",[16,571,383],{}," in your project root. It will use the private SSH key which you have provided in ",[16,574,419],{}," GitLab CI\u002FCD Variables as a ",[16,577,423],{}," (NOT as a ",[16,580,581],{},"Variable","!), so you don't need to know about the actual location of the key, just use ",[16,584,585],{},"ssh -i ${SSH_KEY}"," in the deploy commands.",[140,588,592],{"className":589,"code":590,"filename":383,"language":591,"meta":145,"style":145},"language-yaml shiki shiki-themes github-light-default github-light-default github-dark-default","image: node:current\n\nvariables:\n  # In case you did not set up $SSH_KNOWN_HOSTS variable:\n  # SSH_OPTS: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=\u002Fdev\u002Fnull'\n  SSH_OPTS: ''\n  BASE_DIR: public_html\u002Fblog\n  KEEP: 5\n\ndefault:\n  before_script:\n    - mkdir -p ~\u002F.ssh && chmod 700 ~\u002F.ssh\n    - echo \"$SSH_KNOWN_HOSTS\" > ~\u002F.ssh\u002Fknown_hosts\n    - chmod 644 ~\u002F.ssh\u002Fknown_hosts\n    - chmod 600 ${SSH_KEY}\n    - export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)\n\ncache:\n  paths:\n    - node_modules\n\nbuild:\n  stage: build\n  script:\n    - cp .env.prod .env\n    - yarn install\n    - yarn generate\n  artifacts:\n    paths:\n      - dist\n  rules:\n    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH\n    - if: $RUN_ALWAYS\n\ndeploy:\n  stage: deploy\n  script:\n    - apt-get update -y && apt-get install -y rsync\n    - ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && test ! -h current || cp -r \\$(ls -td *\u002F | grep -v current | head -n1) deploy_tmp\"\n    - rsync -aHv --delete -e \"ssh -i ${SSH_KEY} ${SSH_OPTS}\" dist\u002F ${SSH_USER}@${DEPLOY_HOST}:${BASE_DIR}\u002Fdeploy_tmp\n    - ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && mv deploy_tmp ${COMMIT_TIME} && rm -f current && ln -s ${COMMIT_TIME} current\"\n    - ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && rm -rf \\$(ls -dt *\u002F | grep -v current | tail -n +$(($KEEP+1)))\"\n  rules:\n    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH\n    - if: $RUN_ALWAYS\n","yaml",[16,593,594,604,610,618,623,628,638,648,658,662,670,678,687,695,703,711,719,724,732,740,748,753,761,772,780,788,796,804,812,820,829,837,850,862,867,875,885,892,900,908,916,924,932,939,950],{"__ignoreMap":145},[149,595,596,599,601],{"class":151,"line":152},[149,597,598],{"class":205},"image",[149,600,218],{"class":199},[149,602,603],{"class":159},"node:current\n",[149,605,606],{"class":151,"line":166},[149,607,609],{"emptyLinePlaceholder":608},true,"\n",[149,611,612,615],{"class":151,"line":212},[149,613,614],{"class":205},"variables",[149,616,617],{"class":199},":\n",[149,619,620],{"class":151,"line":227},[149,621,622],{"class":273},"  # In case you did not set up $SSH_KNOWN_HOSTS variable:\n",[149,624,625],{"class":151,"line":240},[149,626,627],{"class":273},"  # SSH_OPTS: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=\u002Fdev\u002Fnull'\n",[149,629,630,633,635],{"class":151,"line":253},[149,631,632],{"class":205},"  SSH_OPTS",[149,634,218],{"class":199},[149,636,637],{"class":159},"''\n",[149,639,640,643,645],{"class":151,"line":264},[149,641,642],{"class":205},"  BASE_DIR",[149,644,218],{"class":199},[149,646,647],{"class":159},"public_html\u002Fblog\n",[149,649,650,653,655],{"class":151,"line":270},[149,651,652],{"class":205},"  KEEP",[149,654,218],{"class":199},[149,656,657],{"class":324},"5\n",[149,659,660],{"class":151,"line":277},[149,661,609],{"emptyLinePlaceholder":608},[149,663,665,668],{"class":151,"line":664},10,[149,666,667],{"class":205},"default",[149,669,617],{"class":199},[149,671,673,676],{"class":151,"line":672},11,[149,674,675],{"class":205},"  before_script",[149,677,617],{"class":199},[149,679,681,684],{"class":151,"line":680},12,[149,682,683],{"class":199},"    - ",[149,685,686],{"class":159},"mkdir -p ~\u002F.ssh && chmod 700 ~\u002F.ssh\n",[149,688,690,692],{"class":151,"line":689},13,[149,691,683],{"class":199},[149,693,694],{"class":159},"echo \"$SSH_KNOWN_HOSTS\" > ~\u002F.ssh\u002Fknown_hosts\n",[149,696,698,700],{"class":151,"line":697},14,[149,699,683],{"class":199},[149,701,702],{"class":159},"chmod 644 ~\u002F.ssh\u002Fknown_hosts\n",[149,704,706,708],{"class":151,"line":705},15,[149,707,683],{"class":199},[149,709,710],{"class":159},"chmod 600 ${SSH_KEY}\n",[149,712,714,716],{"class":151,"line":713},16,[149,715,683],{"class":199},[149,717,718],{"class":159},"export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)\n",[149,720,722],{"class":151,"line":721},17,[149,723,609],{"emptyLinePlaceholder":608},[149,725,727,730],{"class":151,"line":726},18,[149,728,729],{"class":205},"cache",[149,731,617],{"class":199},[149,733,735,738],{"class":151,"line":734},19,[149,736,737],{"class":205},"  paths",[149,739,617],{"class":199},[149,741,743,745],{"class":151,"line":742},20,[149,744,683],{"class":199},[149,746,747],{"class":159},"node_modules\n",[149,749,751],{"class":151,"line":750},21,[149,752,609],{"emptyLinePlaceholder":608},[149,754,756,759],{"class":151,"line":755},22,[149,757,758],{"class":205},"build",[149,760,617],{"class":199},[149,762,764,767,769],{"class":151,"line":763},23,[149,765,766],{"class":205},"  stage",[149,768,218],{"class":199},[149,770,771],{"class":159},"build\n",[149,773,775,778],{"class":151,"line":774},24,[149,776,777],{"class":205},"  script",[149,779,617],{"class":199},[149,781,783,785],{"class":151,"line":782},25,[149,784,683],{"class":199},[149,786,787],{"class":159},"cp .env.prod .env\n",[149,789,791,793],{"class":151,"line":790},26,[149,792,683],{"class":199},[149,794,795],{"class":159},"yarn install\n",[149,797,799,801],{"class":151,"line":798},27,[149,800,683],{"class":199},[149,802,803],{"class":159},"yarn generate\n",[149,805,807,810],{"class":151,"line":806},28,[149,808,809],{"class":205},"  artifacts",[149,811,617],{"class":199},[149,813,815,818],{"class":151,"line":814},29,[149,816,817],{"class":205},"    paths",[149,819,617],{"class":199},[149,821,823,826],{"class":151,"line":822},30,[149,824,825],{"class":199},"      - ",[149,827,828],{"class":159},"dist\n",[149,830,832,835],{"class":151,"line":831},31,[149,833,834],{"class":205},"  rules",[149,836,617],{"class":199},[149,838,840,842,845,847],{"class":151,"line":839},32,[149,841,683],{"class":199},[149,843,844],{"class":205},"if",[149,846,218],{"class":199},[149,848,849],{"class":159},"$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH\n",[149,851,853,855,857,859],{"class":151,"line":852},33,[149,854,683],{"class":199},[149,856,844],{"class":205},[149,858,218],{"class":199},[149,860,861],{"class":159},"$RUN_ALWAYS\n",[149,863,865],{"class":151,"line":864},34,[149,866,609],{"emptyLinePlaceholder":608},[149,868,870,873],{"class":151,"line":869},35,[149,871,872],{"class":205},"deploy",[149,874,617],{"class":199},[149,876,878,880,882],{"class":151,"line":877},36,[149,879,766],{"class":205},[149,881,218],{"class":199},[149,883,884],{"class":159},"deploy\n",[149,886,888,890],{"class":151,"line":887},37,[149,889,777],{"class":205},[149,891,617],{"class":199},[149,893,895,897],{"class":151,"line":894},38,[149,896,683],{"class":199},[149,898,899],{"class":159},"apt-get update -y && apt-get install -y rsync\n",[149,901,903,905],{"class":151,"line":902},39,[149,904,683],{"class":199},[149,906,907],{"class":159},"ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && test ! -h current || cp -r \\$(ls -td *\u002F | grep -v current | head -n1) deploy_tmp\"\n",[149,909,911,913],{"class":151,"line":910},40,[149,912,683],{"class":199},[149,914,915],{"class":159},"rsync -aHv --delete -e \"ssh -i ${SSH_KEY} ${SSH_OPTS}\" dist\u002F ${SSH_USER}@${DEPLOY_HOST}:${BASE_DIR}\u002Fdeploy_tmp\n",[149,917,919,921],{"class":151,"line":918},41,[149,920,683],{"class":199},[149,922,923],{"class":159},"ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && mv deploy_tmp ${COMMIT_TIME} && rm -f current && ln -s ${COMMIT_TIME} current\"\n",[149,925,927,929],{"class":151,"line":926},42,[149,928,683],{"class":199},[149,930,931],{"class":159},"ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && rm -rf \\$(ls -dt *\u002F | grep -v current | tail -n +$(($KEEP+1)))\"\n",[149,933,935,937],{"class":151,"line":934},43,[149,936,834],{"class":205},[149,938,617],{"class":199},[149,940,942,944,946,948],{"class":151,"line":941},44,[149,943,683],{"class":199},[149,945,844],{"class":205},[149,947,218],{"class":199},[149,949,849],{"class":159},[149,951,953,955,957,959],{"class":151,"line":952},45,[149,954,683],{"class":199},[149,956,844],{"class":205},[149,958,218],{"class":199},[149,960,861],{"class":159},[116,962,963],{},[12,964,965,968,969,971,972,975,976,978,979,982,983,985,986,988,989,992],{},[69,966,967],{},"UPDATE 2021-08-18:"," Above ",[16,970,383],{}," can be further optimized, so that we no longer need to provide ",[16,973,974],{},"$RUN_ALWAYS"," variable to force-trigger a pipeline run on a different branch than default (",[16,977,108],{}," \u002F ",[16,980,981],{},"master","). I have also combined ",[16,984,758],{}," and ",[16,987,872],{}," stages into a single one, for improved deployment speed, as we don't need to split this up for this simple project. Like this, ",[16,990,991],{},"dist"," artifacts are no longer needed:",[140,994,996],{"className":589,"code":995,"filename":383,"language":591,"meta":145,"style":145},"image: node:current\n\nvariables:\n  # In case you did not set up $SSH_KNOWN_HOSTS variable:\n  # SSH_OPTS: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=\u002Fdev\u002Fnull'\n  SSH_OPTS: ''\n  BASE_DIR: public_html\u002Fblog\n  KEEP: 5\n\ndefault:\n  before_script:\n    - mkdir -p ~\u002F.ssh && chmod 700 ~\u002F.ssh\n    - echo \"$SSH_KNOWN_HOSTS\" > ~\u002F.ssh\u002Fknown_hosts\n    - chmod 644 ~\u002F.ssh\u002Fknown_hosts\n    - chmod 600 ${SSH_KEY}\n    - export COMMIT_TIME=$(git show -s --format=%ct $CI_COMMIT_SHA)\n\ncache:\n  paths:\n    - node_modules\n\n# combined build and deploy step to speed it up\nbuild_deploy:\n  stage: deploy\n  script:\n    # build\n    - cp .env.prod .env\n    - yarn install\n    - yarn generate\n    # deploy\n    - apt-get update -y && apt-get install -y rsync\n    - ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && test ! -h current || cp -r \\$(ls -td *\u002F | grep -v current | head -n1) deploy_tmp\"\n    - rsync -aHv --delete -e \"ssh -i ${SSH_KEY} ${SSH_OPTS}\" dist\u002F ${SSH_USER}@${DEPLOY_HOST}:${BASE_DIR}\u002Fdeploy_tmp\n    - ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && mv deploy_tmp ${COMMIT_TIME} && rm -f current && ln -s ${COMMIT_TIME} current\"\n    - ssh -i ${SSH_KEY} ${SSH_OPTS} ${SSH_USER}@${DEPLOY_HOST} \"cd ${BASE_DIR} && rm -rf \\$(ls -dt *\u002F | grep -v current | tail -n +$(($KEEP+1)))\"\n  rules:\n    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH\n    - if: $CI_PIPELINE_SOURCE == \"web\"\n",[16,997,998,1006,1010,1016,1020,1024,1032,1040,1048,1052,1058,1064,1070,1076,1082,1088,1094,1098,1104,1110,1116,1120,1125,1132,1140,1146,1151,1157,1163,1169,1174,1180,1186,1192,1198,1204,1210,1220],{"__ignoreMap":145},[149,999,1000,1002,1004],{"class":151,"line":152},[149,1001,598],{"class":205},[149,1003,218],{"class":199},[149,1005,603],{"class":159},[149,1007,1008],{"class":151,"line":166},[149,1009,609],{"emptyLinePlaceholder":608},[149,1011,1012,1014],{"class":151,"line":212},[149,1013,614],{"class":205},[149,1015,617],{"class":199},[149,1017,1018],{"class":151,"line":227},[149,1019,622],{"class":273},[149,1021,1022],{"class":151,"line":240},[149,1023,627],{"class":273},[149,1025,1026,1028,1030],{"class":151,"line":253},[149,1027,632],{"class":205},[149,1029,218],{"class":199},[149,1031,637],{"class":159},[149,1033,1034,1036,1038],{"class":151,"line":264},[149,1035,642],{"class":205},[149,1037,218],{"class":199},[149,1039,647],{"class":159},[149,1041,1042,1044,1046],{"class":151,"line":270},[149,1043,652],{"class":205},[149,1045,218],{"class":199},[149,1047,657],{"class":324},[149,1049,1050],{"class":151,"line":277},[149,1051,609],{"emptyLinePlaceholder":608},[149,1053,1054,1056],{"class":151,"line":664},[149,1055,667],{"class":205},[149,1057,617],{"class":199},[149,1059,1060,1062],{"class":151,"line":672},[149,1061,675],{"class":205},[149,1063,617],{"class":199},[149,1065,1066,1068],{"class":151,"line":680},[149,1067,683],{"class":199},[149,1069,686],{"class":159},[149,1071,1072,1074],{"class":151,"line":689},[149,1073,683],{"class":199},[149,1075,694],{"class":159},[149,1077,1078,1080],{"class":151,"line":697},[149,1079,683],{"class":199},[149,1081,702],{"class":159},[149,1083,1084,1086],{"class":151,"line":705},[149,1085,683],{"class":199},[149,1087,710],{"class":159},[149,1089,1090,1092],{"class":151,"line":713},[149,1091,683],{"class":199},[149,1093,718],{"class":159},[149,1095,1096],{"class":151,"line":721},[149,1097,609],{"emptyLinePlaceholder":608},[149,1099,1100,1102],{"class":151,"line":726},[149,1101,729],{"class":205},[149,1103,617],{"class":199},[149,1105,1106,1108],{"class":151,"line":734},[149,1107,737],{"class":205},[149,1109,617],{"class":199},[149,1111,1112,1114],{"class":151,"line":742},[149,1113,683],{"class":199},[149,1115,747],{"class":159},[149,1117,1118],{"class":151,"line":750},[149,1119,609],{"emptyLinePlaceholder":608},[149,1121,1122],{"class":151,"line":755},[149,1123,1124],{"class":273},"# combined build and deploy step to speed it up\n",[149,1126,1127,1130],{"class":151,"line":763},[149,1128,1129],{"class":205},"build_deploy",[149,1131,617],{"class":199},[149,1133,1134,1136,1138],{"class":151,"line":774},[149,1135,766],{"class":205},[149,1137,218],{"class":199},[149,1139,884],{"class":159},[149,1141,1142,1144],{"class":151,"line":782},[149,1143,777],{"class":205},[149,1145,617],{"class":199},[149,1147,1148],{"class":151,"line":790},[149,1149,1150],{"class":273},"    # build\n",[149,1152,1153,1155],{"class":151,"line":798},[149,1154,683],{"class":199},[149,1156,787],{"class":159},[149,1158,1159,1161],{"class":151,"line":806},[149,1160,683],{"class":199},[149,1162,795],{"class":159},[149,1164,1165,1167],{"class":151,"line":814},[149,1166,683],{"class":199},[149,1168,803],{"class":159},[149,1170,1171],{"class":151,"line":822},[149,1172,1173],{"class":273},"    # deploy\n",[149,1175,1176,1178],{"class":151,"line":831},[149,1177,683],{"class":199},[149,1179,899],{"class":159},[149,1181,1182,1184],{"class":151,"line":839},[149,1183,683],{"class":199},[149,1185,907],{"class":159},[149,1187,1188,1190],{"class":151,"line":852},[149,1189,683],{"class":199},[149,1191,915],{"class":159},[149,1193,1194,1196],{"class":151,"line":864},[149,1195,683],{"class":199},[149,1197,923],{"class":159},[149,1199,1200,1202],{"class":151,"line":869},[149,1201,683],{"class":199},[149,1203,931],{"class":159},[149,1205,1206,1208],{"class":151,"line":877},[149,1207,834],{"class":205},[149,1209,617],{"class":199},[149,1211,1212,1214,1216,1218],{"class":151,"line":887},[149,1213,683],{"class":199},[149,1215,844],{"class":205},[149,1217,218],{"class":199},[149,1219,849],{"class":159},[149,1221,1222,1224,1226,1228],{"class":151,"line":894},[149,1223,683],{"class":199},[149,1225,844],{"class":205},[149,1227,218],{"class":199},[149,1229,1230],{"class":159},"$CI_PIPELINE_SOURCE == \"web\"\n",[12,1232,1233,1234,1236,1237,1239],{},"From now on, once you push to the default (usually  ",[16,1235,108],{}," or ",[16,1238,981],{},") branch, continuous deployment is triggered.",[116,1241,1242,1245,1266],{},[12,1243,1244],{},"If you explicitely want to skip CI\u002FCD for a single push, use:",[140,1246,1248],{"className":142,"code":1247,"language":144,"meta":145,"style":145},"$ git push -o ci.skip\n",[16,1249,1250],{"__ignoreMap":145},[149,1251,1252,1254,1257,1260,1263],{"class":151,"line":152},[149,1253,156],{"class":155},[149,1255,1256],{"class":159}," git",[149,1258,1259],{"class":159}," push",[149,1261,1262],{"class":324}," -o",[149,1264,1265],{"class":159}," ci.skip\n",[12,1267,1268],{},"GitLab will respect it!",[12,1270,1271,1272,1275,1276,1279],{},"If you wish to run a manual pipeline on a different branch, just do this from Gitlab's ",[69,1273,1274],{},"Run pipeline"," form. The ",[16,1277,1278],{},"$CI_PIPELINE_SOURCE == \"web\""," rule comes into play!",[12,1281,1282,1283,1286,1287,1289],{},"The deployment task cares about basic versioning, so the final result will be such a structure (the Unix timestamp was taken from the latest git commit), while the oldest releases are getting rotated away (see ",[16,1284,1285],{},"KEEP"," variable in ",[16,1288,383],{},"):",[140,1291,1296],{"className":1292,"code":1294,"language":1295},[1293],"language-text","public_html\u002Fblog\n├── 1629150195\n├── 1629154265\n├── 1629154699\n├── 1629188761\n├── 1629188770\n└── current -> 1629188770\n","text",[16,1297,1294],{"__ignoreMap":145},[12,1299,1300,1301,1303,1304,1307],{},"Honestly, the deploy commands don't look that sexy in our ",[16,1302,383],{},". But we are perfectly happy with this, especially since we didn't introduce any new monster dependency like ",[25,1305,99],{"href":97,"rel":1306},[29]," or similar deployment tools.",[12,1309,1310,1311,1313],{},"Finally, point your production web server's virtual host to ",[16,1312,344],{},". No need to restart anything or flush any caches, as this is a static site \u002F SPA only.",[12,1315,1316],{},"Done! Now, go back coding and no longer care about deployment.",[1318,1319,1320],"style",{},"html pre.shiki code .sQJ0q, html code.shiki .sQJ0q{--shiki-light:#953800;--shiki-default:#953800;--shiki-dark:#FFA657}html pre.shiki code .s-BGO, html code.shiki .s-BGO{--shiki-light:#0A3069;--shiki-default:#0A3069;--shiki-dark:#A5D6FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s_bOF, html code.shiki .s_bOF{--shiki-light:#1F2328;--shiki-default:#1F2328;--shiki-dark:#E6EDF3}html pre.shiki code .sfEsd, html code.shiki .sfEsd{--shiki-light:#116329;--shiki-default:#116329;--shiki-dark:#7EE787}html pre.shiki code .s9BHP, html code.shiki .s9BHP{--shiki-light:#6E7781;--shiki-default:#6E7781;--shiki-dark:#8B949E}html pre.shiki code .sDkKp, html code.shiki .sDkKp{--shiki-light:#0550AE;--shiki-default:#0550AE;--shiki-dark:#79C0FF}html pre.shiki code .s5nWI, html code.shiki .s5nWI{--shiki-light:#CF222E;--shiki-default:#CF222E;--shiki-dark:#FF7B72}",{"title":145,"searchDepth":166,"depth":212,"links":1322},[1323,1324,1325],{"id":56,"depth":166,"text":57},{"id":113,"depth":166,"text":114},{"id":348,"depth":166,"text":349,"children":1326},[1327,1328],{"id":376,"depth":212,"text":377},{"id":543,"depth":212,"text":1329},"Create .gitlab-ci.yml",[1331],{"link":1332,"desc":1333},"https:\u002F\u002Fgist.github.com\u002Fonlime\u002F410d2f45642b4397bcad669a25db6ba7","Gist","2021-08-08 12:10","Simple SSH\u002Frsync based Continuous Deployment of a Nuxt.js static frontend project with GitLab CI\u002FCD.",{"type":9,"value":1337},[1338,1350],[12,1339,14,1340,19,1342,23,1344,31,1347,37],{},[16,1341,18],{},[16,1343,22],{},[25,1345,30],{"href":27,"rel":1346},[29],[25,1348,36],{"href":34,"rel":1349},[29],[12,1351,40,1352,46,1355,52],{},[25,1353,45],{"href":43,"rel":1354},[29],[25,1356,51],{"href":49,"rel":1357},[29],"md","\u002Fimages\u002Fcovers\u002Fgitlab-ci-cd-test-deploy.png",{},"\u002Farticles\u002F20210808-gitlab-cd-rsync-deployment",false,null,{"title":6,"description":1335},"articles\u002F20210808-gitlab-cd-rsync-deployment",[1367,1368,1369,1370,1371],"devops","gitlab","linux","vue.js","nuxt.js","CpFFjr2aRzvd-4N5jkelaM4GjrdvGMk5tKPB2R0cMjw",[1374,1379],{"title":1375,"path":1376,"stem":1377,"createdAt":1378,"children":-1},"Cleanup helper script to destroy old ZFS snapshots","\u002Farticles\u002F20210806-zfs-destroy-snapshots","articles\u002F20210806-zfs-destroy-snapshots","2021-08-06 13:00",{"title":1380,"path":1381,"stem":1382,"createdAt":1383,"children":-1},"Automated Mail Client Configuration with email-autoconf","\u002Farticles\u002F20210826-email-autoconf","articles\u002F20210826-email-autoconf","2021-08-26",1794961391277]