Skip to content

Commit

Permalink
feat: write first version of this training
Browse files Browse the repository at this point in the history
Co-authored-by: Thibauld Dujardin<dujardin.t@sfeir.com>
Co-authored-by: Romain Lespinasse<lespinasse.r@sfeir.com>
  • Loading branch information
rlespinasse committed Sep 21, 2022
1 parent fc4e2b9 commit d6d2431
Show file tree
Hide file tree
Showing 875 changed files with 26,695 additions and 43,459 deletions.
27 changes: 27 additions & 0 deletions .github/actions/contexts-access/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Contexts Access
description: Show contexts accessible inside an Action
inputs:
value:
description: value
required: false
default: some
runs:
using: composite
steps:
- id: some-step
run: echo "Some step stuff"
shell: bash
- run: |
echo "${{ github.sha }}"
echo "${{ env.ANYTHING }}"
echo "${{ job.container }}"
echo "${{ steps.some-step.conclusion }}"
echo "${{ runner.os }}"
echo "${{ strategy.fail-fast }}"
echo "${{ matrix.value }}"
echo "${{ inputs.value }}"
shell: bash
# Inaccessible contexts
# - jobs
# - secrets
# - needs
19 changes: 19 additions & 0 deletions .github/actions/operations/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: 'Operations'
description: 'Perform operations'
inputs:
input1:
description: 'value that will be use to do math'
required: true
input2:
description: 'value that will be use to do math'
required: true
outputs:
addition:
description: 'The current addition result'
subtraction:
description: 'The current subtraction result'
multiplication:
description: 'The current multiplication result'
runs:
using: 'node16'
main: 'dist/index.js'
9 changes: 9 additions & 0 deletions .github/actions/operations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const core = require('@actions/core');
const { runner } = require("./runner");
const { add, subtract, multiply } = require("./math");

async function run() {
runner(core, add, subtract, multiply);
}

run();
61 changes: 61 additions & 0 deletions .github/actions/operations/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const core = require('@actions/core');
const { runner } = require("./runner");
const { add, subtract, multiply } = require("./math");
const process = require('process');
const cp = require('child_process');
const path = require('path');

test('make sure it returns something', async () => {
const core = {
getInput: jest.fn().mockResolvedValue(parseInt(42)),
debug: jest.fn(),
info: jest.fn(),
warning: jest.fn(),
setFailed: jest.fn(),
setOutput: jest.fn()
};
const math = {
add: jest.fn().mockReturnValue(1)
};
runner(core, math.add, subtract, multiply)
expect(core.getInput).toHaveBeenCalledTimes(2);
expect(core.debug).toHaveBeenCalledTimes(2);
expect(core.info).toHaveBeenCalledTimes(3);
expect(core.warning).toHaveBeenCalledTimes(1);
expect(core.setOutput).toHaveBeenCalledTimes(3);
expect(core.setFailed).toHaveBeenCalledTimes(0);
expect(math.add).toHaveBeenCalledTimes(1);
});

describe("simple test", () => {
it('test runs', () => {
process.env['INPUT_INPUT1'] = 9;
process.env['INPUT_INPUT2'] = 5;

const ip = path.join(__dirname, 'index.js');
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
expect(result).toContain("::set-output name=addition::14");
expect(result).toContain("::set-output name=subtraction::4");
expect(result).toContain("::set-output name=multiplication::45");
})
});

describe("simple arithmetic", () => {
describe("addition", () => {
test("expect 9 + 5 = 14", () => {
expect(add(9, 5)).toEqual(14);
});
});

describe("subtract", () => {
test("expect 9 - 5 = 4", () => {
expect(subtract(9, 5)).toEqual(4);
});
});

describe("multiply", () => {
test("expect 9 * 5 = 45", () => {
expect(multiply(9, 5)).toEqual(45);
});
});
});
9 changes: 9 additions & 0 deletions .github/actions/operations/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const add = (x, y) => parseInt(x, 10) + parseInt(y, 10);
const subtract = (x, y) => parseInt(x, 10) - parseInt(y, 10);
const multiply = (x, y) => parseInt(x, 10) * parseInt(y, 10);

module.exports = {
add,
subtract,
multiply,
};
17 changes: 17 additions & 0 deletions .github/actions/operations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "operations",
"version": "1.0.0",
"description": "Perform operations",
"main": "index.js",
"scripts": {
"prepare": "ncc build index.js -o dist --source-map",
"test": "jest"
},
"dependencies": {
"@actions/core": "^1.2.5"
},
"devDependencies": {
"@vercel/ncc": "^0.31.1",
"jest": "^27.3.1"
}
}
33 changes: 33 additions & 0 deletions .github/actions/operations/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function runner(core, add, subtract, multiply) {
try {
const input1 = core.getInput('input1');
const input2 = core.getInput('input2');

const start = new Date();
core.debug('Starting at : ' + start.toTimeString());

core.info(`Performing addition of : ${input1} & ${input2} ...`);
const addResult = add(input1, input2);

core.info(`Performing subtraction of : ${input1} & ${input2} ...`);
const subtractResult = subtract(input1, input2);

core.info(`Performing multiplication of : ${input1} & ${input2} ...`);
const multiplyResult = multiply(input1, input2);

const end = new Date();
core.debug('Ending at : ' + end.toTimeString());

core.warning('Delta between startTime & endTime : ' + (end - start));

core.setOutput('addition', addResult);
core.setOutput('subtraction', subtractResult);
core.setOutput('multiplication', multiplyResult);
} catch (error) {
core.setFailed(error.message);
}
}

module.exports = {
runner
};
8 changes: 8 additions & 0 deletions .github/actions/runs-reusable-workflow/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Runs Reusable Workflow
description: Example of composite action calling a reusable workflow
runs:
using: 'composite'
steps:
- uses: actions/checkout@v3
# This should failed due to system limitation
- uses: ./.github/workflows/code.yaml
30 changes: 30 additions & 0 deletions .github/actions/runs-using-composite/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Runs Using Composite
description: Example of composite action
runs:
using: 'composite'
steps:
# Using 'shell: bash' enable the actions to be available for all OS even windows.
# But the script will still need to manage the OS compliance
- run: $GITHUB_ACTION_PATH/setup.sh
shell: bash
if: runner.os == 'windows'

# Composite action can run another action (composite or note)
- name: Use another action
uses: rlespinasse/github-slug-action@v4
with:
prefix: CI_

# Result of an previous step are directly available (like $CI_GITHUB_REF_SLUG from github-slug-action)
- run: $GITHUB_ACTION_PATH/main.sh
shell: bash
env:
SLUG_REF: $CI_GITHUB_REF_SLUG

- run: ls
shell: bash
working-directory: .

- run: $GITHUB_ACTION_PATH/cleanup.sh
shell: bash
if: failure()
3 changes: 3 additions & 0 deletions .github/actions/runs-using-composite/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

echo "Cleaning up..."
3 changes: 3 additions & 0 deletions .github/actions/runs-using-composite/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

echo "Doing stuff..." "$SLUG_REF"
3 changes: 3 additions & 0 deletions .github/actions/runs-using-composite/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

echo "Setting up..."
5 changes: 5 additions & 0 deletions .github/actions/runs-using-docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM debian

ADD main.sh /usr/local/bin/main.sh
ADD setup.sh /usr/local/bin/setup.sh
ADD cleanup.sh /usr/local/bin/cleanup.sh
14 changes: 14 additions & 0 deletions .github/actions/runs-using-docker/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Runs Using Docker
description: Example of Dockerfile action
runs:
using: 'docker'
image: 'Dockerfile'
# pre/post features will not run if the action is run locally, it's only print a warning
pre-entrypoint: 'setup.sh'
entrypoint: 'main.sh'
post-entrypoint: 'cleanup.sh'
env:
KEY: 'Value'
args:
- 'firstArg'
- 'secondArg'
3 changes: 3 additions & 0 deletions .github/actions/runs-using-docker/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

echo "Cleaning up..." "$@" "($KEY)"
3 changes: 3 additions & 0 deletions .github/actions/runs-using-docker/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

echo "Doing stuff..." "$@" "($KEY)"
3 changes: 3 additions & 0 deletions .github/actions/runs-using-docker/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

echo "Setting up..." "$@" "($KEY)"
10 changes: 10 additions & 0 deletions .github/actions/runs-using-node/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Runs Using Node
description: Example of JavaScript action
runs:
using: 'node16'
main: 'main.js'
# pre/post features will not run if the action is run locally, it's only print a warning
pre: 'setup.js'
pre-if: runner.os == 'Windows'
post: 'cleanup.js'
post-if: failure()
1 change: 1 addition & 0 deletions .github/actions/runs-using-node/cleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(`Cleaning up..`);
1 change: 1 addition & 0 deletions .github/actions/runs-using-node/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(`Doing stuff...`);
1 change: 1 addition & 0 deletions .github/actions/runs-using-node/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(`Setting up..`);
22 changes: 22 additions & 0 deletions .github/actions/summary-composite/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Summary in composite
description: Example of summary in a composite action
runs:
using: 'composite'
steps:
# the summary can be fill using the actions toolkit
- uses: actions/github-script@v6
with:
script: |
await core.summary
.addRaw('## Summary from composite action using github script')
.write()
# or fill using $GITHUB_STEP_SUMMARY environment variable
- run: |
echo "## Summary from composite action using echo" >> $GITHUB_STEP_SUMMARY
shell: bash
# Composite action is call from one step, and contains multiple steps.
# Due to limitation, you will not see the '## Summary from composite action using github script' part,
# The composite steps override previous defined summary.
# This will not impacted the other steps of a job from the caller workflow
27 changes: 27 additions & 0 deletions .github/actions/testable-action/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Testable action
description: Example of a testable action
inputs:
value:
description: value to affect to the output
required: true
outputs:
value:
description: output value
value: '${{ steps.final-output-value.outputs.outputValue }}'
runs:
using: 'composite'
steps:
- run: |
echo "OUTPUT_VALUE=output-${{ inputs.value }}" >> $GITHUB_ENV
shell: bash
if: ${{ inputs.value != '' }}
- run: |
echo "::error ::Value need to be fill"
exit 1
shell: bash
if: ${{ inputs.value == '' }}
- id: final-output-value
run: echo "::set-output name=outputValue::${{ env.OUTPUT_VALUE }}"
shell: bash
23 changes: 23 additions & 0 deletions .github/workflows/code.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run Actions / Code section
on:
push:
workflow_call: # this trigger enable the workflow to be reusable from another workflow, see reuse.yaml file
jobs:
runs-using-actions:
runs-on: ubuntu-latest
steps:
# Run action using the public access {repo}/{path}@{ref}
- uses: sfeir-open-source/sfeir-school-github-action-dev/.github/actions/runs-using-node@main
- uses: sfeir-open-source/sfeir-school-github-action-dev/.github/actions/runs-using-docker@main
- uses: sfeir-open-source/sfeir-school-github-action-dev/.github/actions/runs-using-composite@main

runs-using-actions-local:
runs-on: ubuntu-latest
steps:
# Run local action by checkout the source and uses it
- uses: actions/checkout@v3
- uses: ./.github/actions/runs-using-composite

# For node and docker action, the pre/post features will not run if the action is run locally, it's only print a warning
- uses: ./.github/actions/runs-using-node
- uses: ./.github/actions/runs-using-docker
Loading

0 comments on commit d6d2431

Please sign in to comment.