Skip to content

Commit bab2bb6

Browse files
feat: add file-exists action (#40)
1 parent 57ecd96 commit bab2bb6

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Test File Exists Action
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- "actions/file-exists/**"
9+
pull_request:
10+
paths:
11+
- "actions/file-exists/**"
12+
13+
permissions:
14+
contents: read
15+
16+
jobs:
17+
test-file-exists:
18+
runs-on: ${{ matrix.os }}
19+
name: Test File Exists Action (${{ matrix.os }})
20+
strategy:
21+
fail-fast: false
22+
matrix:
23+
os: [ubuntu-latest, windows-latest, macos-latest]
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v4
27+
28+
- name: Test with existing file
29+
uses: ./actions/file-exists
30+
with:
31+
file: README.md
32+
33+
test-failure-case:
34+
runs-on: ${{ matrix.os }}
35+
name: Test File Not Exists (${{ matrix.os }})
36+
strategy:
37+
fail-fast: false
38+
matrix:
39+
os: [ubuntu-latest, windows-latest, macos-latest]
40+
steps:
41+
- name: Checkout repository
42+
uses: actions/checkout@v4
43+
44+
- name: Test with non-existent file
45+
uses: ./actions/file-exists
46+
with:
47+
file: this-file-does-not-exist.txt
48+
continue-on-error: true
49+
id: nonexistent
50+
51+
- name: Verify action failed as expected
52+
if: steps.nonexistent.outcome == 'failure'
53+
run: echo "✅ Action correctly failed when file doesn't exist"
54+
55+
- name: Fail job if action didn't fail
56+
if: steps.nonexistent.outcome != 'failure'
57+
run: |
58+
echo "❌ Action should have failed but didn't"
59+
exit 1

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ This repository serves as a comprehensive toolkit for creating and managing GitH
2222

2323
- **[.github/workflows](/.github/workflows)**: GitHub Actions workflows for automating common parts of Skills Exercises
2424
- **[markdown-templates](/markdown-templates)**: Ready-to-use Markdown templates for creating consistent exercise documentation, instructions, and README files
25+
- **[actions](/actions)**: Simple composite actions to help when building GitHub Skills exercises
26+
2527

2628
## Examples
2729

actions/file-exists/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# File Exists :package:
2+
3+
A GitHub Action that checks if a file exists in the repository.
4+
5+
This action will fail if the file does not exist
6+
7+
## Inputs ⚙️
8+
9+
| Name | Description | Required |
10+
| ------ | -------------------------------------------------------------- | -------- |
11+
| `file` | The path to the file to check, relative to the repository root | Yes |
12+
13+
## Usage 🚀
14+
15+
```yaml
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Check if file exists
19+
uses: skills/exercise-toolkit/actions/file-exists@<git-tag>
20+
with:
21+
file: "path/to/your/file.md"
22+
```

actions/file-exists/action.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: "File Exists"
2+
description: "Checks if a file exists in the repository"
3+
inputs:
4+
file:
5+
description: "The path to the file to check"
6+
required: true
7+
runs:
8+
using: "composite"
9+
steps:
10+
- name: Check if file exists
11+
uses: actions/github-script@v7
12+
env:
13+
FILE_PATH: ${{ inputs.file }}
14+
with:
15+
script: |
16+
const fs = require('fs');
17+
const path = require('path');
18+
const filePath = path.join(process.env.GITHUB_WORKSPACE, process.env.FILE_PATH);
19+
20+
if (!fs.existsSync(filePath)) {
21+
core.setFailed(`❌ File does not exist: ${filePath}`);
22+
return;
23+
}
24+
25+
console.log(`✅ File exists: ${filePath}`);

0 commit comments

Comments
 (0)