2

With octokit a repository can be created that exists in an organization using:

require("dotenv").config();

const { Octokit } = require("@octokit/rest");
const cwa = new Octokit({
  auth: process.env.TOKEN, //create Github personal token
});

const main = async (name) => {
  try {
    await cwa.repos.createUsingTemplate({
      template_owner: process.env.OWNER,
      template_repo: "org-template-repo",
      name,
    });
    console.log("repository successfully create ");
  } catch (e) {
    console.log("error:", e);
  }
};

main();

Trying to automate after a repo is created I'm not finding a way in the docs to detect the creation of the templated repository after reading Workflow syntax and on.

repo-created.yml:

name: Repo Template Used
on: [created]
jobs:
  greet-when-created:
    runs-on: ubuntu-latest
    steps:
      - run: echo " The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo " This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo " The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v2
      - run: echo " The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
      - run: echo " This job's status is ${{ job.status }}."

but a workflow_dispatch takes inputs. Further areas of reading/research:

Is there a way to fire a Github Action when an organization's template is used from either the API or Use this template?

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
  • I don't think it's possible to trigger a github action workflow at the repository creation, as it's probably using the [create an organization repository API service](https://docs.github.com/en/rest/reference/repos#create-an-organization-repository) which already contains the template (There doesn't seem to need more than that event to create a repo). A workaround could be using a script that would send a [repository_dispatch](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#repository_dispatch) event to the repository after its creation. Is it an option? – GuiFalourd Jul 20 '21 at 21:21
  • Here is an explanation about how to [create a repository dispatch event](https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event). It can also be used to trigger workflows between repositories if needed ([here is a great content explaining it](https://blog.marcnuri.com/triggering-github-actions-across-different-repositories)). – GuiFalourd Jul 20 '21 at 21:26

1 Answers1

0

Checkout this discussion - https://github.com/orgs/community/discussions/25748

You can use workflow as follows

name: setup repo

on:
  create:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
...
Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122