Skip to content
Merged
59 changes: 59 additions & 0 deletions .github/workflows/_test-file-exists-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Test File Exists Action

on:
push:
branches:
- main
paths:
- "actions/file-exists/**"
pull_request:
paths:
- "actions/file-exists/**"

permissions:
contents: read

jobs:
test-file-exists:
runs-on: ${{ matrix.os }}
name: Test File Exists Action (${{ matrix.os }})
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Test with existing file
uses: ./actions/file-exists
with:
file: README.md

test-failure-case:
runs-on: ${{ matrix.os }}
name: Test File Not Exists (${{ matrix.os }})
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Test with non-existent file
uses: ./actions/file-exists
with:
file: this-file-does-not-exist.txt
continue-on-error: true
id: nonexistent

- name: Verify action failed as expected
if: steps.nonexistent.outcome == 'failure'
run: echo "✅ Action correctly failed when file doesn't exist"

- name: Fail job if action didn't fail
if: steps.nonexistent.outcome != 'failure'
run: |
echo "❌ Action should have failed but didn't"
exit 1
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ This repository serves as a comprehensive toolkit for creating and managing GitH

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


## Examples

Expand Down
22 changes: 22 additions & 0 deletions actions/file-exists/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# File Exists :package:

A GitHub Action that checks if a file exists in the repository.

This action will fail if the file does not exist

## Inputs ⚙️

| Name | Description | Required |
| ------ | -------------------------------------------------------------- | -------- |
| `file` | The path to the file to check, relative to the repository root | Yes |

## Usage 🚀

```yaml
steps:
- uses: actions/checkout@v4
- name: Check if file exists
uses: skills/exercise-toolkit/actions/file-exists@<git-tag>
with:
file: "path/to/your/file.md"
```
25 changes: 25 additions & 0 deletions actions/file-exists/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: "File Exists"
description: "Checks if a file exists in the repository"
inputs:
file:
description: "The path to the file to check"
required: true
runs:
using: "composite"
steps:
- name: Check if file exists
uses: actions/github-script@v7
env:
FILE_PATH: ${{ inputs.file }}
with:
script: |
const fs = require('fs');
const path = require('path');
const filePath = path.join(process.env.GITHUB_WORKSPACE, process.env.FILE_PATH);

if (!fs.existsSync(filePath)) {
core.setFailed(`❌ File does not exist: ${filePath}`);
return;
}

console.log(`✅ File exists: ${filePath}`);
Loading