Skip to content

Commit 33d5c29

Browse files
author
Mokhlesur Mahin
committed
workflow permission
1 parent c5a1c6a commit 33d5c29

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: 'Check Permissions'
2+
description: 'Check if the user triggering the workflow is allowed to proceed.'
3+
inputs:
4+
permissions-file:
5+
description: 'Path to the permissions.yml file'
6+
required: true
7+
actor:
8+
description: 'GitHub actor triggering the workflow'
9+
required: true
10+
runs:
11+
using: 'composite'
12+
steps:
13+
- name: Set up Python
14+
uses: actions/setup-python@v4
15+
16+
- name: Install PyYAML
17+
run: pip install pyyaml
18+
shell: bash
19+
20+
- name: Check Permissions
21+
run: |
22+
python -c "
23+
import yaml
24+
with open('${{ inputs.permissions-file }}', 'r') as f:
25+
permissions = yaml.safe_load(f)
26+
if '${{ inputs.actor }}' not in permissions['allowed_users']:
27+
print('-----------------ERROR--------------------')
28+
print('User ${{ inputs.actor }} is not allowed to trigger this workflow.')
29+
print('------------------------------------------')
30+
exit(1)
31+
"
32+
shell: bash

.github/workflows/test-workflow.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Example Workflow
2+
on:
3+
workflow_dispatch:
4+
jobs:
5+
example-job:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Checkout code
9+
uses: actions/checkout@v3
10+
11+
- name: Check Permissions
12+
uses: ./.github/actions/check-permissions
13+
with:
14+
permissions-file: 'permissions.yml'
15+
actor: '${{ github.actor }}'
16+
17+
- name: Echo Something
18+
run: echo "Hello World!"

permissions.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allowed_users:
2+
- test-user

0 commit comments

Comments
 (0)