Forking actions/checkout#v2 at organization level to be used repo's github actions - github

Problem:
Inherently, github actions has no information about the code within the repo it's being run in. To rid that problem, there is the actions/checkout workflow that is the defacto start of most workflows.
Our enterprise account got locked down to only local actions only:
Because of this, we are not able to use the actions/checkout#v2 at the start of our workflow, thus rendering our Github Actions useless.
Proposed Solution
Fork the actions/checkout repo as a submodule of a repo and use that reference in my code like so:
steps:
- uses: <enterprise_name>/<repo_name>/checkout#main
When running this action as a test, I get this error message:
Error: Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under
'/home/runner/work/_actions/<enterprise_name>/<repo_name>/main/checkout'.
Did you forget to run actions/checkout before running your local action?
So my question:
Is there a way to run a forked or local version of actions/checkout? The above example is telling me, I can't run a local version of actions/checkout because I have clone the repo which is ironic error.

You need to push the actions/checkout repo into an internal or public repo on your enterprise. Then update your workflow to reference organization/repo#2 instead.
After forking the actions/checkout repo to my jessehouwing-actions this would result in the following update of the YAML:
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
Would become:
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: jessehouwing-actions/checkout#v3
Don't use submodules.

Related

Cannot trigger GitHub Actions while pull request from a fork repo

There is a private repo and have a GitHub Actions.
If I make pull request between branches in this repo, GitHub Actions triggered correctly.
name: CI
on:
pull_request:
branches:
- pre-production
- production
jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout#v3
with:
fetch-depth: 2
...
Another developer who only has read premission fork this repo, make some commits, then pull request to the Upstream. The GitHub Actions doesn't been triggered. I have confirmed that he pull request to the correct branch.
Is there any setting let other developer who only has read premission trigger the action in Upstream?
Updated:
There is a option in repo settings called "Run workflows from fork pull requests" but I cannot enable it.
Finally, I found a setting called "Run workflows from fork pull requests". Enable it will solve the problem.
If the repo is under an organization, we should enabled it in the organization setting. After that, we can enable it in the repo setting.

Github Actions Error on "Run google-github-actions/setup-gcloud#master" . How to resolve this?

I am using Github actions to deploy my application to Google cloud and it gave me following error -
Run google-github-actions/setup-gcloud#master
Warning: google-github-actions/setup-gcloud is pinned at "master". We strongly advise against pinning to "#master" as it may be unstable. Please update your GitHub Action YAML from:
uses: 'google-github-actions/setup-gcloud#master'
to:
uses: 'google-github-actions/setup-gcloud#v0'
Alternatively, you can pin to any git tag or git SHA in the repository.
Error: On 2022-04-05, the default branch will be renamed from "master" to "main". Your action is currently pinned to "#master". Even though GitHub creates redirects for renamed branches, testing found that this rename breaks existing GitHub Actions workflows that are pinned to the old branch name.
That 3rd party action is moving away from the convention of using a branch named master.
Instead of:
uses: google-github-actions/setup-gcloud#master
Change the reference in your YAML to:
uses: google-github-actions/setup-gcloud#v0

GitHub Workflow - auto push to git repo after pull_request

I host my git-repo on GitHub.com. Several developers contribute code to either the develop- or feature branch respectively. Once all the tests have passed, the changes are merged into master.
Now, once such a pull-request/merge has been done, the master brach shall be pushed automatically to another GitHub-Repo (where another team picks it up). This is when "Workflows" come into play.
So I created this yaml file to trigger a "git push" after a successfull pull-requrest:
name: push master to official repository
on:
pull_request:
types: [closed]
jobs:
gitHubPush:
runs-on: ubuntu-latest
steps:
- run: "git push https://github.com/OFFICIAL/MyProject master"
But this doesn't work. I get:
fatal: not a git repository (or any of the parent directories): .git
Error: Process completed with exit code 128.
The GitHub Account is registered as "Contributor" on OFFICIAL.
Can you help me out ?
You have to checkout your repository first:
- name: Checkout
uses: actions/checkout#v2
More information about checkout options to find the best fitting your needs here: https://github.com/actions/checkout/

GitHub - jobs : what is : use actions/checkout

I saw a lot of uses of :
jobs:
myjob:
steps:
- name: checkout
uses: "actions/checkout#something"
- ...
But i can not find what is the purpose of the line :
uses : "actions/checkout#something"
Is it similar to this ?
run: git checkout something
For this line: uses : "actions/checkout#something", it will use the actions/checkout github action (source here) with the ref something. This ref only refers to the github action version (nothing to do with your repo)
The uses statement refers to a github action that is being used in this step. From github documentation for jobs.<job_id>.steps[*].uses:
Selects an action to run as part of a step
in your job. An action is a reusable unit of code. You can use an
action defined in the same repository as the workflow, a public
repository, or in a published Docker container image.
From actions/checkout readme :
This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.
By default it checks out only one commit. My understanding is that it's doing something similar to:
git fetch --depth 1 origin $GITHUB_REF
This action also persists an auth token in git config. This way, your workflow can run authenticated git commands
By default, it clones your current repository ({{ github.repository }}) but you can also use this action to clone a different repository, and specify additionnal parameters like token, branch, path etc...
An example with additionnal input parameters: check out all git history by setting fetch-depth to 0 (default is 1), see usage doc:
- uses: actions/checkout#v2
with:
fetch-depth: 0
Understanding terminologies made things clearer
Remote repo - It can also be referred to as the origin
Origin - the default name of the remote repo or the source repo being cloned
Head - a reference to human-friendly names for branches
git checkout - switch to a particular branch and displaying the changes currently on that branch
origin/name_of_branch - branch name created when fetching changes from a particular branch on the remote repo
Side Note: When git fetch is used, a custom branch is created locally in the form "origin/name_of_branch", changes on this branch can be viewed locally. These changes are the updated version of the files, not the specific change in that file as seen when commits are being inspected on GitHub.
Back to the question
When the action is executed
jobs:
myjob:
steps:
- name: checkout
uses: "actions/checkout#something"
- ...
The default steps being executed are:
The current repo in which the workflow is being triggered gets cloned.
Depending on the defined events such as a push or pull request:
For a push event, it runs the command below, where $GITHUB_REF points to the latest commit on the specified branch for the push event in the workflow.
git fetch --depth 1 $GITHUB_REF
For pull requests, it checks $GITHUB_REF points to the latest commit on the pull request source branch. This means it points to the would-be code/result from merging the pull request. This is the code/result other steps within the job are executed on such as running builds or tests. (Not completely sure of the command which runs under the hood)
Environment variables being referenced in the commands are explained here.
Additional options can be added to implement specific processes or scenarios such as checking out a different branch. This can be found in the official repo readme.

GitHub Actions : git pull on the server

I have a personal website on a GitHub repo called personal-website and every-time I am making changes locally, I have to SSH into my server (DigitalOcean), go to var/www/personal-website and make a git pull.
I am trying to find a way to pull from the master every-time there is a push into the same branch. I read about GitHub actions and wrote a file on .github/workflows/devops.yml
name: Build and Deploy
on:
push:
branches: master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
GitHub Actions result of Build and Deploy job
On my GitHub Actions page, the job is successful. However, there is no automatic pull request that is done on the server side. How can I fix this?
Make a .sh script and do git pull / git commit / git push