diff --git a/.github/workflows/_test-file-exists-action.yml b/.github/workflows/_test-file-exists-action.yml new file mode 100644 index 0000000..4e08e7a --- /dev/null +++ b/.github/workflows/_test-file-exists-action.yml @@ -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 diff --git a/README.md b/README.md index 0ea076c..f0bcb6e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/actions/file-exists/README.md b/actions/file-exists/README.md new file mode 100644 index 0000000..873bbf6 --- /dev/null +++ b/actions/file-exists/README.md @@ -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@ + with: + file: "path/to/your/file.md" +``` diff --git a/actions/file-exists/action.yml b/actions/file-exists/action.yml new file mode 100644 index 0000000..e3b9243 --- /dev/null +++ b/actions/file-exists/action.yml @@ -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}`);