What is @actions/io?
@actions/io is an npm package designed to provide file system operations for GitHub Actions. It allows you to perform tasks such as copying, moving, and removing files and directories within your GitHub Actions workflows.
What are @actions/io's main functionalities?
Copy Files and Directories
This feature allows you to copy files and directories from one location to another. The code sample demonstrates copying a file from 'source/file.txt' to 'destination/file.txt'.
const io = require('@actions/io');
async function copyExample() {
try {
await io.cp('source/file.txt', 'destination/file.txt');
console.log('File copied successfully');
} catch (error) {
console.error('Error copying file:', error);
}
}
copyExample();
Move Files and Directories
This feature allows you to move files and directories from one location to another. The code sample demonstrates moving a file from 'source/file.txt' to 'destination/file.txt'.
const io = require('@actions/io');
async function moveExample() {
try {
await io.mv('source/file.txt', 'destination/file.txt');
console.log('File moved successfully');
} catch (error) {
console.error('Error moving file:', error);
}
}
moveExample();
Remove Files and Directories
This feature allows you to remove files and directories. The code sample demonstrates removing a file or directory at 'path/to/file_or_directory'.
const io = require('@actions/io');
async function removeExample() {
try {
await io.rmRF('path/to/file_or_directory');
console.log('File or directory removed successfully');
} catch (error) {
console.error('Error removing file or directory:', error);
}
}
removeExample();
Create Directories
This feature allows you to create directories, including any necessary parent directories. The code sample demonstrates creating a directory at 'path/to/new_directory'.
const io = require('@actions/io');
async function mkdirExample() {
try {
await io.mkdirP('path/to/new_directory');
console.log('Directory created successfully');
} catch (error) {
console.error('Error creating directory:', error);
}
}
mkdirExample();
Other packages similar to @actions/io
fs-extra
fs-extra is a package that extends the native Node.js fs module with additional methods for file system operations. It provides similar functionalities to @actions/io, such as copying, moving, and removing files and directories, but it is more general-purpose and not specifically designed for GitHub Actions.
shelljs
shelljs is a portable Unix shell commands for Node.js. It provides a wide range of file system operations, including copying, moving, and removing files and directories. It is more versatile and can be used in various environments, not just GitHub Actions.
node-fs
node-fs is an extension to the built-in fs module, providing additional file system methods. It offers functionalities similar to @actions/io, such as creating directories and copying files, but it is less focused on GitHub Actions and more on general Node.js applications.
@actions/io
Core functions for cli filesystem scenarios
Usage
mkdir -p
Recursively make a directory. Follows rules specified in man mkdir with the -p
option specified:
const io = require('@actions/io');
await io.mkdirP('path/to/make');
cp/mv
Copy or move files or folders. Follows rules specified in man cp and man mv:
const io = require('@actions/io');
const options = { recursive: true, force: false }
await io.cp('path/to/directory', 'path/to/dest', options);
await io.mv('path/to/file', 'path/to/dest');
rm -rf
Remove a file or folder recursively. Follows rules specified in man rm with the -r
and -f
rules specified.
const io = require('@actions/io');
await io.rmRF('path/to/directory');
await io.rmRF('path/to/file');
which
Get the path to a tool and resolves via paths. Follows the rules specified in man which.
const exec = require('@actions/exec');
const io = require('@actions/io');
const pythonPath: string = await io.which('python', true)
await exec.exec(`"${pythonPath}"`, ['main.py']);