Skip to content

thekubeworld/Github-Actions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 

Repository files navigation

Info and Examples about Github Actions

Github-Actions

Actions based on events, like push, pull or any other.

Workflow -> Jobs -> steps (test code, etc) -> Virtual Env -> Runners

GH Actions - Runners

Runners is the machine that runs the job on top of Github Actions runne and show back the results, can be local (self-host runners) or the machine inside github lab (Linux, Windows or MacOS). Maintenaned by Github, cannot be customized the hardware.

Required tools for sef-hosted runners: $ sudo dnf install curl, git, npm, yarn and pip -y

GH Actions - crontab details

Let's assume users want to execute a job every sunday at 00:00am.

#Every Sunday at 00:00am
   - cron: '0 0 * * 0'

The format follows:

         0      0    *   *    0
second minutes hour day month day-of-week
  |      |       |   |   |    |____________________________ day of week - 0-6 | 
  |      |       |   |   |                                  0 (Sunday), 1 (Monday),
  |      |       |   |   |                                  2 (Tue), 3 (Wed), 
  |      |       |   |   |                                  4 (Thu), 5 (Fri), 6 (Sat)
  |      |       |   |   |
  |      |       |   |   |___________________________ month (1-12)
  |      |       |   |_________________________ day of month (1-31)
  |      |       |______________________ hour (0-23)
  |      |______________ minutes (0-59)
  |__________ seconds (0 - 59)

GH Actions - crontab guru

The website https://crontab.guru is a good resource for creating new crontab jobs schema.

GH Actions - Variables

GH Actions - Workflow Variables

name: 'Variables Example'
# Every push, pull_request (all branches)
on: [push, pull_request]

jobs:
  weekday_job:
    runs-on: ubuntu-latest
    env:
      DAY_OF_WEEK: Mon
      FIRST_NAME: DOUGLAS
    steps:
      - name: "Advise when it's Monday"
        if: env.DAY_OF_WEEK == 'Mon'
        run: echo "Hello $FIRST_NAME, today is Monday!"

GH Actions - Variables - Jobs and Steps

Users can define local variables in jobs or steps Example:

on:
  env:
    username: DOUGLAS
    demo: APPLICATION
jobs:
   VariablesExample:
       runs-on: ubuntu-latest
       env:
         job_var1: "variable JOB 12345"
       steps:
       - name: "Using workflow variables"
         run: echo Hello, $username!
              Welcome to $demo
              job variable $job_var1
              step variable: $step_var1
         env:
           step_var1: "variable step 678910"

GH Actions - Variables - Overwrite

This job will overwrite the value of variable supervar from DOUGLAS to Landgraf

name: CI
on:
  workflow_dispatch:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
env:
  supervar: DOUGLAS

jobs:
  branch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: lastname
        run: echo "supervar=Landgraf" >> $GITHUB_ENV

      - name: Read exported variable
        run: |
          echo "$supervar"
          echo "${{ env.supervar }}"

GH Actions - Default Environment Variables

ENV Variable Description
CI Always set to true
GITHUB_WORKFLOW The name of the workflow
GITHUB_RUN_ID A unique number for each run within a repository.
This number does not change if you re-run the workflow run
GITHUB_RUN_NUMBER A unique number for each run of a particular workflow in a repository.
This number begins at 1 for the workflow's first run, and increments
with each new run. This number does not change if you re-run the workflow run.
GITHUB_ACTION The unique identifier (id) of the action.
GITHUB_ACTIONS Always set to true when GitHub Actions is running the workflow.
You can use this variable to differentiate when tests are being
run locally or by GitHub Actions.
GITHUB_ACTOR The name of the person or app that initiated the workflow.
For example, octocat.
GITHUB_REPOSITORY The owner and repository name. For example, octocat/Hello-World.
GITHUB_EVENT_NAME The name of the webhook event that triggered the workflow.
GITHUB_EVENT_PATH The path of the file with the complete webhook event payload.
For example, /github/workflow/event.json.
GITHUB_WORKSPACE The GitHub workspace directory path. The workspace directory is a copy of your
repository if your workflow uses the actions/checkout action. If you don't use
the actions/checkout action, the directory will be empty.
For example, /home/runner/work/my-repo-name/my-repo-name.
GITHUB_SHA The commit SHA that triggered the workflow.
For example, ffac537e6cbbf934b08745a378932722df287a53
GITHUB_REF The branch or tag ref that triggered the workflow.
For example, refs/heads/feature-branch-1. If neither a branch or tag is available
for the event type, the variable will not exist
GITHUB_HEAD_REF Only set for pull request events. The name of the head branch.
GITHUB_BASE_REF Only set for pull request events. The name of the base branch.
GITHUB_SERVER_URL Returns the URL of the GitHub server. For example: https://github.com.
GITHUB_API_URL Returns the API URL. For example: https://api.github.com.
GITHUB_GRAPHQL_URL Returns the GraphQL API URL. For example: https://api.github.com/graphql.

Note: If you need to use a workflow run's URL from within a job, you can combine these environment variables: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID

Based on this doc: Docs GithHub Actions - Default Environment Variables

GH Actions - workflow dispatch

You will see a ‘Run workflow’ button on the Actions tab, to easily trigger a run.

name: "CNI daily test"
on:
  workflow_dispatch:
  schedule:
    # Daily, 3:25pm UTC
    - cron: '25 15 * * *'

GH Actions - Input

Users can manually trigger job via workflow_dispatch. If required users can define inputs (variables) to change during the manual triggers.

on:
  workflow_dispatch:
    inputs:
      environment:
        description: 'Define env name'
        required: true
        default: 'myenv'
      branch:
        description: 'Define branch name'
        required: true
        default: 'main'

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Environment: ${{ github.event.inputs.environment }}"
        echo "Branch: ${{ github.event.inputs.branch }}"

GH Actions - Examples

GH Actions - if

name: CI
on: push
jobs:
  prod-check:
    if: ${{ github.ref == 'refs/heads/main' }}
    runs-on: ubuntu-latest
    steps:
      - run: echo "Deploying to production server on branch $GITHUB_REF"

In this example, the if statement checks the github.ref context to determine the current branch name; if the name is refs/heads/main, then the subsequent steps are executed. The if check is processed by GitHub Actions, and the job is only sent to the runner if the result is true. Once the job is sent to the runner, the step is executed and refers to the $GITHUB_REF environment variable from the runner.

GH Actions - Run multi lines scripts

Runs a set of commands using the runners shell
- name: Run a multi-line script
  run: |
    echo Add other actions to build,
    echo test, and deploy your project.

GH Actions - Jobs at every 5 minutes

Job every 5 minutes

name: "Antrea Cronjob"
on:
  schedule:
    - cron:  "*/5 * * * *"
jobs:
  antrea-job:
    runs-on: [ ubuntu-latest ]
    steps:
    - uses: actions/checkout@v2
    - name: Run
      working-directory: ./antrea
      run: ./setup.sh

GH Actions - Jobs at every 15 minutes

on:
  schedule:
    - cron:  '*/15 * * * *'    # At every 15 minutes

GH Actions - Jobs based on directory changes

name: "Calico test"
# If any file change in the Calico path a new build will be triggered
on:
  push:
    branches: [ main ]
    paths:
      - calico/**

jobs:
  calico-job:
    runs-on: [ ubuntu-latest ]
    steps:
    - uses: actions/checkout@v2
    - name: Run
      working-directory: ./calico
      run: ./setup.sh

GH Actions - Run the job once a week

Every Sunday at 00:00am

on:
  schedule:
    # Every Sunday at 00:00am
    - cron: '0 0 * * 0'

Every Monday at 1:05am

on:
  schedule:
    # Every Monday at 1:05am
    - cron: '5 1 * * 1'

GH Actions - Depending from another Job

Below example: the job build depends on setup.
The statement is: needs: setup.

name: CI

on: [push]

jobs:
  setup:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - run: ./setup_test_infrastructure.sh
  build:
    needs: setup
    runs-on: 'ubuntu-latest'
    steps:
      - uses: actions/checkout@v1
      - run: |
          ./build.sh
          ./test.sh

GH Actions - Secrets

Secrets is a good way to hide passwords in the CI/CD jobs, specially in the logs. Users will note that even doing echo ${{secrets.MYSECRET}} the real password won't show. It will show *** instead.

To create: First, create a secret in the project Second, use in your yaml file.

name: CI
on:
  workflow_dispatch:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
env:
  supervar: ${{secrets.MY_CREATED_SECRET_NAME}

jobs:
  branch:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Pass secret to mycommand
        run: |
          my_command "${{ env.supervar }}"

GH Actions - GITHUB TOKEN

Example 1:

Passing GITHUB_TOKEN as an input This example workflow uses the labeler action in PR, which requires the GITHUB_TOKEN as the value for the repo-token input parameter:

name: Pull request labeler
on:
- pull_request_target
jobs:
  triage:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/labeler@v2
      with:
        repo-token: ${{ secrets.GITHUB_TOKEN }}

Example 2:

You can use the GITHUB_TOKEN to make authenticated API calls. This example workflow creates an issue using the GitHub REST API:

name: Create issue on commit
on:
- push
jobs:
  create_commit:
    runs-on: ubuntu-latest
    steps:
    - name: Create issue using REST API
      run: |
        curl --request POST \
        --url https://api.github.com/repos/${{ github.repository }}/issues \
        --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
        --header 'content-type: application/json' \
        --data '{
          "title": "Automated issue for commit: ${{ github.sha }}",
          "body": "This issue was automatically created by the GitHub Action workflow **${{ github.workflow }}**. \n\n The commit hash was: _${{ github.sha }}_."
          }' \
        --fail

GH Actions - Artifacts store

To store the artifacts users must use actions/upload-artifact@v2

name: "Antrea v0.12.2 job to generate cyclonus artifacts"
on:
  workflow_dispatch:
  schedule:
    # Daily, 3:00pm
    - cron: '00 15 * * *'

jobs:
  antrea-job-v0-12-2-artifacts:
    runs-on: [ ubuntu-latest ]
    steps:
      - uses: actions/checkout@v2
        with:
           path: main

      - uses: actions/checkout@master
        with:
          repository: K8sbykeshed/k8s-local-dev
          path: k8s-local-dev

      - run: |
          pushd ./k8s-local-dev
            ANTREA_VERSION=v0.12.2 ./k8s-local-dev antrea
          popd

          pushd ./main
            JOB_YAML="./jobs/antrea/cyclonus-job.yaml" DIR_CNI="antrea" ./run-cyclonus-job.sh
          popd

      - uses: actions/upload-artifact@v2
        with:
          name: logs
          path: ./main/downloads/
          retention-days: 60

GH Actions - Artifacts passed to another job in the workflow

on: [push, pull_request]
jobs:
  FoobarStep
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: FoobarStep
      run: |
        cd /path
        build-my-app
    
    - uses: actions/upload-artifact@v2
      with:
        name: my-artifact
        path: "**/bin/myapp-path/"

  NextStep:
    runs-on: ubuntu-latest
    need: FoobarStep
    steps:
    - uses: actions/download-artifact@v2
      with:
        name: my-artifact

See also: https://docs.github.com/en/actions/guides/storing-workflow-data-as-artifacts

GH Actions - Cache

- name: Cache multiple paths
  uses: actions/cache@v2
  with:
    path: |
      ~/cache
      !~/cache/exclude
    key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}

More info: https://github.com/actions/cache

GH Actions - Service Container

Service containers are Docker containers that provide a simple and portable way for you to host services that you might need to test or operate your application in a workflow. For example, your workflow might need to run integration tests that require access to a database and memory cache.
You can configure service containers for each job in a workflow. GitHub creates a fresh Docker container for each service configured in the workflow, and destroys the service container when the job completes. Steps in a job can communicate with all service containers that are part of the same job.

For more info see:
Github Actions - Service Container
Github Actions - Service Container - PostgreSQL
Github Actions - Service Container - Redis

GH Actions - Creating an Action based on Docker image

See: Github Actions - Creating a Docker Action

GH Actions - Mirroring Github Repos

Instruction how to mirror github projects via Github Actions with Git Sync Action

  1. Generate the ssh key (private/pub).
    The command below will generate two files in ~/.ssh dir
  • mirroring_SSH_KEY (private key)
  • mirroring_SSH_KEY.pub (public key)
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"  -f ~/.ssh/mirroring_SSH_KEY
  1. At the Destination Project on Github
    -> Settings -> Deploy Keys:

Title: SSH_PUBLIC_KEY.pub
Key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDG3FdclV4JHvBED5YnxxTO0pdaEdUUaLagvG8ezbBYKUADAiOpmapa2P5SklQJGzKcJpu74JpvEuP52WjYwYYITM/5GQmWhFxLb73/foP5DQWrhak4fzWf3dQeAF+Gg68omUaa83noLaxQEIkgKaeKm3uimxsxE1f8n+X5lU4Yu/F9FYpOI9W4yd+KcVBsWboP1NeiUDnTR0piTmywYyRqMAGfdb1315xGgh+6sdS7snPc4J+Ff8+QDiaQUMOsBffTM3B1Fi+FEnu30951rCiU0K7MxxNPvih5mpnC+EJRJkJ7mhYK3BV0Sgu6a87ym2IcakYstAVHAeyFrbNe/Rb8jCAO+lmo1gks7ykuaJurCTEgUXrwvbYXDz09M9nR5POpksuDe3BUOSoEzReaigD5HvAsc/uxwfSYzg6E8fiIzL2UXHP6O4egdUd7NdkTwpX0AkvmA4TBpVk9GN2+WXe3EVw== [email protected]

[x] Allow write access -> Click: Add Secret

  1. At the Source Project on Github
    -> Settings -> Secrets -> New repository secret

Title: SSH_PRIVATE_KEY
Key: -----BEGIN OPENSSH PRIVATE KEY----- b3BlbnNzaC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gNS5iURsvhO3zZK HpMKNdM5EhQjScFQkJSCTkf+C8pDXpWHpUt5LgSWLbnD49yD12QS/fJ+9weT1r/4hHo39n -----END OPENSSH PRIVATE KEY-----

Click: Add Secret

  1. On the Source github tree (the one will contain the action):
$ git-source-project> mkdir -p .github/workflows 
$ cd .github/workflows
$ vi syncrepo.yml
name: Mirror
on:
  workflow_dispatch:
  schedule:
    - cron:  '*/15 * * * *'    # At every 15 minutes

jobs:
  to_mirror:
    runs-on: ubuntu-latest
    steps:
      - name: sycnmain
        uses: wei/git-sync@v3
        with:
          source_repo: "[email protected]:SOURCEUSER/SOURCEREPO.git"
          source_branch: "main"
          destination_repo: "[email protected]:DESTINATIONUSER/DESTINATIONREPO.git"
          destination_branch: "main"
          ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }} 
      - name: synctags
        uses: wei/git-sync@v3
        with:
          source_repo: "[email protected]:thekubeworld/k8s-local-dev.git"
          source_branch: "refs/tags/*"
          destination_repo: "[email protected]:K8sbykeshed/k8s-local-dev.git"
          destination_branch: "refs/tags/*"
          ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
  1. On the Github url from the Source Project, click in:

    • the tab Actions
    • Mirror (the new job) -> Run workflow -> Branch: main -> Run workflow
  2. As it has mirrored the source repo to the destination, now disable the Github Actions in the Destination repo only,
    otherwise, it will keep failing and sending emails to you

GH Actions - Golint

https://github.com/golangci/golangci-lint-action

GH Actions - Community

Users can exchange knowledge via the community around github actions.

GH Actions - Docs and Books

Docs GitHub Actions - Default Environment Variables
Docs GitHub Actions - Create secret in the project
Docs Github Actions - GITHUB_TOKEN
Docs Github Actions - Storing Workflow data as Artifacts
Github Actions Cache
Github Actins Runners on local Kubernetes
Github Actions - Service Container
Github Actions - Service Container - PostgreSQL
Github Actions - Service Container - Redis
Github Actions - Creating a Docker Action
Hands-on GitHub Actions

About

Info and Examples about Github Actions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published