diff --git a/.github/script/STEP b/.github/script/STEP new file mode 100644 index 0000000..573541a --- /dev/null +++ b/.github/script/STEP @@ -0,0 +1 @@ +0 diff --git a/.github/script/check-file.sh b/.github/script/check-file.sh new file mode 100755 index 0000000..05e5a98 --- /dev/null +++ b/.github/script/check-file.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Make sure this file is executable +# chmod a+x .github/script/check-file.sh + +# Make sure to escape your backslashes => \\ <= in YAML +# So that its still a single \ in bash + +echo "Check that $FILE includes $SEARCH" +if grep --extended-regexp "$SEARCH" -- $FILE +then + echo "Found $SEARCH in $FILE" +else + echo "Missing $SEARCH in $FILE" + echo "----------------" + echo "$(cat $FILE)" + exit 204 # We're sending a weird code so it looks different from other "failures" +fi diff --git a/.github/workflows/0-start.yml b/.github/workflows/0-start.yml new file mode 100644 index 0000000..ecac5b9 --- /dev/null +++ b/.github/workflows/0-start.yml @@ -0,0 +1,81 @@ +name: Step 0, Start + +# This step triggers after the learner creates a new repository from the template +# This step sets STEP to 1 +# This step closes
and opens
+ +# This will run every time we create push a commit to `main` +# Reference https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + push: + branches: + - main + +# Reference https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository + # Need `contents: write` to update the step metadata + contents: write + pull-requests: write + +jobs: + on_start: + name: On start + + # We will only run this action when: + # 1. This repository isn't the template repository + # Reference https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference https://docs.github.com/en/actions/learn-github-actions/expressions + if: ${{ !github.event.repository.is_template }} + + # We'll run Ubuntu for performance instead of Mac or Windows + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 # Let's get all the branches + + # Make a branch, file, commit, and pull request for the learner + - name: Prepare a pull request, branch, and file + run: | + echo "Make sure we are on step 0" + if [ "$(cat .github/script/STEP)" != 0 ] + then + echo "Current step is not 0" + exit 0 + fi + + echo "Make a branch" + BRANCH=reusable-workflow + git checkout -b $BRANCH + + echo "Make a commit" + git config user.name github-actions + git config user.email github-actions@github.com + git commit --allow-empty --message="Create an empty commit" + + echo "Push" + git push --set-upstream origin $BRANCH + + echo "Make a pull request" + # Reference https://cli.github.com/manual/gh_pr_create + gh pr create --title "Reusable workflow example" --body "Reusable workflow example" + + echo "Restore main" + git checkout main + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + # Update README to close
and open
+ # and set STEP to '1' + - name: Update to step 1 + uses: skills/action-update-step@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 0 + to_step: 1 + branch_name: reusable-workflow diff --git a/.github/workflows/1-make-workflow-reusable.yml b/.github/workflows/1-make-workflow-reusable.yml new file mode 100644 index 0000000..03a066e --- /dev/null +++ b/.github/workflows/1-make-workflow-reusable.yml @@ -0,0 +1,56 @@ +name: Step 1, Make a workflow reusable + +# This step triggers after step0 +# This step sets STEP to 2 +# This step closes
and opens
+ +# This will run every time we TBD-step-1-event-desc +# Reference https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + push: + branches: + - reusable-workflow + +# Reference https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository + # Need `contents: write` to update the step metadata + contents: write + +jobs: + make_reusable_workflow: + name: Make reusable workflow + + # We will only run this action when: + # 1. This repository isn't the template repository + # Reference https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference https://docs.github.com/en/actions/learn-github-actions/expressions + if: ${{ !github.event.repository.is_template }} + + # We'll run Ubuntu for performance instead of Mac or Windows + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 # Let's get all the branches + + # Verify the learner added the file contents + - name: Check workflow contents, workflow call + run: ./.github/script/check-file.sh + env: + FILE: ".github/workflows/reusable-workflow.yml" + SEARCH: "workflow_call:" + + # Update README to close
and open
+ # and set STEP to '2' + - name: Update to step 2 + uses: skills/action-update-step@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 1 + to_step: 2 + branch_name: reusable-workflow diff --git a/.github/workflows/2-add-a-job.yml b/.github/workflows/2-add-a-job.yml new file mode 100644 index 0000000..c61da3a --- /dev/null +++ b/.github/workflows/2-add-a-job.yml @@ -0,0 +1,58 @@ +name: Step 2, Add a job + +# This step triggers after TBD-step-2-event-desc +# This step sets STEP to 3 +# This step closes
and opens
+ +# This will run every time we TBD-step-2-event-desc +# Reference https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + push: + branches: + - reusable-workflow + paths-ignore: + - '.github/workflows/reusable-workflow.yml' + +# Reference https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository + # Need `contents: write` to update the step metadata + contents: write + +jobs: + on_add_job: + name: On add job + + # We will only run this action when: + # 1. This repository isn't the template repository + # Reference https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference https://docs.github.com/en/actions/learn-github-actions/expressions + if: ${{ !github.event.repository.is_template }} + + # We'll run Ubuntu for performance instead of Mac or Windows + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 # Let's get all the branches + + # Verify the learner added the file contents + - name: Check workflow syntax, new job + run: ./.github/script/check-file.sh + env: + FILE: ".github/workflows/my-starter-workflow.yml" + SEARCH: "uses: ./.github/workflows/reusable-workflow.yml" + + # Update README to close
and open
+ # and set STEP to '3' + - name: Update to step 3 + uses: skills/action-update-step@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 2 + to_step: 3 + branch_name: reusable-workflow diff --git a/.github/workflows/3-add-matrix-strategy.yml b/.github/workflows/3-add-matrix-strategy.yml new file mode 100644 index 0000000..c2230ec --- /dev/null +++ b/.github/workflows/3-add-matrix-strategy.yml @@ -0,0 +1,58 @@ +name: Step 3, Add a Matrix Strategy + +# This step triggers after TBD-step-3-event-desc +# This step sets STEP to 4 +# This step closes
and opens
+ +# This will run every time we TBD-step-3-event-desc +# Reference https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + push: + branches: + - reusable-workflow + paths-ignore: + - '.github/workflows/reusable-workflow.yml' + +# Reference https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository + # Need `contents: write` to update the step metadata + contents: write + +jobs: + on_matrix_strategy: + name: On matrix strategy + + # We will only run this action when: + # 1. This repository isn't the template repository + # Reference https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference https://docs.github.com/en/actions/learn-github-actions/expressions + if: ${{ !github.event.repository.is_template }} + + # We'll run Ubuntu for performance instead of Mac or Windows + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 # Let's get all the branches + + # Verify the learner added the file contents + - name: Check workflow contents, matrix strategy + run: ./.github/script/check-file.sh + env: + FILE: ".github/workflows/my-starter-workflow.yml" + SEARCH: "matrix:" + + # Update README to close
and open
+ # and set STEP to '4' + - name: Update to step 4 + uses: skills/action-update-step@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 3 + to_step: 4 + branch_name: reusable-workflow diff --git a/.github/workflows/4-merge-your-pull-request.yml b/.github/workflows/4-merge-your-pull-request.yml new file mode 100644 index 0000000..a803428 --- /dev/null +++ b/.github/workflows/4-merge-your-pull-request.yml @@ -0,0 +1,49 @@ +name: Step 4, Merge your pull request + +# This step triggers after TBD-step-4-event-desc +# This step sets STEP to 5 +# This step closes
and opens
+ +# This will run every time we TBD-step-4-event-desc +# Reference https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + push: + branches: + - main + +# Reference https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository + # Need `contents: write` to update the step metadata + contents: write + +jobs: + on_merge: + name: On merge + + # We will only run this action when: + # 1. This repository isn't the template repository + # Reference https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference https://docs.github.com/en/actions/learn-github-actions/expressions + if: ${{ !github.event.repository.is_template }} + + # We'll run Ubuntu for performance instead of Mac or Windows + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 # Let's get all the branches + + # Update README to close
and open
+ # and set STEP to '5' + - name: Update to step 5 + uses: skills/action-update-step@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 4 + to_step: 5 + branch_name: reusable-workflow diff --git a/.github/workflows/6-trigger-the-workflow.yml b/.github/workflows/6-trigger-the-workflow.yml new file mode 100644 index 0000000..73b1480 --- /dev/null +++ b/.github/workflows/6-trigger-the-workflow.yml @@ -0,0 +1,51 @@ +name: Step 5, Trigger the workflow + +# This step triggers after a pull request is merged to `main` +# This step sets STEP to X +# This step closes
and opens
+ +# This will run every time we create push a commit to `main` +# Reference https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows +on: + workflow_dispatch: + workflow_run: + workflows: + - My Starter Workflow + types: + - completed + +# Reference https://docs.github.com/en/actions/security-guides/automatic-token-authentication +permissions: + # Need `contents: read` to checkout the repository + # Need `contents: write` to update the step metadata + contents: write + +jobs: + on_trigger: + name: On trigger + + # We will only run this action when: + # 1. This repository isn't the template repository + # Reference https://docs.github.com/en/actions/learn-github-actions/contexts + # Reference https://docs.github.com/en/actions/learn-github-actions/expressions + if: ${{ !github.event.repository.is_template }} + # TBD replace `skills` with your organization name, if applicable + + # We'll run Ubuntu for performance instead of Mac or Windows + runs-on: ubuntu-latest + + steps: + # We'll need to check out the repository so that we can edit the README + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 # Let's get all the branches + + # Update README to close
and open
+ # and set STEP to 'X' + - name: Update to step X + uses: skills/action-update-step@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + from_step: 5 + to_step: X diff --git a/.github/workflows/my-starter-workflow.yml b/.github/workflows/my-starter-workflow.yml new file mode 100644 index 0000000..a2bcbb5 --- /dev/null +++ b/.github/workflows/my-starter-workflow.yml @@ -0,0 +1,18 @@ +name: My Starter Workflow + +on: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + + - uses: actions/checkout@v3 + + - name: Run a multi-line script + run: | + echo Add other actions to build, + echo test, and deploy to your project. + diff --git a/.github/workflows/reusable-workflow.yml b/.github/workflows/reusable-workflow.yml new file mode 100644 index 0000000..4cb4304 --- /dev/null +++ b/.github/workflows/reusable-workflow.yml @@ -0,0 +1,22 @@ +name: Reusable Workflow + +on: + workflow_dispatch: + inputs: + node: + required: true + type: string + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + + - uses: actions/checkout@v3 + + - name: Output the input value + run: | + echo "The node version to use is: ${{ inputs.node }}" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..773bfd6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +# Compiled source # +################### +*.com +*.class +*.dll +*.exe +*.o +*.so + +# Packages # +############ +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4ea99c2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,395 @@ +Attribution 4.0 International + +======================================================================= + +Creative Commons Corporation ("Creative Commons") is not a law firm and +does not provide legal services or legal advice. Distribution of +Creative Commons public licenses does not create a lawyer-client or +other relationship. Creative Commons makes its licenses and related +information available on an "as-is" basis. Creative Commons gives no +warranties regarding its licenses, any material licensed under their +terms and conditions, or any related information. Creative Commons +disclaims all liability for damages resulting from their use to the +fullest extent possible. + +Using Creative Commons Public Licenses + +Creative Commons public licenses provide a standard set of terms and +conditions that creators and other rights holders may use to share +original works of authorship and other material subject to copyright +and certain other rights specified in the public license below. The +following considerations are for informational purposes only, are not +exhaustive, and do not form part of our licenses. + + Considerations for licensors: Our public licenses are + intended for use by those authorized to give the public + permission to use material in ways otherwise restricted by + copyright and certain other rights. Our licenses are + irrevocable. Licensors should read and understand the terms + and conditions of the license they choose before applying it. + Licensors should also secure all rights necessary before + applying our licenses so that the public can reuse the + material as expected. Licensors should clearly mark any + material not subject to the license. This includes other CC- + licensed material, or material used under an exception or + limitation to copyright. More considerations for licensors: + wiki.creativecommons.org/Considerations_for_licensors + + Considerations for the public: By using one of our public + licenses, a licensor grants the public permission to use the + licensed material under specified terms and conditions. If + the licensor's permission is not necessary for any reason--for + example, because of any applicable exception or limitation to + copyright--then that use is not regulated by the license. Our + licenses grant only permissions under copyright and certain + other rights that a licensor has authority to grant. Use of + the licensed material may still be restricted for other + reasons, including because others have copyright or other + rights in the material. A licensor may make special requests, + such as asking that all changes be marked or described. + Although not required by our licenses, you are encouraged to + respect those requests where reasonable. More considerations + for the public: + wiki.creativecommons.org/Considerations_for_licensees + +======================================================================= + +Creative Commons Attribution 4.0 International Public License + +By exercising the Licensed Rights (defined below), You accept and agree +to be bound by the terms and conditions of this Creative Commons +Attribution 4.0 International Public License ("Public License"). To the +extent this Public License may be interpreted as a contract, You are +granted the Licensed Rights in consideration of Your acceptance of +these terms and conditions, and the Licensor grants You such rights in +consideration of benefits the Licensor receives from making the +Licensed Material available under these terms and conditions. + + +Section 1 -- Definitions. + + a. Adapted Material means material subject to Copyright and Similar + Rights that is derived from or based upon the Licensed Material + and in which the Licensed Material is translated, altered, + arranged, transformed, or otherwise modified in a manner requiring + permission under the Copyright and Similar Rights held by the + Licensor. For purposes of this Public License, where the Licensed + Material is a musical work, performance, or sound recording, + Adapted Material is always produced where the Licensed Material is + synched in timed relation with a moving image. + + b. Adapter's License means the license You apply to Your Copyright + and Similar Rights in Your contributions to Adapted Material in + accordance with the terms and conditions of this Public License. + + c. Copyright and Similar Rights means copyright and/or similar rights + closely related to copyright including, without limitation, + performance, broadcast, sound recording, and Sui Generis Database + Rights, without regard to how the rights are labeled or + categorized. For purposes of this Public License, the rights + specified in Section 2(b)(1)-(2) are not Copyright and Similar + Rights. + + d. Effective Technological Measures means those measures that, in the + absence of proper authority, may not be circumvented under laws + fulfilling obligations under Article 11 of the WIPO Copyright + Treaty adopted on December 20, 1996, and/or similar international + agreements. + + e. Exceptions and Limitations means fair use, fair dealing, and/or + any other exception or limitation to Copyright and Similar Rights + that applies to Your use of the Licensed Material. + + f. Licensed Material means the artistic or literary work, database, + or other material to which the Licensor applied this Public + License. + + g. Licensed Rights means the rights granted to You subject to the + terms and conditions of this Public License, which are limited to + all Copyright and Similar Rights that apply to Your use of the + Licensed Material and that the Licensor has authority to license. + + h. Licensor means the individual(s) or entity(ies) granting rights + under this Public License. + + i. Share means to provide material to the public by any means or + process that requires permission under the Licensed Rights, such + as reproduction, public display, public performance, distribution, + dissemination, communication, or importation, and to make material + available to the public including in ways that members of the + public may access the material from a place and at a time + individually chosen by them. + + j. Sui Generis Database Rights means rights other than copyright + resulting from Directive 96/9/EC of the European Parliament and of + the Council of 11 March 1996 on the legal protection of databases, + as amended and/or succeeded, as well as other essentially + equivalent rights anywhere in the world. + + k. You means the individual or entity exercising the Licensed Rights + under this Public License. Your has a corresponding meaning. + + +Section 2 -- Scope. + + a. License grant. + + 1. Subject to the terms and conditions of this Public License, + the Licensor hereby grants You a worldwide, royalty-free, + non-sublicensable, non-exclusive, irrevocable license to + exercise the Licensed Rights in the Licensed Material to: + + a. reproduce and Share the Licensed Material, in whole or + in part; and + + b. produce, reproduce, and Share Adapted Material. + + 2. Exceptions and Limitations. For the avoidance of doubt, where + Exceptions and Limitations apply to Your use, this Public + License does not apply, and You do not need to comply with + its terms and conditions. + + 3. Term. The term of this Public License is specified in Section + 6(a). + + 4. Media and formats; technical modifications allowed. The + Licensor authorizes You to exercise the Licensed Rights in + all media and formats whether now known or hereafter created, + and to make technical modifications necessary to do so. The + Licensor waives and/or agrees not to assert any right or + authority to forbid You from making technical modifications + necessary to exercise the Licensed Rights, including + technical modifications necessary to circumvent Effective + Technological Measures. For purposes of this Public License, + simply making modifications authorized by this Section 2(a) + (4) never produces Adapted Material. + + 5. Downstream recipients. + + a. Offer from the Licensor -- Licensed Material. Every + recipient of the Licensed Material automatically + receives an offer from the Licensor to exercise the + Licensed Rights under the terms and conditions of this + Public License. + + b. No downstream restrictions. You may not offer or impose + any additional or different terms or conditions on, or + apply any Effective Technological Measures to, the + Licensed Material if doing so restricts exercise of the + Licensed Rights by any recipient of the Licensed + Material. + + 6. No endorsement. Nothing in this Public License constitutes or + may be construed as permission to assert or imply that You + are, or that Your use of the Licensed Material is, connected + with, or sponsored, endorsed, or granted official status by, + the Licensor or others designated to receive attribution as + provided in Section 3(a)(1)(A)(i). + + b. Other rights. + + 1. Moral rights, such as the right of integrity, are not + licensed under this Public License, nor are publicity, + privacy, and/or other similar personality rights; however, to + the extent possible, the Licensor waives and/or agrees not to + assert any such rights held by the Licensor to the limited + extent necessary to allow You to exercise the Licensed + Rights, but not otherwise. + + 2. Patent and trademark rights are not licensed under this + Public License. + + 3. To the extent possible, the Licensor waives any right to + collect royalties from You for the exercise of the Licensed + Rights, whether directly or through a collecting society + under any voluntary or waivable statutory or compulsory + licensing scheme. In all other cases the Licensor expressly + reserves any right to collect such royalties. + + +Section 3 -- License Conditions. + +Your exercise of the Licensed Rights is expressly made subject to the +following conditions. + + a. Attribution. + + 1. If You Share the Licensed Material (including in modified + form), You must: + + a. retain the following if it is supplied by the Licensor + with the Licensed Material: + + i. identification of the creator(s) of the Licensed + Material and any others designated to receive + attribution, in any reasonable manner requested by + the Licensor (including by pseudonym if + designated); + + ii. a copyright notice; + + iii. a notice that refers to this Public License; + + iv. a notice that refers to the disclaimer of + warranties; + + v. a URI or hyperlink to the Licensed Material to the + extent reasonably practicable; + + b. indicate if You modified the Licensed Material and + retain an indication of any previous modifications; and + + c. indicate the Licensed Material is licensed under this + Public License, and include the text of, or the URI or + hyperlink to, this Public License. + + 2. You may satisfy the conditions in Section 3(a)(1) in any + reasonable manner based on the medium, means, and context in + which You Share the Licensed Material. For example, it may be + reasonable to satisfy the conditions by providing a URI or + hyperlink to a resource that includes the required + information. + + 3. If requested by the Licensor, You must remove any of the + information required by Section 3(a)(1)(A) to the extent + reasonably practicable. + + 4. If You Share Adapted Material You produce, the Adapter's + License You apply must not prevent recipients of the Adapted + Material from complying with this Public License. + + +Section 4 -- Sui Generis Database Rights. + +Where the Licensed Rights include Sui Generis Database Rights that +apply to Your use of the Licensed Material: + + a. for the avoidance of doubt, Section 2(a)(1) grants You the right + to extract, reuse, reproduce, and Share all or a substantial + portion of the contents of the database; + + b. if You include all or a substantial portion of the database + contents in a database in which You have Sui Generis Database + Rights, then the database in which You have Sui Generis Database + Rights (but not its individual contents) is Adapted Material; and + + c. You must comply with the conditions in Section 3(a) if You Share + all or a substantial portion of the contents of the database. + +For the avoidance of doubt, this Section 4 supplements and does not +replace Your obligations under this Public License where the Licensed +Rights include other Copyright and Similar Rights. + + +Section 5 -- Disclaimer of Warranties and Limitation of Liability. + + a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE + EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS + AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF + ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, + IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, + WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR + PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, + ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT + KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT + ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. + + b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE + TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, + NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, + INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, + COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR + USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN + ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR + DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR + IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. + + c. The disclaimer of warranties and limitation of liability provided + above shall be interpreted in a manner that, to the extent + possible, most closely approximates an absolute disclaimer and + waiver of all liability. + + +Section 6 -- Term and Termination. + + a. This Public License applies for the term of the Copyright and + Similar Rights licensed here. However, if You fail to comply with + this Public License, then Your rights under this Public License + terminate automatically. + + b. Where Your right to use the Licensed Material has terminated under + Section 6(a), it reinstates: + + 1. automatically as of the date the violation is cured, provided + it is cured within 30 days of Your discovery of the + violation; or + + 2. upon express reinstatement by the Licensor. + + For the avoidance of doubt, this Section 6(b) does not affect any + right the Licensor may have to seek remedies for Your violations + of this Public License. + + c. For the avoidance of doubt, the Licensor may also offer the + Licensed Material under separate terms or conditions or stop + distributing the Licensed Material at any time; however, doing so + will not terminate this Public License. + + d. Sections 1, 5, 6, 7, and 8 survive termination of this Public + License. + + +Section 7 -- Other Terms and Conditions. + + a. The Licensor shall not be bound by any additional or different + terms or conditions communicated by You unless expressly agreed. + + b. Any arrangements, understandings, or agreements regarding the + Licensed Material not stated herein are separate from and + independent of the terms and conditions of this Public License. + + +Section 8 -- Interpretation. + + a. For the avoidance of doubt, this Public License does not, and + shall not be interpreted to, reduce, limit, restrict, or impose + conditions on any use of the Licensed Material that could lawfully + be made without permission under this Public License. + + b. To the extent possible, if any provision of this Public License is + deemed unenforceable, it shall be automatically reformed to the + minimum extent necessary to make it enforceable. If the provision + cannot be reformed, it shall be severed from this Public License + without affecting the enforceability of the remaining terms and + conditions. + + c. No term or condition of this Public License will be waived and no + failure to comply consented to unless expressly agreed to by the + Licensor. + + d. Nothing in this Public License constitutes or may be interpreted + as a limitation upon, or waiver of, any privileges and immunities + that apply to the Licensor or You, including from the legal + processes of any jurisdiction or authority. + + +======================================================================= + +Creative Commons is not a party to its public +licenses. Notwithstanding, Creative Commons may elect to apply one of +its public licenses to material it publishes and in those instances +will be considered the “Licensor.” The text of the Creative Commons +public licenses is dedicated to the public domain under the CC0 Public +Domain Dedication. Except for the limited purpose of indicating that +material is shared under a Creative Commons public license or as +otherwise permitted by the Creative Commons policies published at +creativecommons.org/policies, Creative Commons does not authorize the +use of the trademark "Creative Commons" or any other trademark or logo +of Creative Commons without its prior written consent including, +without limitation, in connection with any unauthorized modifications +to any of its public licenses or any other arrangements, +understandings, or agreements concerning use of licensed material. For +the avoidance of doubt, this paragraph does not form part of the +public licenses. + +Creative Commons may be contacted at creativecommons.org. diff --git a/README.md b/README.md index 1f9a6bc..93bcb33 100644 --- a/README.md +++ b/README.md @@ -1 +1,309 @@ -# reusable-workflows + + +# Reusable Workflows and Matrix Strategies + +_Make a workflow reusable, call it in another workflow, and use a matrix strategy to run multiple versions_ + + + + + +Reusable workflows offer a simple and powerful way to avoid copying and pasting workflows across your repositories, and adding a matrix strategy lets you use variables in a single job definition to automatically create multiple job runs. + +- **Who is this for**: Developers, DevOps engineers, students, managers, teams, GitHub users. +- **What you'll learn**: How to create and use reusable workflows, create a matrix strategy, trigger workflows, and find workflow logs. +- **What you'll build**: An Actions workflow with a matrix strategy that calls a reusable workflow to output multiple verisions of node. +- **Prerequisites**: In this course you will work with pull requests and YAML workflow files. We recommend you take the [Introduction to GitHub](https://github.com/skills/introduction-to-github) course first or be familiar with GitHub basics, and the [Hello GitHub Actions](https://github.com/skills/hello-github-actions) course for an introduction to GitHub Actions and workflow files. +- **How long**: This course is five steps long and can be finished in less than one hour. +- **Attribution**: This Skills course was inspired by a [demo video](https://www.youtube.com/watch?v=MBpyouQtY_M) created by Mickey Gousset ([@mickeygousset](https://github.com/mickeygousset)). + +## How to start this course + +1. Above these instructions, right-click **Use this template** and open the link in a new tab. + ![Use this template](https://user-images.githubusercontent.com/1221423/169618716-fb17528d-f332-4fc5-a11a-eaa23562665e.png) +2. In the new tab, follow the prompts to create a new repository. + - For owner, choose your personal account or an organization to host the repository. + - We recommend creating a public repository—private repositories will [use Actions minutes](https://docs.github.com/en/billing/managing-billing-for-github-actions/about-billing-for-github-actions). + ![Create a new repository](https://user-images.githubusercontent.com/1221423/169618722-406dc508-add4-4074-83f0-c7a7ad87f6f3.png) +3. After your new repository is created, wait about 20 seconds, then refresh the page. Follow the step-by-step instructions in the new repository's README. + + + + + +
+

Step 1: Make a workflow reusable

+ +_Welcome to "Reusable Workflows and Matrix Strategies"! :wave:_ + +You can do a lot with GitHub Actions! You can automate repetitive tasks, build continuous integration and continuous deployment pipelines, and customize essentially any part of your software development workflow. It doesn't matter if you're just learning about workflows and GitHub Actions for the first time or you're well exerpienced with the process, you'll quickly find yourself repeating automation jobs and steps within the same workflow, and even using the dreaded copy and paste method for workflows across multiple repositories. + +Is there a solution to reduce these repetitive tasks? Yes, I'm glad you asked :wink: Enter **reusable workflows**, a simple and powerful way to avoid copying and pasting workflows across your repositories. + +**What are the benefits of using reusable workflows?**: Reusable workflows are … reusable. Reusable workflows let you DRY (don’t repeat yourself) your Actions configurations, so you don’t need to copy and paste your workflows from one repository to another. + +- Case in point: if you have three different Node applications and you’re building them all the same way, you can use one reusable workflow instead of copying and pasting your workflows again and again. + +**I have a workflow, how do I make it reusable?**: A reusable workflow is just like any GitHub Actions workflow with one key difference: it includes a `workflow_call` event trigger, similar to event triggers like `push`, `issues`, and `workflow_dispatch`. This means that all you need to do to make a workflow reusable is to use the workflow call trigger. + +Let's get started with our first step to see how this would work! + +### :keyboard: Activity: Add a `workflow_call` trigger to a workflow + +1. Open a new browser tab, and navigate to this same repository. Then, work on the steps in your second tab while you read the instructions in this tab. +1. Navigate to the **Code** tab. +1. From the **main** branch dropdown, click on the **reusable-workflow** branch. +1. Navigate to the `.github/workflows/` folder, then select the **reusable-workflow.yml** file. +1. Replace the `workflow_dispatch` event trigger with the `workflow_call` event trigger. It should look like the following: + + ```yaml + name: Reusable Workflow + + on: + workflow_call: + inputs: + node: + required: true + type: string + ``` +1. To commit your changes, click **Start commit**, and then **Commit changes**. +1. Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one. + +
+ + + +
+

Step 2: Add a job to call the reusable workflow

+ +_Nice work! :tada: You made a workflow reusable!_ + +Now that you have a reusable workflow, you can call it in another workflow within a new or existing job. But before we do that, let's take a minute to understand what our reusable workflow is doing by looking at the content of the file. + +**Understanding the file contents of your reusable workflow** + +```yaml +name: Reusable Workflow + +on: + workflow_call: + inputs: + node: + required: true + type: string + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + + - uses: actions/checkout@v3 + + - name: Output the input value + run: | + echo "The node version to use is: ${{ inputs.node }}" +``` + +The resuable workflow requires an `input` of `node` in order for the workflow to work. You need to make sure that the other workflow you are using to call this reusable workflow outputs a node version. If a node input is detected, the workflow will kick off a job called `build` that runs on ubuntu-latest. + +The step within the `build` job uses an action called `checkout@v3` to checkout the code and then a step to output the input value by running an echo command to print to the Actions log console the following message, `The node version to use is: ${{ inputs.node }}`. The node input here is the output node value you need to have in your other workflow. + +Okay, now that we know what the reusable workflow is doing, let's now add a new job to another workflow called **my-starter-workflow** to call our reusable workflow. We can do this by using the `uses:` command and then setting the path to the workflow we want to use. We also need to make sure we define that node input or the reusable workflow won't work. + +### :keyboard: Activity: Add a job to your workflow to call the reusable workflow + +1. Navigate to the `.github/workflows/` folder and open the `my-starter-workflow.yml` file. +1. Add a new job to the workflow called `call-reusable-workflow`. +1. Add a `uses` command and path the command to the `reusable-workflow.yml` file. +1. Add a `with` command to pass in a `node` paramater and set the value to `14`. + + ```yaml + call-reusable-workflow: + uses: ./.github/workflows/reusable-workflow.yml + with: + node: 14 + ``` +1. To commit your changes, click **Start commit**, and then **Commit changes**. +1. Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one. + +
+ + + +
+

Step 3: Add a matrix strategy to your workflow

+ +_Well done! :sparkles:_ + +Your **My Starter Workflow** now has a job that outputs the node version of 14 and calls the reusable workflow called **Reusable Workflow**. It then prints a message to the Actions logs of the node version for the build. Now, we haven't checked the Actions logs at the point to see the message, but don't worry, we'll get there after this next step. Let's improve our **My Starter Workflow** a little more but adding a matrix strategy. + +**What is a matrix strategy**: A matrix strategy lets you use variables in a single job definition to automatically create multiple job runs that are based on the combinations of the variables. For example, you can use a matrix strategy to test your code in multiple versions of a language or on multiple operating systems. Below is an example: + +```yaml +jobs: + example_matrix: + strategy: + matrix: + version: [10, 12, 14] + os: [ubuntu-latest, windows-latest] +``` +To define a matrix strategy inside a job, you first need to define the matrix with the keyword `strategy` followed by the nested keyword `matrix`. You can then define variables for the matrix. In the above example, the variables are `version` with the values of `10, 12, and 14`, and another variable called `os` with the values of `ubuntu-latest and windows latest`. + +The `example_matrix` job will run for each possible combination of the variables. So, in the above example, the workflow will run six jobs, one for each combination of the os and version variables. If you want to run a job for multiple versions, using a matrix strategy is a great solution over writing out 6 different jobs. + +Let's add a matrix strategy to the **My Starter Workflow** so we can run our job on different versions of node instead of the hard-coded single verison of 14. + +### :keyboard: Activity: Use a matrix strategy to run multiple versions + +1. In the same `my-starter-workflow.yml` file, add a `strategy` keyword under the `call-reusable-workflow` job. +1. Under `strategy`, add a `matrix` keyword. +1. Define the `nodeversion` variable to run over the following versions of node `[14, 16, 18, 20]`. +1. Replace the hard-coded `node` paramter of 14 used in the `with` command, and call the `nodeversion` in the matrix by using the following syntax `${{ matrix.nodeversion }}`. Below is that your job should look like: + + ```yaml + call-reusable-workflow: + strategy: + matrix: + nodeversion: [14, 16, 18, 20] + uses: ./.github/workflows/reusable-workflow.yml + with: + node: ${{ matrix.nodeversion }} + ``` +1. To commit your changes, click **Start commit**, and then **Commit changes**. +1. Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one. + +
+ + + +
+

Step 4: Merge your changes

+ +_Nicely done! :partying_face:_ + +You've added a matrix strategy to your workflow file that is now running on four different versions of node `[14, 16, 18, 20]` instead of the single hard-coded version of only `14`. + +You may not have noticed yet, but I've opened up a pull request called **Reusable workflow example** that is linked to the branch you've been making your file changes to (**reusable-workflow**). Let's navigate to this pull request and merge it so that your workflow file changes will be part of the `main` branch. + +### :keyboard: Activity: Create and merge your pull request + +1. In your repo, click on the **Pull requests** tab. +1. Click on the **Reusable workflow example** pull request. +1. Click **Merge pull request**, then click **Confirm merge**. +1. Optionally, click **Delete branch** to delete your `reusable-workflow` branch. +1. Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one. + + +
+ + + +
+

Step 5: Trigger your workflow and view the Actions logs

+ +_You're almost done. Last step! :heart:_ + +Now that the changes have been merged into the `main` branch, let's trigger the **My Starter Workflow** workflow to see everyting in action! But before we do, let's recall what we should expect to see before we run the workflow. + - We should expect to see five jobs running from our *My Starter Workflow**. Do you remember which ones? We have the `build` job and then the `call-reusable-workflow` job that has the matrix strategy. + ![Screen Shot 2022-09-08 at 9 53 52 AM](https://user-images.githubusercontent.com/6351798/189220189-97361a5e-eecf-4666-a859-e0587354bafe.png) + - We should also expect to see the echo message printed as an output from the reusable workflow with the node version for each of the matrix version jobs. + ![Screen Shot 2022-09-08 at 9 52 41 AM](https://user-images.githubusercontent.com/6351798/189220620-0576540a-366f-44e1-866c-2955af399cdb.png) + +### :keyboard: Activity: Run the My Starter Workflow and view the Actions logs + +1. Navigate to the **Actions** tab in your repo. +1. Choose the **My Starter Workflow** workflow from the left, and select the **Run workflow** button and run the workflow on the **Main** branch. +1. Wait a few seconds for the workflow run to appear in the queue. Once it shows, select the **My Starter Workflow** from the workflow runs queue. + +Notice the list of build jobs on the left. One for the `build` job and four for the different node versions (14, 16, 18, 20) that you are running from your matrix. When one of the node version jobs complete, you can select that job and view the Actions logs for the **Output the input value**. This will print out the message from the reusable workflow file. + +When you're done reviewing the Actions logs, return here and refresh the page to finish the course! 🎉 + + + +
+ + + +
+

Finish

+ +_Congratulations friend, you've completed this course!_ 🎉 + +celebrate + +Here's a recap of all the tasks you've accomplished in your repository: + +- You made a workflow reusable by using the `workflow_call` event trigger +- You created a new job in a separate workflow to call the reusable workflow +- You added a matrix strategy to run a job on multiple node versions +- You navigated through the Actions logs to view the workflow runs and results from specific jobs + +### What's next? + +- Learn more about GitHub Actions by reading "[Learn GitHub Actions](https://docs.github.com/actions/learn-github-actions)". +- Use actions created by others in [awesome-actions](https://github.com/sdras/awesome-actions). +- We'd love to hear what you thought of this course [in our discussion board](https://github.com/skills/.github/discussions). +- [Take another GitHub Skills course](https://github.com/skills). +- Learn more about GitHub by reading the "[Get started](https://docs.github.com/get-started)" docs. +- To find projects to contribute to, check out [GitHub Explore](https://github.com/explore). + +
+ + + +--- + +Get help: [TBD-support](TBD-support-link) • [Review the GitHub status page](https://www.githubstatus.com/) + +© 2022 TBD-copyright-holder • [Code of Conduct](https://www.contributor-covenant.org/version/2/1/code_of_conduct/code_of_conduct.md) • [CC-BY-4.0 License](https://creativecommons.org/licenses/by/4.0/legalcode)