All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
62 lines
2.1 KiB
JavaScript
62 lines
2.1 KiB
JavaScript
import Client from 'ssh2-sftp-client';
|
|
import path from 'path';
|
|
|
|
const sftp = new Client();
|
|
|
|
const config = {
|
|
host: process.env.SFTP_HOST,
|
|
port: parseInt(process.env.SFTP_PORT || '22', 10),
|
|
username: process.env.SFTP_USER,
|
|
password: process.env.SFTP_PASSWORD
|
|
};
|
|
|
|
const localDir = path.resolve('./dist');
|
|
const remoteDir = 'dist';
|
|
|
|
async function main() {
|
|
if (!config.host || !config.username || !config.password) {
|
|
console.error('Error: Missing SFTP credentials in the .env file.');
|
|
console.error('Please create a .env file with SFTP_HOST, SFTP_USER, and SFTP_PASSWORD variables.');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`Connecting to SFTP server ${config.host} as ${config.username}...`);
|
|
try {
|
|
await sftp.connect(config);
|
|
console.log('Successfully connected! Uploading files recursively...');
|
|
|
|
// Upload local dist folder recursively to remote home folder
|
|
await sftp.uploadDir(localDir, remoteDir);
|
|
console.log('Upload complete! Applying correct server permissions...');
|
|
|
|
// Set folder permissions relative to dist/ on server
|
|
await sftp.chmod('dist', 0o755);
|
|
await sftp.chmod('dist/.htaccess', 0o644);
|
|
await sftp.chmod('dist/index.html', 0o644);
|
|
|
|
try {
|
|
await sftp.chmod('dist/assets', 0o755);
|
|
const list = await sftp.list('dist/assets');
|
|
for (const item of list) {
|
|
await sftp.chmod(`dist/assets/${item.name}`, 0o644);
|
|
}
|
|
// Fix permissions for local flag images
|
|
await sftp.chmod('dist/flags', 0o755);
|
|
const flags = await sftp.list('dist/flags');
|
|
for (const item of flags) {
|
|
await sftp.chmod(`dist/flags/${item.name}`, 0o644);
|
|
}
|
|
console.log('Folder permissions applied successfully (dist -> 755, assets -> 755, flags -> 755, files -> 644).');
|
|
} catch (err) {
|
|
console.log('Note: Some assets file permissions could not be set automatically, but parent directory is open.');
|
|
}
|
|
|
|
console.log('Deployment completed successfully! Website is now fully live.');
|
|
} catch (err) {
|
|
console.error('Deployment failed:', err.message);
|
|
} finally {
|
|
await sftp.end();
|
|
}
|
|
}
|
|
|
|
main();
|