Skip to content

Commit 734f311

Browse files
author
Akshay Kalia
committed
add action.yml, python script + workflows
1 parent b6bf487 commit 734f311

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

.github/workflows/test_action.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# .github/workflow/test_action.yaml
2+
name: Test Action
3+
on: [push]
4+
5+
jobs:
6+
get-num-square:
7+
runs-on: ubuntu-latest
8+
name: Return's the number square
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
- name: Fetch num squared
13+
id: get_square
14+
uses: ./ # Uses an action in the root directory
15+
# or use a released Github Action
16+
# uses: shipyard/github-action/fetch-shipyard-env@1.0.0
17+
with:
18+
num: 11
19+
- name: Print the square
20+
run: echo "${{ steps.get_square.outputs.num_squared }}"

action.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# action.yaml
2+
name: 'Custom Github Action'
3+
description: 'A Github Action that takes an input and returns the square of the number'
4+
inputs:
5+
num:
6+
description: 'Enter a number'
7+
required: true
8+
default: "1"
9+
outputs:
10+
num_squared:
11+
description: 'Square of the input'
12+
# need to specify the extra `value` field for `composite` actions
13+
value: ${{ steps.get-square.outputs.num_squared }}
14+
runs:
15+
using: 'composite'
16+
steps:
17+
- name: Install Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: '3.10'
21+
- name: Install Dependencies
22+
run: pip install -r requirements.txt
23+
shell: bash
24+
- name: Pass Inputs to Shell
25+
run: |
26+
echo "INPUT_NUM=${{ inputs.num }}" >> $GITHUB_ENV
27+
shell: bash
28+
- name: Fetch the number's square
29+
id: get-square
30+
run: python src/get_num_square.py
31+
shell: bash

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest

src/get_num_square.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# src/get_num_square.py
2+
import os
3+
4+
# get the input and convert it to int
5+
num = os.environ.get("INPUT_NUM")
6+
if num:
7+
try:
8+
num = int(num)
9+
except Exception:
10+
exit('ERROR: the INPUT_NUM provided ("{}") is not an integer'.format(num))
11+
else:
12+
num = 1
13+
14+
# to set output, print to shell in following syntax
15+
print(f"::set-output name=num_squared::{num^2}")

0 commit comments

Comments
 (0)