GitHub Actions Reusable Workflows FULL TUTORIAL with Examples: Templates on Steroids

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ก.ค. 2024
  • No more duplication! You can now reuse entire GitHub Actions workflows as if they were a single action. Instead of copying and pasting workflow definitions across repositories, you can now reference an existing workflow with a single line of configuration.
    Github Actions Reusable Workflows make workflows easier to maintain and allows you to create new workflows more quickly by building on the work of others
    🌟EXCLUSIVE CONTENT🌟
    Do you want to access exclusive content, live chats and Q&As, polls, and even 1:1 calls with me? Join my Patreon: / coderdave
    🆘 NEED HELP? 🆘
    Book a 1:1 Consultation with CoderDave: geni.us/cdconsult
    🙏🏻SUPPORT THE CHANNEL🙏🏻
    Buy me a coffee: www.buymeacoffee.com/CoderDave
    PayPal me donation: paypal.me/dabenveg
    🎥VIDEOS
    ► GitHub Actions Templates with Composite Actions: • Create Reusable GitHub...
    ► GitHub Actions FULL Course: • GitHub Actions Tutoria...
    ► Use GitHub Like a PRO: • Use GitHub like a PRO!...
    💬JOIN THE COMMUNITY
    ► Discord: geni.us/cddiscord
    ► Newsletter: coderdave.io/newsletter
    ► Blog: dev.to/n3wt0n
    ► GitHub: github.com/n3wt0n
    ► Twitter: / davidebenvegnu
    ► Facebook: / coderdaveyt
    ⏲TIMESTAMPS
    0:00 The new feature
    0:40 Reusable Workflows in GitHub Actions
    1:24 How to Create a Reusable Workflow
    3:44 Creation Notes
    4:11 Use a Reusable Workflow
    6:39 Why Reusable Workflows
    6:58 Reusable Workflows Caveats
    7:36 Reusable Workflows Limitations
    8:14 Final Notes
    🎤PODCAST: geni.us/cdpodcast
    ❓QUESTIONS?
    Have a question about DevOps, Cloud, Coding, or Anything Else? Post in comments section of this video!
    🔴SUBSCRIBE to CoderDave here: th-cam.com/users/CoderDave?sub...
    _______________
    👕Get my MERCH: geni.us/cdmerch
    🔮TOOLS I USE
    ► Twingate - Connect to your Private Resources SECURELY: geni.us/twingate
    ► TubeBuddy - #1 TH-cam channel Management tool (FREE): www.tubebuddy.com/CoderDave
    ► Moosend - Free Newsletter and Automation Platform: geni.us/moosend
    📸🖥️GEAR AND SOFTWARE
    ► Music - Epidemic Sound (Get 30 days free): epidemicsound.com/referral/zf...
    ► Editing - Adobe Premiere Pro: geni.us/AdobeVideo
    ► Gear I Use for TH-cam: kit.co/CoderDave/gear-i-use-f...
    ► Gear I Use for Streaming: kit.co/CoderDave/gear-i-use-f...
    ► My Computer Setup: kit.co/CoderDave/main-compute...
    ► Full office setup: github.com/n3wt0n/work-from-h...
    Disclaimer:
    Some product links are affiliate links which means if you buy something I'll receive a small commission at no additional cost to you.
    As an Amazon Associate, I earn from qualifying purchases.
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 82

  • @CoderDave
    @CoderDave  2 ปีที่แล้ว +4

    ✨ Question of the day ✨: Do you have any use case for Reusable Workflows?
    GET EXCLUSIVE CONTENT:
    Join my Patreon: www.patreon.com/CoderDave
    SUPPORT THE CHANNEL :
    Buy me a coffee: www.buymeacoffee.com/CoderDave
    PayPal me donation: paypal.me/dabenveg

    • @RajabNatshah
      @RajabNatshah 2 ปีที่แล้ว +2

      Yes .. Having a workflow for products ( or project templates ).. then in each project could reuse the workflow .. not have a copy of the product workflow.. only reuse

  • @KennethRrvik
    @KennethRrvik 2 ปีที่แล้ว

    Great little snippet, helped me getting a quick test in action ;)

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      Great to hear!

  • @kumarsaroj18
    @kumarsaroj18 2 ปีที่แล้ว +2

    loved this video..
    crisp, but still very informative.
    I was looking for a way to optimize and reduce the number of similar workflow files.

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      Thanks 🙏🏻 always happy to know I’m helping ☺️

  • @AleksandarIvanov69
    @AleksandarIvanov69 2 ปีที่แล้ว +1

    It's good to have both CAs and RWs as an option, cause even tho they largely overlap, each one fits very specific different use cases.
    We are fiddling internally with both right now and we are fascinated by the potential for elegant solutions they both provide :)

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +1

      I agree, there is an overlap and you can do similar things with both. But I think the “philosophy“ behind them is different.

  • @adamsebetich9290
    @adamsebetich9290 2 ปีที่แล้ว +2

    Huge! Finally this comes out. I’ll wait for it to not be in beta to use

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      True, we were waiting for this for a while. I think you can start using it already, it seems pretty stable. I believe (but this is just my idea) that if anything will change from beta to GA will be added features, not removing anything 😄

  • @koodauskanava9096
    @koodauskanava9096 2 ปีที่แล้ว

    Thanks for this, very nice introduction video

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +1

      Glad you liked it!

  • @AleksandarIvanov69
    @AleksandarIvanov69 2 ปีที่แล้ว +1

    Seeing how we are both looking at the same things, but you being way down the path we are on and the fact that everything I've seen from you so far has been great, I would love to see the following videos on your channel:
    - Tutorial: How to run a GiHA workflow inside a container with non-root context
    - Recurring series: Reviewing GiHA Ci/CD pipelines

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +2

      First of all, thanks for the compliments :)
      Second, for Actions in container it is a little tricky, because Actions in Docker won't support running other Docker Actions (and there are a lot)... I'm waiting for the team to support that natively, I don't want to endorse "sketchy workarounds" :)
      For the last point, what do you mean by "Reviewing Actions CI?CD Pipelines"? You mean the workflows I have, or some viewer workflow, or...? I think this could be interesting, thanks for the suggestion!
      PS: if you want, join our Discord server: geni.us/cddiscord

    • @AleksandarIvanov69
      @AleksandarIvanov69 2 ปีที่แล้ว +2

      @@CoderDave I mean reviewing other people's pipelines, to point out mistakes, what could be improved and how. Something like that :)

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +1

      Sounds like a great idea!

    • @AleksandarIvanov69
      @AleksandarIvanov69 2 ปีที่แล้ว

      @@CoderDave I have a submission at hand already, so whenever you are :)

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      Let's see if we get enough interest on this, but I love the idea. Thanks!
      Check this out and submit your application ;) th-cam.com/users/postUgkxJ0P46PUBTAXcuh8VmbvTBJaNQsjckn0W

  • @vishnuvardhan-dg6xm
    @vishnuvardhan-dg6xm 2 ปีที่แล้ว

    Hi Dave, I have two repos consisting projects in one and dependecy poms in another. Selfhosted runner is being used. To build the services i need to install the dependencies in maven local repo and resolve them while building. How could i build the dependencies to maven local repo prior to building services in another repo using reusable workflows.?

  • @Only_Good_Trip
    @Only_Good_Trip 11 หลายเดือนก่อน

    Really helpful :-) thanks a lot

    • @CoderDave
      @CoderDave  11 หลายเดือนก่อน

      Glad you found it helpful ☺️

  • @lucsegers6931
    @lucsegers6931 9 หลายเดือนก่อน

    really loved this video, thank you. Did the reusable workflows changed a lot since this video? I understand the use of composite actions and reusable workflows, but what about templates? How does templates compare to reusable workflows and composite actions. Is templates being deprecated?

    • @CoderDave
      @CoderDave  9 หลายเดือนก่อน

      Thanks for the comment! AFAIK, Actions has never had templates (like for example Azure Pipelines has). Reusable workflows are the "de facto" templates in GitHub Actions. Them, and Composite Actions. Both can be considered sort-of templates. You may wanna check my video about that: th-cam.com/video/zc19mR3O4a4/w-d-xo.html

  • @RajabNatshah
    @RajabNatshah 2 ปีที่แล้ว +1

    Thank you :)

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      Welcome 😀

  • @iimnurdiansyah53
    @iimnurdiansyah53 2 ปีที่แล้ว

    Nice info, thanks

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      More than welcome ☺️

  • @subhajitsarkar7515
    @subhajitsarkar7515 2 ปีที่แล้ว

    this was really helpful , t
    hanks

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      Glad it was helpful!

  • @moustafa6006
    @moustafa6006 2 ปีที่แล้ว +1

    now you can reference a reusable workflow just like composite action. Meaning, it will use the file in the same branch with a path instead of using the repot and a ref.

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +1

      Yes, you can host them in either another repo or in the same repo, it is neat :) I talked about this in last week's video :)

  • @user-yc6lg1co5q
    @user-yc6lg1co5q 7 หลายเดือนก่อน

    Excellent Tutorial!
    If the reusable workflow is in a different repository, is it possible, in the caller workflow and under the job calling the reusable workflow, to checkout that second repository holding the reusable workflow? Also it seems performing the checkout (of the second repo) within the reusable workflow itself, never take effect; rather the checked-out caller workflow repo persists all through the entire job run.

    • @CoderDave
      @CoderDave  7 หลายเดือนก่อน +1

      Thanks :) No it is not possible, not directly anyway.
      If you use the checkout step in the reusable workflow, it will always checkout the caller repo. If you want to checkout the repo where the reusable workflow is (let's call it "myuser/reusables") then you can use the `repository` parameter of the checkout action (under `with`) and hardcode the "myuser/reusables" repo name there

  • @a2zbabysteps
    @a2zbabysteps 11 หลายเดือนก่อน +1

    @CoderDave Can we call the reusable workflow via curl - asynchronously (like fire and forget calls to these steps). i.e. our progress of main workflow should not be waiting for output from this reusable workflow?

    • @CoderDave
      @CoderDave  11 หลายเดือนก่อน

      No, unfortunately that is not possible. I can see the usefulness of that tho

  • @MichaelRainabbaRichardson
    @MichaelRainabbaRichardson 2 ปีที่แล้ว

    Is there a plan to see the environment variables translated into the shared workflow? Is there a reason that isn't currently done? Outstanding video and exactly what I needed. Thank you.

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +1

      Thanks! unfortunately I don't have an answer on your questions... I;m sure there is a technical reason why the env variables are not expanded to the reusable workflow, but I'm not sure why...

    • @gagikmirzoyan9884
      @gagikmirzoyan9884 ปีที่แล้ว +1

      I"m not sure if there was this method during the time this comment posted, but alongside parameters of reusable workflow under "with:" you can have "secrets: inherit" so reusable workflow will inherit caller workflow secrets.

    • @fromhereandthere460
      @fromhereandthere460 ปีที่แล้ว

      @@CoderDave then if you have a repo that needs an environment variable exclusive to it, then you can't use the reusable workflow because it won't be referenced in the re-usable-workflow right?

  • @ShyamKumar-yn2jw
    @ShyamKumar-yn2jw 2 ปีที่แล้ว +2

    how to call a workflow in a private repo. it says it private reusable workflow can be used only in the same repo. but didnt worked for me. Thanks in advance.

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +1

      You can call them in private repo from the same repo, using the "normal" syntax:
      jobs:
      your-job-name:
      uses: username-or-org/repository-name/.github/workflows/worflows_file-name.yml@your-version-tag-or-branch

    • @ShyamKumar-yn2jw
      @ShyamKumar-yn2jw 2 ปีที่แล้ว +1

      @@CoderDave Thanks :) , it worked, I missed the branch/tag

  • @fellifel147
    @fellifel147 ปีที่แล้ว

    Do u have any videos on workflow_run?

  • @suhaskulkarni5857
    @suhaskulkarni5857 ปีที่แล้ว +1

    How can we use org secrets in the workflow call? The workflow call always accepts secrets from called workflow by using inherit. Is there a workaround to use the secrets from workflow call repo or org secrets?

    • @CoderDave
      @CoderDave  ปีที่แล้ว

      Hi, afaik there is no way to be consume a secret directly from the reusable workflow. All has to be passed from the caller using the secret parameters

  • @NunoMaiaNM
    @NunoMaiaNM 2 ปีที่แล้ว +1

    I've setup two workflows in the same repository, and the called workflow does not receive any inputs passed from the caller :( . Do you have any sugestion?

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      Uhm that’s very hard to understand without seeing the workflows… perhaps you can join our discord server and ask there so we can take a look at it ☺️

  • @iceboxbob
    @iceboxbob 2 ปีที่แล้ว +1

    Do you think I can run specific jobs from the called workflow in a caller workflow?

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +1

      Yes, but you’d need to implement the logic by yourself. For example, having some IF conditions in the template and then passing the variable from the caller to the reusable workflow to control the execution

    • @iceboxbob
      @iceboxbob 2 ปีที่แล้ว

      @@CoderDave cool, thanks for the response. Your video was helpful.

  • @AnilKumar-qh5gz
    @AnilKumar-qh5gz ปีที่แล้ว +1

    I was able to create a reusable workflow (reusable.yml) and a caller workflow (caller-reusable.yml), which works fine. Thank you for the great video. Now my requirement is to pass a JSON file/data from caller workflow to the reusable workflow for some processing. I tried passing the JSON file/data from caller workflow to reusable workflow and then process the JSON file in reusable workflow by calling a PS script. But I am not able to refer to my PS script file (test.ps1) which has been created in reusable workflow.
    It simply throws below error, thought the path is correct:
    The term '/test.ps1' is not recognized as a
    | name of a cmdlet, function, script file, or executable
    | program. Check the spelling of the name, or if a path was
    | included, verify that the path is correct and try again.
    Any pointers please?

    • @CoderDave
      @CoderDave  ปีที่แล้ว

      When a reusable workflow is executed, the “context” is set to the caller workflow… this is why you cannot find your file.
      You could create a Composite action for the step where you need the ps file (in that case it is used), and reference that in the reusable workflow

  • @vishnuvardhan-dg6xm
    @vishnuvardhan-dg6xm 2 ปีที่แล้ว +1

    I have multiple workflow files for different services in one repo configured for same event. How can i stop all the workflows getting triggered except the service wish to run if committed?

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +1

      It is difficult to answer this without knowing how your repo is structured. I'd go with one of those 2 solutions.
      If your different services are in different folders, you can add the branch filter to your triggers... something like:
      on:
      push:
      branches:
      - main
      paths:
      - service1/
      more docs here: docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-including-paths
      if instead they are not in different folders, you could use the job conditionals to check for specific conditions (like file names, comments in the commit, etc)

    • @vishnuvardhan-dg6xm
      @vishnuvardhan-dg6xm 2 ปีที่แล้ว

      @@CoderDave Thank you Dave 😀

  • @sahilchaudhry08
    @sahilchaudhry08 ปีที่แล้ว +1

    Are the reusable workflow similar to templates in Azure DevOps YAML pipelines?

    • @CoderDave
      @CoderDave  ปีที่แล้ว

      They are somewhat similar, they can be used more or less for the same scanarios

    • @sahilchaudhry08
      @sahilchaudhry08 ปีที่แล้ว +1

      @@CoderDave I have already built some Azure devops YAML templates to build and deploy dotnet applications so I want to build similar stuff for Github Actions. What do you suggest?

    • @CoderDave
      @CoderDave  ปีที่แล้ว

      Yes, that is definitely a good use case. I'd create a repo with all the reusable workflows and you're good to go :)

  • @kalirajaramasamy1248
    @kalirajaramasamy1248 2 ปีที่แล้ว

    Thanks for awesome video. I tried this approach and getting the below error.
    No steps defined in `steps` for the following jobs: docker.
    I tried in enterprise github account. Could you please help me on this error?

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      It looks like GitHub doesn’t recognize your reusable workflow. It’s difficult to say without seeing your setup, but I’d double check the syntax in both files and see if everything is correct… with YAML just a small thing missing and everything stops working 😁

    • @kalirajaramasamy1248
      @kalirajaramasamy1248 2 ปีที่แล้ว

      @@CoderDave I just followed your videos and double cheked the indentation as well. Everything looks correct.
      One thing to note here is, In our project github account composite action also does not work.
      Composite action is not yet available for enterprise github account.

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      Uhm that is very strange. Both composite actions and reusable workflows are available in GitHub Enterprise. I’ve use them in GHEC already with clients. But it doesn’t work if they are hosted on private repos. They must be in public repos or internal repos with the proper settings

  • @yankee-in-london
    @yankee-in-london 2 ปีที่แล้ว

    Great video but being a bit of a newbie I am struggling to get this to work where the Parent and Child workflows are in the same repo. I figured the parent workflow could run a conditional statement and then reference the other workflow with "./.github/workflows/my-workflow" but it says it can't find it and I've been in a spin cycle since.

    • @yankee-in-london
      @yankee-in-london 2 ปีที่แล้ว

      steps:
      - name: Check if NPM Release (commit msg)
      if: contains(github.event.head_commit.message, 'release v')
      uses: ./.github/workflows/release

    • @yankee-in-london
      @yankee-in-london 2 ปีที่แล้ว

      I think part of my confusion is how to separate Actions and Workflows ... your example seemed to point to actions/my-workflow but you defined a workflow.

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      On the phone right now so I can’t test, but I believe for local workflows you need to use the full path, including “.yaml” extension

    • @yankee-in-london
      @yankee-in-london 2 ปีที่แล้ว +1

      @@CoderDave I eventually got it to work ... you not only need the '.yaml' but you also seem to need to self-reference the repo name and branch ... a bit awkward but it works

  • @akankshayadav7575
    @akankshayadav7575 2 ปีที่แล้ว

    What is the difference between composite action and reusable workflows

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว +2

      Great question, there are many differences, in fact too many for just an answer to the comment.
      But you can see composite actions as a subset of feature of reusable workflows. In general, composite actions are simpler, cannot use conditionals or multiple jobs, and have limited logs.
      Reusable workflows, on the other hand, are much more feature-rich, and can be seen as actual templates.
      I will make a video about the comparison soon, stay tuned.

    • @akankshayadav7575
      @akankshayadav7575 2 ปีที่แล้ว

      @@CoderDave Thank you so much

    • @akankshayadav7575
      @akankshayadav7575 2 ปีที่แล้ว

      Is shared libraries concept of GitHub Actions = reusable workflows ?

    • @CoderDave
      @CoderDave  2 ปีที่แล้ว

      More or less, yes

  • @ovididubois1612
    @ovididubois1612 ปีที่แล้ว

    Hi

  • @ashu9103
    @ashu9103 7 หลายเดือนก่อน

    when I call reusable workflow, it does not checkout reusable workflow repo but it checks out calling workflow. your example is not helpful.

    • @CoderDave
      @CoderDave  5 หลายเดือนก่อน

      That is how it’s designed to work… if you don’t like it , talk to GitHub… nothing I can do about it

  • @dannyv12
    @dannyv12 ปีที่แล้ว

    So why do you have to rebuild your app for every job this is pretty lame. 😢

    • @CoderDave
      @CoderDave  ปีที่แล้ว

      You don't have to... that was just an example... you can make the job(s) and workflow(s) do whatever you need them to

    • @dannyv12
      @dannyv12 ปีที่แล้ว

      @@CoderDave so how can i reuse a job build and use that in the new run-as for the new job because I can't find it?

    • @CoderDave
      @CoderDave  ปีที่แล้ว

      You mean building your app on Job A and reusing the build artifact on job B? That is not what reusable templates are for. To do that you can use the “upload artifact” action in job A and the “download artifact” action on job B