When doing deployment to a Remote Windows Server, we usually build our project locally and move the output build folder to the specific remote path. However, if we adopted the source control (Git) with CICD pipeline to automate the task of copying and updating files upon detected changes in specific branch, it will be partial automate since moving the files are done manually through Sftp client like WinSCP. Hence, the action to moving the files to the server has to be converted to single line command. Secure copy protocol (SCP) is a network protocol to transfer files between hosts on the network via Secure Shell (SSH).
Let’s see the basic command. Below show example to move a folder to remote server.
scp -r C:/app/MyProject/build TARGET_USER@TARGET_IP:~/build
Step 1: Startup OpenSSH Service in Windows Server
- Make sure OpenSSH Service is started on Windows server. If the Status is disabled, go to Properties > Log On > Log on as > Local System account. We will use the existing users available in the Remote Window Server for auth.
Step 2: Generate SSH Key Pairs
- At your host machine, run command below
# Key Type: RSA
# Number of bit for RSA Key: 2048 bit
# Generated key pairs path: $HOME/ssh_keys/windows-server-rsa
# Passphare: ""
ssh-keygen -t rsa -b 2048 -f $HOME/ssh_keys/windows-server-rsa -q -N ""
- Make sure private key to be not too open.
chmod 600 $HOME/ssh_keys/windows-server-rsa
- Copy the value in the
windows-server-rsa.pub
and prepare to paste the value to the Windows Server in the next step.
Step 3: Paste the Public Key in .ssh/authorized_keys
- Go to the target logon user folder in C drive of Window
C:/Users/${REMOTE_LOGON_USER}
, and navigate to.ssh
folder and paste the public key toauthorized_keys
file.
# Create .ssh folder if no exist
mkdir -p C:/Users/${REMOTE_LOGON_USER}/.ssh
# Paste the ssh public key to the file
echo ssh-rsa AAAAB3NzaC1yc2EAAA... >> C:/Users/${REMOTE_LOGON_USER}/.ssh/authorized_keys
Step 4: Create SSH Config
- In your local machine, go to the SSH Config file. And add a new Host config.
# Mac/Linux
cd ~/.ssh
vi config
# Windows
cd C:/Users/${YOUR_LOCAL_USER}/.ssh
# Make sure have bash in Windows
bash -c "vi config"
- Add your Windows Server Host
Host windows-server
HostName xxx.xxx.xxx.xxx #Remote Server Public IP Address
User REMOTE_LOGON_USER #Remote User
IdentityFile $HOME/ssh_keys/windows-server-rsa #Path of the generated private key just now
Step 5: Testing SSH Connection
# If command work, scp should also be fine
ssh windows-server
# If dont want to use SSH config, provide full command
ssh -i PRIVATE_KEY_PATH USER@xxx.xxx.xxx.xxx
Step 6: SCP Deployment
- In your remote Windows server, go to your project path. Make sure to give full permission of target folder to your logon user.
- If everything look good, try to run SCP command
scp -r ~/build windows-server:C:/MY_PROJECT_PATH/build
- After success copy the files to the server, you might need to run some command in the Window server. SSH provide RemoteCommand option for running command after logon to the server.
ssh windows-server -oRemoteCommand="cd C:/MY_PROJECT_PATH && git pull origin main && cmd -l"
# add --login/-l to maintain the connection after the command. Remove it if no necessary
- Combine together scp and ssh command above. If you are running NodeJS project, we can add “deploy” and “postdeploy” script to the package.json.
{
"scripts": {
"deploy": scp -r ~/build windows-server:C:/MY_PROJECT_PATH/build && ssh windows-server -oRemoteCommand="cd C:/MY_PROJECT_PATH && git pull origin main"
"postdeploy": pm2 restart my-project // will run after npm run deploy
}
}
That’s all for the deployment to Windows server via SCP.
Support & Appreciation
Big thanks for being awesome and diving into my blog posts! If you’ve enjoyed the content and want to see more, consider supporting the blog financially. Your contributions help keep the virtual ink flowing and ensure more quality posts in the future. Every bit counts, and I appreciate your support in making this blogging journey even more exciting!
Support me here: https://www.buymeacoffee.com/cheahwen