Trigger A GitHub Workflow When A New, Separate Repository is Created - github

I have the following code written which is meant to add a new tag to a repository. This works well if included in the project I'm trying to update directly.
name: Check Github Tags
on: push
jobs:
check-git-tags:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
with:
fetch-depth: 0
- name: Print Repository info
run: |
echo 'Commit:' $GITHUB_SHA
echo 'Repository:' $GITHUB_REPOSITORY
echo 'Workspace:' $GITHUB_WORKSPACE
echo 'Ref:' $GITHUB_REF
git tag
- name: Push new tag
id: push_new_tag
uses: anothrNick/github-tag-action#1.17.2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CUSTOM_TAG: test2
- name: Print New Tag Added
run: echo 'New tag' ${{steps.push_new_tag.outputs.new_tag}} 'added to commit' $GITHUB_SHA 'in' $GITHUB_REPOSITORY.
My question: Can I execute this script from outside of another project / repository? I need to "listen" for any new repositories being created within the organization and then tag the repository to ensure it has the 'test2' tag. So I really only need the workflow to run on the very first push to any new repo. But I don't know who will create the repo, what it will be called, etc. Is it possible to run this as a separate script?

Related

Github Actions to see changed files

I am trying to use actions to post a comment on a PR if a file has changed but my action is unable to see the changes.
jobs:
check:
runs-on: self-hosted
permissions:
pull-requests: write
steps:
- uses: actions/checkout#v2
with:
fetch-depth: 2
- name: Get all changed files and echo alert
env:
GITHUB_TOKEN: *******************
run: |
git diff --name-status
or if I change it to git status it outputs a message saying no changes working tree is clean, but I know one file has changed.
I tried using v1 instead of v2 but that doesn't work either.
Does anyone have any ideas on what i am doing wrong or how I can get this working?
Checkout is just "checking out" clean repository state for a given commit or PR.
If you expect to get changes files from PR, you can do it by using external actions, for examples:
- name: Get changed files using defaults
id: changed-files
uses: tj-actions/changed-files#v32
- name: List all added files
run: |
for file in ${{ steps.changed-files.outputs. modified_files }}; do
echo "$file was modified."
done
Use Github contexts: Learn GitHub Actions Contexts
Here is the command you probably need:
git diff --name-only ${{ github.event.after }} ${{ github.event.before }}
Also, you could affect the whole action by setting paths at the beginning of an action:
name: Action #1
on:
push:
branches:
- main
paths:
- <folder>/** // action will be triggered by push to the main branch AND when there are changed files in <folder>
pull_request:
branches:
- main
paths:
- <folder>/*.js // action will be triggered only on pull-requests AND when any .js files in <folder> have been changed

Unable to Manually Trigger GitHub Action

I recently started working on using some GitHub actions on my projects. I am able to setup them up to run automatically but am struggling with having them run manually. I know that you need the have the workflow_dispatch in the on section. I'm not sure if it's not working because I have it automatically run too. Is someone able to tell me what I am doing wrong?
Here is one of my workflow YAML files
name: Create-Doc-Nightly
on:
push:
branches: [ "nightly" ]
paths:
- 'src/**'
- 'pom.xml'
workflow_dispatch:
jobs:
doc:
name: Create Doc
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
name: Step 1 - Checkout Nightly Branch
with:
persist-credentials: false
fetch-depth: 0
- name: Step 2 - Setup JDK 17
uses: actions/setup-java#v3.4.1
with:
java-version: 17
distribution: 'temurin'
- name: Step 3 - Remove Doc
run: |
git remote set-url origin https://jnstockley:${{ secrets.TOKEN }}#github.com/jnstockley/BTTN.git
git config user.email "jack#jstockley.com"
git config --local user.name "Jack Stockley"
git rm -r docs
git commit -m "Removed Docs"
git push origin nightly
- name: Step 4 - Create Doc
run: mvn dokka:dokka -f pom.xml
- name: Step 5 - Move Docs
run: |
rm -rf docs
mkdir -p docs
mv target/dokka/* docs
- name: Step 6 - Publish docs
run: |
git remote set-url origin https://jnstockley:${{ secrets.TOKEN }}#github.com/jnstockley/BTTN.git
git config user.email "jack#jstockley.com"
git config --local user.name "Jack Stockley"
git add -f docs
git commit -m "Updated Docs"
git push origin nightly
Link to GitHub repo, nightly branch: https://github.com/jnstockley/BTTN/tree/nightly
The workflow must be on your default branch in order to use workflow_dispatch.
I believe in your case it's only on the branch nightly while it should also be on main.
To manually trigger a workflow, use the workflow_dispatch event. You can manually trigger a workflow run using the GitHub API, GitHub CLI, or GitHub browser interface. For more information, see Manually running a workflow
on: workflow_dispatch
Providing inputs
You can configure custom-defined input properties, default input values, and required inputs for the event directly in your workflow. When you trigger the event, you can provide the ref and any inputs. When the workflow runs, you can access the input values in the inputs context. For more information, see Contexts
This example defines inputs called logLevel, tags, and environment. You pass values for these inputs to the workflow when you run it. This workflow then prints the values to the log, using the inputs.logLevel, inputs.tags, and inputs.environment context properties.
yaml
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
type: choice
options:
- info
- warning
- debug
tags:
description: 'Test scenario tags'
required: false
type: boolean
environment:
description: 'Environment to run tests against'
type: environment
required: true
jobs:
log-the-inputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Log level: $LEVEL"
echo "Tags: $TAGS"
echo "Environment: $ENVIRONMENT"
env:
LEVEL: ${{ inputs.logLevel }}
TAGS: ${{ inputs.tags }}
ENVIRONMENT: ${{ inputs.environment }}
If you run this workflow from a browser you must enter values for the required inputs manually before the workflow will run.
You might like the following documentation links
workflow_dispatch
github docs - events-that-trigger-workflows

GitHub Actions get from API to create pull request

Is there a way to tell GitHub to automatically create a pull request from an API providing JSON content and merge it into my project?
I want to:
Edit files on a platform (I control the platform) using my own production editors/tools.
Have GitHub request it (REST), then create a PR or a commit, so people can collaborate on it with forks/GitHub project management.
Push from GitHub back to the platform for publishing.
3 is no problem, but 2 I can't find documentation for if it's even possible.
name: Manual workflow
on:
workflow_dispatch:
jobs:
makefiles:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout#v2
- name: Getting
uses: fjogeleit/http-request-action#master
id: myRequest
with:
url: 'https://domain/api/file'
method: 'GET'
- name: Show File
run: echo ${{ steps.myRequest.outputs.response }}
- name: Create A File
uses: 1arp/create-a-file-action#0.2
with:
path: 'src'
file: 'foo.bar'
content: ${{steps.myRequest.outputs.response}}
- name: final commit
uses: zwaldowski/git-commit-action#v1
id: git_commit
- name: show
run: echo "${{ steps.git_commit.outputs.sha }}"

git actions save workflow trigger to variable

I wrote a Github action workflow of which will be triggered only when some specific files were updated:
name: CI
on:
push:
paths:
### If a push was applied on one of these files, the CI workflow is triggered.###
### I want to know which file triggered the CI workflow and save it to a variable s I can use later in the CI steps ###
- 'dwh/helm/values-versions.yaml'
- 'ai/helm/values-versions.yaml'
- 'platform/helm/values-versions.yaml'
jobs:
copy-values-template-to-fluent-bit:
runs-on: self-hosted
container:
image: ghcr.io/***/myImage
credentials:
username: ${{ secrets.GHCR_USER }}
password: ${{ secrets.GHCR_PASS }}
steps:
- uses: actions/checkout#v2
- name: show repo files
run: |
pwd
ls -l
I need a way to figure out which file triggered the CI workflow and save it to a variable.
You can use this action Get All Changed Files:
- id: files
uses: jitterbit/get-changed-files#v1
- run: |
for changed_file in ${{ steps.files.outputs.all }}; do
echo "Do something with this ${changed_file} to check if this is you file and set variable."
done

Getting base branch SHA on pull request in Github Action Workflow

In GitHub action on pull request, I need to run some code in the context of the "current master", and later re-run the same code in the context of the PR branch.
I can check out compare a pull request to the base it is being PR-ed against. How would I find the SHA of the base branch (e.g. current master if PR is against the master)?
jobs:
job_on_base:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: "${{ github.base_ref }}"
- run: |
# Seems like I can get it here with $(git log -1 --format="%H")
echo "My current SHA is ... ?"
job_on_pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: "${{ github.ref }}"
- run: |
echo "My current SHA is $GITHUB_SHA"
echo "The BASE SHA is ?"
If the job runs on a pull_request event the base sha is available as ${{ github.event.pull_request.base.sha }}
This turned out to be a git question, rather than Github actions. The actions/checkout#v2 creates a shallow --depth=1 clone, so to get PR's parent one can parse git cat-file -p output as described here. The first (base) parent could be accessed with
git cat-file -p <SHA> | awk 'NR > 1 {if(/^parent/){print $2; exit}}'
The better approach turned out to be using fetch-depth: 2 parameter. It allows just one job to handle both pull request and master merge cases, and can also be used with HEAD^1 to get to the parent.
steps:
- uses: actions/checkout#v2
with:
fetch-depth: 2
What worked for me was: github.event.workflow_run.head_sha.
The top-voted answer suggesting github.event.pull_request.head.sha didn't work for me.
I found this by examining all the possible data in the github.context object using the method suggested here - https://stackoverflow.com/a/70107953