diff --git a/.github/workflows/e2e-manual.yml b/.github/workflows/e2e-manual.yml new file mode 100644 index 0000000..8b3b114 --- /dev/null +++ b/.github/workflows/e2e-manual.yml @@ -0,0 +1,104 @@ +name: e2e Manual Test + +on: + workflow_dispatch: + inputs: + ARGS: + description: 'ARGS' + required: true + default: '-rltgoDzvO --delete --chmod=ugo+rwX --progress' + EXCLUDE: + description: 'EXCLUDE' + required: true + default: 'skip_dir/, /node_modules/' + SSH_CMD_ARGS: + description: 'SSH_CMD_ARGS' + required: true + default: '-o StrictHostKeyChecking=no, -o UserKnownHostsFile=/dev/null' + + +env: + TEST_HOST_DOCKER: ./test + TEST_USER: test + +jobs: + e2e-manual: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + # ################################################################# + # START [E2E Test Specific] steps + # ################################################################# + + - name: [E2E Test Specific] Clean up old test files + run: | + docker stop ssh-host-container || true && docker rm ssh-host-container || true + + - name: [E2E Test Specific] Create ssh keys + run: | + echo $HOME + ls -la $HOME + ssh-keygen -m PEM -t rsa -b 4096 -f "$HOME/.ssh/id_rsa" -N "" + eval `ssh-agent -s` + ssh-add "$HOME/.ssh/id_rsa" + ssh-add -l + echo "SSH_PRIVATE_KEY<> $GITHUB_ENV + cat $HOME/.ssh/id_rsa >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + + - name: [E2E Test Specific] Build Host Server Image + working-directory: ${{ env.TEST_HOST_DOCKER }} + run: | + docker build \ + -t ssh-host-image . \ + --build-arg SSH_PUB_KEY="$(cat $HOME/.ssh/id_rsa.pub)" + docker run -d -p 8822:22 --name=ssh-host-container ssh-host-image + docker exec ssh-host-container sh -c "hostname --ip-address" > ip.txt + echo "REMOTE_HOST<> $GITHUB_ENV + cat ip.txt >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + shell: bash + + - name: [E2E Test Specific] Create project file + run: | + mkdir test_project2 && cd "$_" + truncate -s 5MB info2.txt + cd ../ + mkdir test_project && cd "$_" + touch index.html + date +"%Y-%m-%d %H:%M:%S,%3N" >> index.html + truncate -s 50MB image.svg + truncate -s 5MB info.txt + truncate -s 500MB big_file.txt + mkdir skip_dir && cd "$_" + truncate -s 5MB text_in_skip_dir.txt + cd ../ + cat index.html + echo "test_project:" && ls -lR + echo "skip_dir:" && ls -lR skip_dir + + # ################################################################# + # END [E2E Test Specific] steps + # ################################################################# + + - name: e2e Test ssh-deploy action + uses: easingthemes/ssh-deploy@feature/multi-src + env: + # ENV Vars created in previous steps: + # SSH_PRIVATE_KEY: $EXAMPLE_SSH_PRIVATE_KEY + # REMOTE_HOST: $EXAMPLE_REMOTE_HOST + REMOTE_USER: ${{ env.TEST_USER }} + ARGS: ${{ github.event.inputs.ARGS }} + SSH_CMD_ARGS: ${{ github.event.inputs.SSH_CMD_ARGS }} + SOURCE: test_project/ test_project2/ + TARGET: /var/www/html/ + EXCLUDE: ${{ github.event.inputs.EXCLUDE }} + SCRIPT_BEFORE: | + whoami + ls -lR /var/www/html/ + SCRIPT_AFTER: | + ls -lR /var/www/html/ + echo $RSYNC_STDOUT diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 16ef6c1..728601f 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -3,25 +3,11 @@ name: e2e Test on: push: branches: [ 'feature/multi-src' ] - workflow_dispatch: - inputs: - ARGS: - description: 'ARGS' - required: true - default: '-rltgoDzvO --delete --chmod=ugo=rwX --progress' - EXCLUDE: - description: 'EXCLUDE' - required: true - default: 'skip_dir/, /node_modules/' - SSH_CMD_ARGS: - description: 'SSH_CMD_ARGS' - required: true - default: '-o StrictHostKeyChecking=no, -o UserKnownHostsFile=/dev/null' - env: TEST_HOST_DOCKER: ./test TEST_USER: test + TEST_USER2: test2 jobs: e2e: @@ -31,11 +17,15 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Clean up old test files + # ################################################################# + # START [E2E Test Specific] steps + # ################################################################# + + - name: [E2E Test Specific] Clean up old test files run: | docker stop ssh-host-container || true && docker rm ssh-host-container || true - - name: Create ssh keys + - name: [E2E Test Specific] Create ssh keys run: | echo $HOME ls -la $HOME @@ -47,13 +37,12 @@ jobs: cat $HOME/.ssh/id_rsa >> $GITHUB_ENV echo "EOF" >> $GITHUB_ENV - - name: Build Host Server Image + - name: [E2E Test Specific] Build Host Server Image working-directory: ${{ env.TEST_HOST_DOCKER }} run: | docker build \ -t ssh-host-image . \ - --build-arg SSH_PUB_KEY="$(cat $HOME/.ssh/id_rsa.pub)" \ - --build-arg ssh_user="${{ env.TEST_USER }}" + --build-arg SSH_PUB_KEY="$(cat $HOME/.ssh/id_rsa.pub)" docker run -d -p 8822:22 --name=ssh-host-container ssh-host-image docker exec ssh-host-container sh -c "hostname --ip-address" > ip.txt echo "REMOTE_HOST<> $GITHUB_ENV @@ -61,7 +50,7 @@ jobs: echo "EOF" >> $GITHUB_ENV shell: bash - - name: Create project file + - name: [E2E Test Specific] Create project file run: | mkdir test_project2 && cd "$_" truncate -s 5MB info2.txt @@ -76,24 +65,38 @@ jobs: truncate -s 5MB text_in_skip_dir.txt cd ../ cat index.html - echo "test_project:" && ls -l - echo "skip_dir:" && ls -l skip_dir + echo "test_project:" && ls -lR + echo "skip_dir:" && ls -lR skip_dir + + # ################################################################# + # END [E2E Test Specific] steps + # ################################################################# + + - name: set shared ENV variables for multi target deployment + run: | + echo "ARGS=-rltgoDzvOR --delete --chmod=ugo+rwX --progress" >> $GITHUB_ENV + echo "SSH_CMD_ARGS=-o StrictHostKeyChecking=no, -o UserKnownHostsFile=/dev/null" >> $GITHUB_ENV + echo "SOURCE=test_project/ test_project2/" >> $GITHUB_ENV + echo "EXCLUDE=skip_dir/, /node_modules/" >> $GITHUB_ENV + echo "SCRIPT_BEFORE<> $GITHUB_ENV + echo "whoami" >> $GITHUB_ENV + echo "ls -lR /var/www/html/" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + echo "SCRIPT_AFTER<> $GITHUB_ENV + echo $RSYNC_STDOUT >> $GITHUB_ENV + echo "ls -lR /var/www/html/" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV - - name: e2e Test published ssh-deploy action + - name: e2e Test ssh-deploy action - Target 1 uses: easingthemes/ssh-deploy@feature/multi-src env: - # ENV Vars created in previous steps: - # SSH_PRIVATE_KEY: $EXAMPLE_SSH_PRIVATE_KEY - # REMOTE_HOST: $EXAMPLE_REMOTE_HOST + # Shared ENV Vars created in previous steps REMOTE_USER: ${{ env.TEST_USER }} - ARGS: ${{ github.event.inputs.ARGS || '-rltgoDzvO --delete --chmod=ugo=rwX --progress' }} - SSH_CMD_ARGS: ${{ github.event.inputs.SSH_CMD_ARGS || '-o StrictHostKeyChecking=no, -o UserKnownHostsFile=/dev/null' }} - SOURCE: ["test_project/, test_project2/"] - TARGET: "/var/www/html/" - EXCLUDE: ${{ github.event.inputs.EXCLUDE || 'skip_dir/, /node_modules/' }} - SCRIPT_BEFORE: | - whoami - ls -al /var/www/html/ - SCRIPT_AFTER: | - ls -al /var/www/html/ - echo $RSYNC_STDOUT + TARGET: /var/www/html/${{ env.TEST_USER }} + + - name: e2e Test ssh-deploy action - Target 2 + uses: easingthemes/ssh-deploy@feature/multi-src + env: + # Shared ENV Vars created in previous steps + REMOTE_USER: ${{ env.TEST_USER2 }} + TARGET: /var/www/html/${{ env.TEST_USER2 }} diff --git a/.github/workflows/manual-release.yml b/.github/workflows/manual-release.yml index e4040ad..d713e2e 100644 --- a/.github/workflows/manual-release.yml +++ b/.github/workflows/manual-release.yml @@ -2,43 +2,72 @@ name: Manual Release on: workflow_dispatch: inputs: - dryrun: - description: 'DryRUn' + version: + description: 'Version' + type: choice required: true - default: 'false' + default: patch + options: + - patch + - minor + - major + dryRun: + description: 'DryRun' + type: boolean + default: true +# ENV and Config +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GIT_AUTHOR_NAME: github-actions + GIT_AUTHOR_EMAIL: github-actions@github.com + GIT_COMMITTER_NAME: github-actions + GIT_COMMITTER_EMAIL: github-actions@github.com + CI: true + CONFIG_NODE_VERSION: '["lts/*"]' + CONFIG_OS: '["ubuntu-latest"]' +# Main Job jobs: - release: - name: Test, Build and Release - runs-on: ${{ matrix.os }} + config: + runs-on: ubuntu-latest + outputs: + NODE_VERSION: ${{ steps.set-config.outputs.CONFIG_NODE_VERSION }} + OS: ${{ steps.set-config.outputs.CONFIG_OS }} + steps: + - id: set-config + run: | + echo "CONFIG_NODE_VERSION=${{ toJSON(env.CONFIG_NODE_VERSION) }}" >> $GITHUB_OUTPUT + echo "CONFIG_OS=${{ toJSON(env.CONFIG_OS) }}" >> $GITHUB_OUTPUT + release-manual: + name: Test, Build and force Release + needs: config + + runs-on: ${{ matrix.OS }} strategy: matrix: - os: [ ubuntu-latest ] - node-version: [ 16.x ] + OS: ${{ fromJSON(needs.config.outputs.OS) }} + NODE_VERSION: ${{ fromJSON(needs.config.outputs.NODE_VERSION) }} + steps: - - name: Checkout + - name: Checkout repo uses: actions/checkout@v3 - - name: Setup Node.js + - name: Setup Node.js ${{ matrix.NODE_VERSION }} uses: actions/setup-node@v3 with: - node-version: ${{ matrix['node-version'] }} + node-version: ${{ matrix.NODE_VERSION }} + - name: Commit trigger + run: | + git commit --allow-empty -m "${{ github.event.inputs.version }}: Trigger Manual Release" - name: Install dependencies run: npm ci - name: Build Library run: npm run build --if-present - name: Run Tests run: npm test --if-present - - name: Release + - name: Publish npm package uses: cycjimmy/semantic-release-action@v3 with: - dry_run: ${{ github.event.inputs.dryrun == 'true' }} + dry_run: ${{ github.event.inputs.dryRun == 'true' }} extra_plugins: | @semantic-release/changelog @semantic-release/git - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - GIT_AUTHOR_NAME: github-actions - GIT_AUTHOR_EMAIL: github-actions@github.com - GIT_COMMITTER_NAME: github-actions - GIT_COMMITTER_EMAIL: github-actions@github.com - CI: true diff --git a/README.md b/README.md index f4cc8fe..85097db 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ For any initial/required rsync flags, eg: `-avzr --delete` ##### 6. `SOURCE` (optional, default '') -The source directory, path relative to `$GITHUB_WORKSPACE` root, eg: `dist/` +The source directory, path relative to `$GITHUB_WORKSPACE` root, eg: `dist/`. +Multiple sources should be separated by space. ##### 7. `TARGET` (optional, default '/home/REMOTE_USER/') @@ -148,6 +149,14 @@ Check actions tab for example. More info for SSH keys: https://www.ssh.com/ssh/public-key-authentication +## Tips + +- Optional ENV variables are created for simple requirements. +For complex use cases, use `ARGS` and `SSH_CMD_ARGS` to fully configure `rsync` with all possible options. +- If you need to use multiple steps, eg multi targets deployment, save shared ENV variables in `>> $GITHUB_ENV`. +Check .github/workflows/e2e.yml for an example +- For multi sources, use -R ARG to manipulate folders structure. + ## Disclaimer diff --git a/dist/index.js b/dist/index.js index e31c50d..dad89fd 100755 --- a/dist/index.js +++ b/dist/index.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -(()=>{var e={898:(e,s,r)=>{"use strict";var o=r(81).spawn;var t=r(837);var escapeSpaces=function(e){if(typeof e==="string"){return e.replace(/\b\s/g,"\\ ")}else{return e}};var escapeSpacesInOptions=function(e){["src","dest","include","exclude","excludeFirst"].forEach((function(s){var r=e[s];if(typeof r==="string"){e[s]=escapeSpaces(r)}else if(Array.isArray(r)===true){e[s]=r.map(escapeSpaces)}}));return e};e.exports=function(e,s){e=e||{};e=t._extend({},e);e=escapeSpacesInOptions(e);var r=e.platform||process.platform;var n=r==="win32";if(typeof e.src==="undefined"){throw new Error("'src' directory is missing from options")}if(typeof e.dest==="undefined"){throw new Error("'dest' directory is missing from options")}var c=e.dest;if(typeof e.host!=="undefined"){c=e.host+":"+e.dest}if(!Array.isArray(e.src)){e.src=[e.src]}var i=[].concat(e.src);i.push(c);var a=(e.args||[]).find((function(e){return e.match(/--chmod=/)}));if(n&&!a){i.push("--chmod=ugo=rwX")}if(typeof e.host!=="undefined"||e.ssh){i.push("--rsh");var d="ssh";if(typeof e.port!=="undefined"){d+=" -p "+e.port}if(typeof e.privateKey!=="undefined"){d+=" -i "+e.privateKey}if(typeof e.sshCmdArgs!=="undefined"){d+=" "+e.sshCmdArgs.join(" ")}i.push(d)}if(e.recursive===true){i.push("--recursive")}if(e.times===true){i.push("--times")}if(e.syncDest===true||e.deleteAll===true){i.push("--delete");i.push("--delete-excluded")}if(e.syncDestIgnoreExcl===true||e.delete===true){i.push("--delete")}if(e.dryRun===true){i.push("--dry-run");i.push("--verbose")}if(typeof e.excludeFirst!=="undefined"&&t.isArray(e.excludeFirst)){e.excludeFirst.forEach((function(e,s){i.push("--exclude="+e)}))}if(typeof e.include!=="undefined"&&t.isArray(e.include)){e.include.forEach((function(e,s){i.push("--include="+e)}))}if(typeof e.exclude!=="undefined"&&t.isArray(e.exclude)){e.exclude.forEach((function(e,s){i.push("--exclude="+e)}))}switch(e.compareMode){case"sizeOnly":i.push("--size-only");break;case"checksum":i.push("--checksum");break}if(typeof e.args!=="undefined"&&t.isArray(e.args)){i=[...new Set([...i,...e.args])]}i=[...new Set(i)];var noop=function(){};var l=e.onStdout||noop;var u=e.onStderr||noop;var f="rsync ";i.forEach((function(e){if(e.substr(0,4)==="ssh "){e='"'+e+'"'}f+=e+" "}));f=f.trim();if(e.noExec){s(null,null,null,f);return}try{var p="";var h="";var y;if(n){y=o("cmd.exe",["/s","/c",'"'+f+'"'],{windowsVerbatimArguments:true,stdio:[process.stdin,"pipe","pipe"]})}else{y=o("/bin/sh",["-c",f])}y.stdout.on("data",(function(e){l(e);p+=e}));y.stderr.on("data",(function(e){u(e);h+=e}));y.on("exit",(function(e){var r=null;if(e!==0){r=new Error("rsync exited with code "+e);r.code=e}s(r,p,h,f)}))}catch(e){s(e,null,null,f)}}},505:(e,s,r)=>{const{existsSync:o,mkdirSync:t,writeFileSync:n}=r(147);const{join:c}=r(17);const validateDir=e=>{if(!e){console.warn("⚠️ [DIR] dir is not defined");return}if(o(e)){console.log(`✅ [DIR] ${e} dir exist`);return}console.log(`[DIR] Creating ${e} dir in workspace root`);t(e);console.log("✅ [DIR] dir created.")};const handleError=(e,s)=>{if(s){throw new Error(e)}console.warn(e)};const writeToFile=({dir:e,filename:s,content:r,isRequired:t,mode:i="0644"})=>{validateDir(e);const a=c(e,s);if(o(a)){const e=`⚠️ [FILE] ${a} Required file exist.`;handleError(e,t);return}try{console.log(`[FILE] writing ${a} file ...`,r.length);n(a,r,{encoding:"utf8",mode:i})}catch(e){const s=`⚠️[FILE] Writing to file error. filePath: ${a}, message: ${e.message}`;handleError(s,t)}};const validateRequiredInputs=e=>{const s=Object.keys(e);const r=s.filter((s=>{const r=e[s];if(!r){console.error(`❌ [INPUTS] ${s} is mandatory`)}return r}));if(r.length!==s.length){throw new Error("⚠️ [INPUTS] Inputs not valid, aborting ...")}};const snakeToCamel=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,((e,s)=>s.toUpperCase()));e.exports={writeToFile:writeToFile,validateRequiredInputs:validateRequiredInputs,snakeToCamel:snakeToCamel}},229:(e,s,r)=>{const{snakeToCamel:o}=r(505);const t=["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","ARGS","SSH_CMD_ARGS","EXCLUDE","SCRIPT_BEFORE","SCRIPT_AFTER"];const n=process.env.GITHUB_WORKSPACE;const c=process.env.REMOTE_USER;const i={source:"",target:`/home/${c}/`,exclude:"",args:"-rltgoDzvO",sshCmdArgs:"-o StrictHostKeyChecking=no",deployKeyName:"deploy_key"};const a={githubWorkspace:n};t.forEach((e=>{const s=o(e.toLowerCase());const r=process.env[e]||process.env[`INPUT_${e}`];const t=r===undefined?i[s]:r;let n=t;switch(s){case"args":n=t.split(" ");break;case"exclude":case"sshCmdArgs":n=t.split(",").map((e=>e.trim()));break}a[s]=n}));a.sshServer=`${a.remoteUser}@${a.remoteHost}`;a.rsyncServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,s,r)=>{const{exec:o}=r(81);const{sshServer:t,githubWorkspace:n}=r(229);const{writeToFile:c}=r(505);const handleError=(e,s,r)=>{if(s){r(new Error(e))}else{console.warn(e)}};const remoteCmd=async(e,s,r,i)=>new Promise(((a,d)=>{const l=`local_ssh_script-${i}.sh`;try{c({dir:n,filename:l,content:e});console.log(`Executing remote script: ssh -i ${s} ${t}`);o(`DEBIAN_FRONTEND=noninteractive ssh -i ${s} ${t} 'RSYNC_STDOUT="${process.env.RSYNC_STDOUT}" bash -s' < ${l}`,((e,s,o)=>{if(e){const t=`⚠️ [CMD] Remote script failed: ${e.message}`;console.warn(`${t} \n`,s,o);handleError(t,r,d)}else{console.log("✅ [CMD] Remote script executed. \n",s,o);a(s)}}))}catch(e){handleError(e.message,r,d)}}));e.exports={remoteCmdBefore:async(e,s,r)=>remoteCmd(e,s,r,"before"),remoteCmdAfter:async(e,s,r)=>remoteCmd(e,s,r,"after")}},447:(e,s,r)=>{const{execSync:o}=r(81);const t=r(898);const nodeRsyncPromise=async e=>new Promise(((s,r)=>{const logCMD=e=>{console.warn("================================================================");console.log(e);console.warn("================================================================")};try{t(e,((e,o,t,n)=>{if(e){console.error("❌ [Rsync] error: ");console.error(e);console.error("❌ [Rsync] stderr: ");console.error(t);console.error("❌️ [Rsync] stdout: ");console.error(o);console.error("❌ [Rsync] command: ");logCMD(n);r(new Error(`${e.message}\n\n${t}`))}else{console.log("⭐ [Rsync] command finished: ");logCMD(n);s(o)}}))}catch(e){console.error("❌ [Rsync] command error: ",e.message,e.stack);r(e)}}));const validateRsync=async()=>{try{o("rsync --version",{stdio:"inherit"});console.log("✅️ [CLI] Rsync exists");return}catch(e){console.warn("⚠️ [CLI] Rsync doesn't exists",e.message)}console.log('[CLI] Start rsync installation with "apt-get" \n');try{o("sudo DEBIAN_FRONTEND=noninteractive apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install rsync",{stdio:"inherit"});console.log("✅ [CLI] Rsync installed. \n")}catch(e){throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`)}};const rsyncCli=async({source:e,rsyncServer:s,exclude:r,remotePort:o,privateKeyPath:t,args:n,sshCmdArgs:c})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${s}`);if(r)console.log(`[Rsync] excluding folders ${r}`);const i={ssh:true,recursive:true,onStdout:e=>console.log(e.toString()),onStderr:e=>console.error(e.toString())};return nodeRsyncPromise({...i,src:e,dest:s,excludeFirst:r,port:o,privateKey:t,args:n,sshCmdArgs:c})};const sshDeploy=async e=>{await validateRsync();const s=await rsyncCli(e);console.log("✅ [Rsync] finished.",s);process.env.RSYNC_STDOUT=`${s}`;return s};e.exports={sshDeploy:sshDeploy}},822:(e,s,r)=>{const{join:o}=r(17);const{execSync:t}=r(81);const{writeToFile:n}=r(505);const c="known_hosts";const getPrivateKeyPath=(e="")=>{const{HOME:s}=process.env;const r=o(s||"~",".ssh");const t=o(r,c);return{dir:r,filename:e,path:o(r,e),knownHostsPath:t}};const addSshKey=(e,s)=>{const{dir:r,filename:o}=getPrivateKeyPath(s);n({dir:r,filename:c,content:""});console.log("✅ [SSH] known_hosts file ensured",r);n({dir:r,filename:o,content:e,isRequired:true,mode:"0400"});console.log("✅ [SSH] key added to `.ssh` dir ",r,o)};const updateKnownHosts=e=>{const{knownHostsPath:s}=getPrivateKeyPath();console.log("[SSH] Adding host to `known_hosts` ....",e,s);try{t(`ssh-keyscan -H ${e} >> ${s}`,{stdio:"inherit"})}catch(s){console.error("❌ [SSH] Adding host to `known_hosts` ERROR",e,s.message)}console.log("✅ [SSH] Adding host to `known_hosts` DONE",e,s)};e.exports={getPrivateKeyPath:getPrivateKeyPath,updateKnownHosts:updateKnownHosts,addSshKey:addSshKey}},81:e=>{"use strict";e.exports=require("child_process")},147:e=>{"use strict";e.exports=require("fs")},17:e=>{"use strict";e.exports=require("path")},837:e=>{"use strict";e.exports=require("util")}};var s={};function __nccwpck_require__(r){var o=s[r];if(o!==undefined){return o.exports}var t=s[r]={exports:{}};var n=true;try{e[r](t,t.exports,__nccwpck_require__);n=false}finally{if(n)delete s[r]}return t.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var r={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:s,remoteCmdAfter:r}=__nccwpck_require__(976);const{addSshKey:o,getPrivateKeyPath:t,updateKnownHosts:n}=__nccwpck_require__(822);const{validateRequiredInputs:c}=__nccwpck_require__(505);const i=__nccwpck_require__(229);const run=async()=>{const{source:a,remoteUser:d,remoteHost:l,remotePort:u,deployKeyName:f,sshPrivateKey:p,args:h,exclude:y,sshCmdArgs:m,scriptBefore:g,scriptAfter:_,rsyncServer:w}=i;c({sshPrivateKey:p,remoteHost:l,remoteUser:d});o(p,f);const{path:v}=t(f);if(g||_){n(l)}if(g){await s(g,v)}await e({source:a,rsyncServer:w,exclude:y,remotePort:u,privateKeyPath:v,args:h,sshCmdArgs:m});if(_){await r(_,v)}};run().then(((e="")=>{console.log("✅ [DONE]",e)})).catch((e=>{console.error("❌ [ERROR]",e.message);process.exit(1)}))})();module.exports=r})(); \ No newline at end of file +(()=>{var e={898:(e,s,r)=>{"use strict";var o=r(81).spawn;var t=r(837);var escapeSpaces=function(e){if(typeof e==="string"){return e.replace(/\b\s/g,"\\ ")}else{return e}};var escapeSpacesInOptions=function(e){["src","dest","include","exclude","excludeFirst"].forEach((function(s){var r=e[s];if(typeof r==="string"){e[s]=escapeSpaces(r)}else if(Array.isArray(r)===true){e[s]=r.map(escapeSpaces)}}));return e};e.exports=function(e,s){e=e||{};e=t._extend({},e);e=escapeSpacesInOptions(e);var r=e.platform||process.platform;var n=r==="win32";if(typeof e.src==="undefined"){throw new Error("'src' directory is missing from options")}if(typeof e.dest==="undefined"){throw new Error("'dest' directory is missing from options")}var c=e.dest;if(typeof e.host!=="undefined"){c=e.host+":"+e.dest}if(!Array.isArray(e.src)){e.src=[e.src]}var i=[].concat(e.src);i.push(c);var a=(e.args||[]).find((function(e){return e.match(/--chmod=/)}));if(n&&!a){i.push("--chmod=ugo=rwX")}if(typeof e.host!=="undefined"||e.ssh){i.push("--rsh");var d="ssh";if(typeof e.port!=="undefined"){d+=" -p "+e.port}if(typeof e.privateKey!=="undefined"){d+=" -i "+e.privateKey}if(typeof e.sshCmdArgs!=="undefined"){d+=" "+e.sshCmdArgs.join(" ")}i.push(d)}if(e.recursive===true){i.push("--recursive")}if(e.times===true){i.push("--times")}if(e.syncDest===true||e.deleteAll===true){i.push("--delete");i.push("--delete-excluded")}if(e.syncDestIgnoreExcl===true||e.delete===true){i.push("--delete")}if(e.dryRun===true){i.push("--dry-run");i.push("--verbose")}if(typeof e.excludeFirst!=="undefined"&&t.isArray(e.excludeFirst)){e.excludeFirst.forEach((function(e,s){i.push("--exclude="+e)}))}if(typeof e.include!=="undefined"&&t.isArray(e.include)){e.include.forEach((function(e,s){i.push("--include="+e)}))}if(typeof e.exclude!=="undefined"&&t.isArray(e.exclude)){e.exclude.forEach((function(e,s){i.push("--exclude="+e)}))}switch(e.compareMode){case"sizeOnly":i.push("--size-only");break;case"checksum":i.push("--checksum");break}if(typeof e.args!=="undefined"&&t.isArray(e.args)){i=[...new Set([...i,...e.args])]}i=[...new Set(i)];var noop=function(){};var l=e.onStdout||noop;var u=e.onStderr||noop;var f="rsync ";i.forEach((function(e){if(e.substr(0,4)==="ssh "){e='"'+e+'"'}f+=e+" "}));f=f.trim();if(e.noExec){s(null,null,null,f);return}try{var p="";var h="";var y;if(n){y=o("cmd.exe",["/s","/c",'"'+f+'"'],{windowsVerbatimArguments:true,stdio:[process.stdin,"pipe","pipe"]})}else{y=o("/bin/sh",["-c",f])}y.stdout.on("data",(function(e){l(e);p+=e}));y.stderr.on("data",(function(e){u(e);h+=e}));y.on("exit",(function(e){var r=null;if(e!==0){r=new Error("rsync exited with code "+e);r.code=e}s(r,p,h,f)}))}catch(e){s(e,null,null,f)}}},505:(e,s,r)=>{const{existsSync:o,mkdirSync:t,writeFileSync:n}=r(147);const{join:c}=r(17);const validateDir=e=>{if(!e){console.warn("⚠️ [DIR] dir is not defined");return}if(o(e)){console.log(`✅ [DIR] ${e} dir exist`);return}console.log(`[DIR] Creating ${e} dir in workspace root`);t(e);console.log("✅ [DIR] dir created.")};const handleError=(e,s)=>{if(s){throw new Error(e)}console.warn(e)};const writeToFile=({dir:e,filename:s,content:r,isRequired:t,mode:i="0644"})=>{validateDir(e);const a=c(e,s);if(o(a)){const e=`⚠️ [FILE] ${a} Required file exist.`;handleError(e,t);return}try{console.log(`[FILE] writing ${a} file ...`,r.length);n(a,r,{encoding:"utf8",mode:i})}catch(e){const s=`⚠️[FILE] Writing to file error. filePath: ${a}, message: ${e.message}`;handleError(s,t)}};const validateRequiredInputs=e=>{const s=Object.keys(e);const r=s.filter((s=>{const r=e[s];if(!r){console.error(`❌ [INPUTS] ${s} is mandatory`)}return r}));if(r.length!==s.length){throw new Error("⚠️ [INPUTS] Inputs not valid, aborting ...")}};const snakeToCamel=e=>e.replace(/[^a-zA-Z0-9]+(.)/g,((e,s)=>s.toUpperCase()));e.exports={writeToFile:writeToFile,validateRequiredInputs:validateRequiredInputs,snakeToCamel:snakeToCamel}},229:(e,s,r)=>{const{snakeToCamel:o}=r(505);const t=["REMOTE_HOST","REMOTE_USER","REMOTE_PORT","SSH_PRIVATE_KEY","DEPLOY_KEY_NAME","SOURCE","TARGET","ARGS","SSH_CMD_ARGS","EXCLUDE","SCRIPT_BEFORE","SCRIPT_AFTER"];const n=process.env.GITHUB_WORKSPACE;const c=process.env.REMOTE_USER;const i={source:"./",target:`/home/${c}/`,exclude:"",args:"-rltgoDzvO",sshCmdArgs:"-o StrictHostKeyChecking=no",deployKeyName:`deploy_key_${c}_${Date.now()}`};const a={githubWorkspace:n};t.forEach((e=>{const s=o(e.toLowerCase());const r=process.env[e]||process.env[`INPUT_${e}`];const t=r===undefined?i[s]:r;let n=t;switch(s){case"source":n=t.indexOf(" ")>-1?t.split(" "):t;break;case"args":n=t.split(" ");break;case"exclude":case"sshCmdArgs":n=t.split(",").map((e=>e.trim()));break}a[s]=n}));a.sshServer=`${a.remoteUser}@${a.remoteHost}`;a.rsyncServer=`${a.remoteUser}@${a.remoteHost}:${a.target}`;e.exports=a},976:(e,s,r)=>{const{exec:o}=r(81);const{sshServer:t,githubWorkspace:n}=r(229);const{writeToFile:c}=r(505);const handleError=(e,s,r)=>{if(s){r(new Error(e))}else{console.warn(e)}};const remoteCmd=async(e,s,r,i)=>new Promise(((a,d)=>{const l=`local_ssh_script-${i}.sh`;try{c({dir:n,filename:l,content:e});console.log(`Executing remote script: ssh -i ${s} ${t}`);o(`DEBIAN_FRONTEND=noninteractive ssh -i ${s} -o StrictHostKeyChecking=no ${t} 'RSYNC_STDOUT="${process.env.RSYNC_STDOUT}" bash -s' < ${l}`,((e,s,o)=>{if(e){const t=`⚠️ [CMD] Remote script failed: ${e.message}`;console.warn(`${t} \n`,s,o);handleError(t,r,d)}else{console.log("✅ [CMD] Remote script executed. \n",s,o);a(s)}}))}catch(e){handleError(e.message,r,d)}}));e.exports={remoteCmdBefore:async(e,s,r)=>remoteCmd(e,s,r,"before"),remoteCmdAfter:async(e,s,r)=>remoteCmd(e,s,r,"after")}},447:(e,s,r)=>{const{execSync:o}=r(81);const t=r(898);const nodeRsyncPromise=async e=>new Promise(((s,r)=>{const logCMD=e=>{console.warn("================================================================");console.log(e);console.warn("================================================================")};try{t(e,((e,o,t,n)=>{if(e){console.error("❌ [Rsync] error: ");console.error(e);console.error("❌ [Rsync] stderr: ");console.error(t);console.error("❌️ [Rsync] stdout: ");console.error(o);console.error("❌ [Rsync] command: ");logCMD(n);r(new Error(`${e.message}\n\n${t}`))}else{console.log("⭐ [Rsync] command finished: ");logCMD(n);s(o)}}))}catch(e){console.error("❌ [Rsync] command error: ",e.message,e.stack);r(e)}}));const validateRsync=async()=>{try{o("rsync --version",{stdio:"inherit"});console.log("✅️ [CLI] Rsync exists");return}catch(e){console.warn("⚠️ [CLI] Rsync doesn't exists",e.message)}console.log('[CLI] Start rsync installation with "apt-get" \n');try{o("sudo DEBIAN_FRONTEND=noninteractive apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install rsync",{stdio:"inherit"});console.log("✅ [CLI] Rsync installed. \n")}catch(e){throw new Error(`⚠️ [CLI] Rsync installation failed. Aborting ... error: ${e.message}`)}};const rsyncCli=async({source:e,rsyncServer:s,exclude:r,remotePort:o,privateKeyPath:t,args:n,sshCmdArgs:c})=>{console.log(`[Rsync] Starting Rsync Action: ${e} to ${s}`);if(r&&r.length>0)console.log(`[Rsync] excluding folders ${r}`);const i={ssh:true,recursive:true,onStdout:e=>console.log(e.toString()),onStderr:e=>console.error(e.toString())};return nodeRsyncPromise({...i,src:e,dest:s,excludeFirst:r,port:o,privateKey:t,args:n,sshCmdArgs:c})};const sshDeploy=async e=>{await validateRsync();const s=await rsyncCli(e);console.log("✅ [Rsync] finished.",s);process.env.RSYNC_STDOUT=`${s}`;return s};e.exports={sshDeploy:sshDeploy}},822:(e,s,r)=>{const{join:o}=r(17);const{execSync:t}=r(81);const{writeToFile:n}=r(505);const c="known_hosts";const getPrivateKeyPath=(e="")=>{const{HOME:s}=process.env;const r=o(s||"~",".ssh");const t=o(r,c);return{dir:r,filename:e,path:o(r,e),knownHostsPath:t}};const addSshKey=(e,s)=>{const{dir:r,filename:o}=getPrivateKeyPath(s);n({dir:r,filename:c,content:""});console.log("✅ [SSH] known_hosts file ensured",r);n({dir:r,filename:o,content:e,isRequired:true,mode:"0400"});console.log("✅ [SSH] key added to `.ssh` dir ",r,o)};const updateKnownHosts=e=>{const{knownHostsPath:s}=getPrivateKeyPath();console.log("[SSH] Adding host to `known_hosts` ....",e,s);try{t(`ssh-keyscan -H ${e} >> ${s}`,{stdio:"inherit"})}catch(s){console.error("❌ [SSH] Adding host to `known_hosts` ERROR",e,s.message)}console.log("✅ [SSH] Adding host to `known_hosts` DONE",e,s)};e.exports={getPrivateKeyPath:getPrivateKeyPath,updateKnownHosts:updateKnownHosts,addSshKey:addSshKey}},81:e=>{"use strict";e.exports=require("child_process")},147:e=>{"use strict";e.exports=require("fs")},17:e=>{"use strict";e.exports=require("path")},837:e=>{"use strict";e.exports=require("util")}};var s={};function __nccwpck_require__(r){var o=s[r];if(o!==undefined){return o.exports}var t=s[r]={exports:{}};var n=true;try{e[r](t,t.exports,__nccwpck_require__);n=false}finally{if(n)delete s[r]}return t.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var r={};(()=>{const{sshDeploy:e}=__nccwpck_require__(447);const{remoteCmdBefore:s,remoteCmdAfter:r}=__nccwpck_require__(976);const{addSshKey:o,getPrivateKeyPath:t,updateKnownHosts:n}=__nccwpck_require__(822);const{validateRequiredInputs:c}=__nccwpck_require__(505);const i=__nccwpck_require__(229);const run=async()=>{const{source:a,remoteUser:d,remoteHost:l,remotePort:u,deployKeyName:f,sshPrivateKey:p,args:h,exclude:y,sshCmdArgs:m,scriptBefore:g,scriptAfter:_,rsyncServer:w}=i;c({sshPrivateKey:p,remoteHost:l,remoteUser:d});o(p,f);const{path:v}=t(f);if(g||_){n(l)}if(g){await s(g,v)}await e({source:a,rsyncServer:w,exclude:y,remotePort:u,privateKeyPath:v,args:h,sshCmdArgs:m});if(_){await r(_,v)}};run().then(((e="")=>{console.log("✅ [DONE]",e)})).catch((e=>{console.error("❌ [ERROR]",e.message);process.exit(1)}))})();module.exports=r})(); \ No newline at end of file diff --git a/src/inputs.js b/src/inputs.js index 5b7bcb5..0471ca3 100644 --- a/src/inputs.js +++ b/src/inputs.js @@ -10,12 +10,12 @@ const githubWorkspace = process.env.GITHUB_WORKSPACE; const remoteUser = process.env.REMOTE_USER; const defaultInputs = { - source: '', + source: './', target: `/home/${remoteUser}/`, exclude: '', args: '-rltgoDzvO', sshCmdArgs: '-o StrictHostKeyChecking=no', - deployKeyName: 'deploy_key' + deployKeyName: `deploy_key_${remoteUser}_${Date.now()}` }; const inputs = { @@ -29,6 +29,9 @@ inputNames.forEach((input) => { let extendedVal = validVal; // eslint-disable-next-line default-case switch (inputName) { + case 'source': + extendedVal = validVal.indexOf(' ') > -1 ? validVal.split(' ') : validVal; + break; case 'args': extendedVal = validVal.split(' '); break; diff --git a/src/remoteCmd.js b/src/remoteCmd.js index 640234b..b5b57c7 100644 --- a/src/remoteCmd.js +++ b/src/remoteCmd.js @@ -17,7 +17,7 @@ const remoteCmd = async (content, privateKeyPath, isRequired, label) => new Prom writeToFile({ dir: githubWorkspace, filename, content }); console.log(`Executing remote script: ssh -i ${privateKeyPath} ${sshServer}`); exec( - `DEBIAN_FRONTEND=noninteractive ssh -i ${privateKeyPath} ${sshServer} 'RSYNC_STDOUT="${process.env.RSYNC_STDOUT}" bash -s' < ${filename}`, + `DEBIAN_FRONTEND=noninteractive ssh -i ${privateKeyPath} -o StrictHostKeyChecking=no ${sshServer} 'RSYNC_STDOUT="${process.env.RSYNC_STDOUT}" bash -s' < ${filename}`, (err, data, stderr) => { if (err) { const message = `⚠️ [CMD] Remote script failed: ${err.message}`; diff --git a/src/rsyncCli.js b/src/rsyncCli.js index cebb941..562a22e 100644 --- a/src/rsyncCli.js +++ b/src/rsyncCli.js @@ -55,7 +55,7 @@ const rsyncCli = async ({ privateKeyPath, args, sshCmdArgs }) => { console.log(`[Rsync] Starting Rsync Action: ${source} to ${rsyncServer}`); - if (exclude) console.log(`[Rsync] excluding folders ${exclude}`); + if (exclude && exclude.length > 0) console.log(`[Rsync] excluding folders ${exclude}`); const defaultOptions = { ssh: true, diff --git a/test/Dockerfile b/test/Dockerfile index a79ba12..fc9efdf 100644 --- a/test/Dockerfile +++ b/test/Dockerfile @@ -6,23 +6,32 @@ RUN apt update RUN apt install openssh-server rsync sudo -y -RUN useradd -rm -d /home/test -s /bin/bash -g root -G sudo -u 1000 test - -RUN usermod -aG sudo test - RUN echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config.d/pub.conf RUN echo "AuthorizedKeysFile .ssh/authorized_keys" >> /etc/ssh/sshd_config.d/pub.conf RUN mkdir -p /var/www/html -RUN chown -R test /var/www/html +RUN mkdir -p /var/www/html/test +RUN mkdir -p /var/www/html/test2 +RUN chmod -R 775 /var/www/html + +RUN useradd -rm -d /home/test -s /bin/bash -g root -G sudo -u 1000 test +RUN usermod -aG sudo test RUN mkdir -p /home/test/.ssh RUN echo "$SSH_PUB_KEY" > /home/test/.ssh/authorized_keys RUN chmod 700 /home/test/.ssh RUN chown -R test /home/test/.ssh +RUN useradd -rm -d /home/test2 -s /bin/bash -g root -G sudo -u 1002 test2 +RUN usermod -aG sudo test2 +RUN mkdir -p /home/test2/.ssh +RUN echo "$SSH_PUB_KEY" > /home/test2/.ssh/authorized_keys +RUN chmod 700 /home/test2/.ssh +RUN chown -R test2 /home/test2/.ssh + RUN service ssh start RUN echo 'test:test' | chpasswd +RUN echo 'test2:test2' | chpasswd EXPOSE 22