14

I would like my repo to have 2 functions:

  1. Create a release on tag push
  2. Test my software in a docker environment

Both require an action.yml in the repo. How do I combine them?

name: "Upload a Release Asset"
description: "Upload a release asset to an existing release on your repository"
author: "Github"
inputs:
  upload_url:
    description: "The URL for uploading assets to the release"
    required: true
  asset_path:
    description: "The path to the asset you want to upload"
    required: true
  asset_name:
    description: "The name of the asset you want to upload"
    required: true
  asset_content_type:
    description: "The content-type of the asset you want to upload. See the supported Media Types here: https://www.iana.org/assignments/media-types/media-types.xhtml for more information"
    required: true
outputs:
  browser_download_url:
    description: "The URL users can navigate to in order to download the uploaded asset"
runs:
  using: "node12"
  main: "dist/index.js"
branding:
  icon: "package"
  color: "gray-dark"
name: 'Hello World'
description: 'Greet someone and record the time'
inputs:
  who-to-greet:  # id of input
    description: 'Who to greet'
    required: true
    default: 'World'
outputs:
  time: # id of output
    description: 'The time we greeted you'
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.who-to-greet }}
benchenggis
  • 241
  • 2
  • 8

2 Answers2

40

@chenghopan! If you want to have two actions inside the same repository, they should be located in separate directories.

However, the action.yml file is not required.

This file is only required for an action if you plan to list it in the GitHub Marketplace.

If you have the actions in the same repo, they can have their own action.yml file located along with their Dockerfile or node script. Here's an example with two dockerfiles:

.
├── README.md
├── .github
│   └── workflows
│       └── main.yml
├── action1
│   ├── Dockerfile
│   ├── action.yml
│   └── entrypoint.sh
└── action2
    ├── Dockerfile
    ├── action.yml
    └── entrypoint.sh

And here's a workflow in the same repo calling both actions in the same repo:

name: Test two actions
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: ./action1
      - uses: ./action2

And here's a workflow in a different repo calling the actions:

name: Test two actions
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: managedkaos/github-actions-two-actions/action1@master
      - uses: managedkaos/github-actions-two-actions/action2@master

If you're OK with not listing the actions in the GitHub Marketplace, just put the action.yml file in the same dir as the action and you'll be fine!

For reference, you can find the code in these examples here:

  1. https://github.com/managedkaos/github-actions-two-actions
  2. https://github.com/managedkaos/test-two-actions
Michael J
  • 1,383
  • 2
  • 14
  • 25
  • ... and what is `entrypoint.sh` ? – TSR Nov 03 '21 at 14:05
  • @TSR i won't copy and paste all of the content from the docs but check out this link: https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions#entrypoint "The Docker ENTRYPOINT instruction has a shell form and exec form. The Docker ENTRYPOINT documentation recommends using the exec form of the ENTRYPOINT instruction. For more information about exec and shell form, see the ENTRYPOINT reference in the Docker documentation." – Michael J Nov 04 '21 at 17:15
  • Much better than the accepted answer. Thank you. – afilina Nov 18 '21 at 15:42
  • This answer was very helpful, and to extend it further you can put the actions into subdirectories like "actions/one/action.yml" and "actions/two/action.yml" so you don't have to have multiple top-level subdirectories. – Kevin P. Fleming Nov 30 '21 at 12:10
  • Sure. You can put the action in any directory in the repo as long as you reference them correctly using the complete path to the Dockerfile. – Michael J Dec 02 '21 at 04:14
  • Unfortunately I don't see how to publish such actions on Marketplace. It sees that I have a new repo with actions but after leading me to the Releases page there is no any button to publish. Even putting README and LICENCE in each action directory does not help. – Nakilon Apr 07 '22 at 04:06
  • 1
    @Nakilon if you are trying to combine _two_ actions into one action _and_ publish the combined action in the Marketplace, you should look into using a Composite Action: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action I will honestly say that I have not tried this approach but its the only one I can think of for a repo with two actions that must be combined and published all in one bundle. But I also think once you are getting to that point, you are better off using a reusable workflow: https://github.blog/2022-02-10-using-reusable-workflows-github-actions/ – Michael J Apr 08 '22 at 21:22
  • @Nakilon from the blog post linked i shared: "composite actions enable you to combine multiple actions into a single action that you can then insert into any workflow. This means you can refactor long YAML workflow files into much smaller files....In practice, there are kinds of problems you can solve with either reusable workflows or a composite workflow. Both approaches have individual strengths and weaknesses. 80% of the time you can probably use either one. But 20% of the time, you’ll need to use one or the other." – Michael J Apr 08 '22 at 21:24
9

Both require an action.yml in the repo. How do I combine them?

You could leave each of your actions in their own separate GitHub Action repository.

And, since August 2020, combine them.

See:

GitHub Actions: Composite Run Steps

You can now create reusable actions using shell scripts and even mix multiple shell languages in the same action.
You probably have a lot of shell script to automate many tasks, now you can easily turn them into an action and reuse them for different workflows. Sometimes it's easier to just write a shell script than JavaScript or Docker.
Now you don't have to worry about wrapping it up your scripts in a Docker containers.

Here's an example of how you can use composite run steps actions:

Composite Run Step Example -- https://i2.wp.com/user-images.githubusercontent.com/8660827/89449714-7bdd0480-d727-11ea-9863-7486fe666fd5.png?ssl=1

workflow.yml:

jobs:
 build:
   runs-on: windows-latest
   steps:
   - uses: actions/checkout@v2
   - uses: octocat/say-hello@v1
     with: 
       name: OctoCat

octocat/say-hello/action.yml:

inputs:
 name: 
   description: 'Your name'
   default: 'No name provided'
runs:
 using: "composite"
 steps: 
   - run: echo Hello ${{ inputs.name }}.
     shell: bash
   - run: echo "Nice to meet you!"
     shell: pwsh

Learn more about composite run steps and visit the GitHub Actions community forum for questions.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250