Study Guide 2023+

git

Warning: These notes are partial, ongoing, incomplete, and may contain typos/inaccuracies. (They are kept factually accurate, time permitting.)

They are being united from many disparate notes created in the past and the layout/organization will gradually improve with time!

Please view them on a computer as they are not optimized for mobile (although you can still view them on Mobile along with the Flashcards at your own risk)!

Topics and code examples are lazy-loaded and may require two-clicks from the TOC to correctly calculate the updated x,y coordinates (after rendering). Thanks!

Git: Quick Reference Sheet

Some common and useful Git commands.

Checkout New Branch From Origin

  1. git fetch origin my_branch
  2. git pull
  3. git checkout my_branch

View Branch Commit History

  1. git log

View Entire Local Change History

  1. git reflog

View File Changes

By SHA:

  1. git diff --name-only af43c41d..HEAD
  2. git diff af43c41d..master

By branch:

  1. git diff --name-only origin/deploy..master
  2. git diff origin/deploy..master

Correct Previous Commits

Review, alter, remove, amend last 3 commits:

  1. git rebase -i HEAD~3
  2. Type i to enter interactive mode.
  3. Find the line with the desired commit hash. Modify it using pick, drop, etc.
  4. Hit the esc button to exit interactive mode.
  5. Type wq to save and close the file (Git will proceed through the stipulated changes) or type q! to close the file abandoning all changes.
  6. git push -f to override previous changes - do not use this on master/main only ever within a development branch.

Git Amend

Correct the last commit message:

  1. git commit --amend -m "Your new message"

Discard Uncommitted Branch Charge

  1. git clean -f -d
  2. git reset --hard HEAD

Abandon a Rebase

  1. git rebase --abort
  2. git clean -f -d
  3. git reset --hard HEAD

Change Branch W/ Same Name As Dir

If a branch contains a dir in the root with the same name as a branch, Git will now complain.

Use the following instead:

  1. git fetch origin deploy (if freshly cloned)
  2. git switch -f deploy

Set Environment Config

  1. git config --global user.username "my_username"
  2. git config --global user.email "my_email@email.com"

Disable automatic conversion of Unix line endings to Dos/Windows ones (on Windows):

  1. git config --global core.autocrlf false
  1. https://devhints.io/vim
  2. https://git-scm.com/doc
  3. https://toolslick.com/conversion/text/dos-to-unix
  4. https://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf
  5. https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings#global-settings-for-line-endings
  6. https://jollygood.prose.sh/vim-in-git-for-windows

GitHub Actions: Overview

GitHub Actions (the tool itself) supports Workflow and CI/CD automation through GitHub.

Official Documentation: https://docs.github.com/en/actions/about-github-actions/understanding-github-actions

Very helpful exercises: https://learn.microsoft.com/en-us/collections/n5p4a5z7keznp5

GitHub Actions

GitHub Actions are packaged scripts to automate tasks through GitHub.

There are three kinds of GitHub Actions:

  1. Container Actions - where a Linux Environment comprises part of the Action.

    # Example
    name: "Hello Actions"
    description: "Greet someone"
    author: "octocat@github.com"
    
    inputs:
        MY_NAME:
            description: "Who to greet"
            required: true
            default: "World"
    
    runs:
        uses: "docker"
        image: "Dockerfile"
    
    branding:
        icon: "mic"
        color: "purple"
    

    https://github.com/Thoughtscript/example-container-action

    https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-docker-container-action

  2. JavaScript Actions - execute JavaScript as an Action.

    name: 'Hello World'
    description: Simple example
    
    inputs:
      myinput:  # id of input
        description: My input arg
        required: true
        default: "I am a string"
    
    outputs:
      myoutput: # id of output
        description: Output of the function
    
    runs:
      using: node20
      main: script.js
    

    https://github.com/Thoughtscript/example-js-action

    https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-javascript-action

  3. Composite Actions - combine multiple Workflow Steps together into one Action.

    name: 'Hello World'
    description: 'Greet someone'
    
    inputs:
      who-to-greet:  # id of input
        description: 'Who to greet'
        required: true
        default: 'World'
    
    runs:
      using: "composite"
      steps:
        - name: Set Greeting
          run: echo "Hello $INPUT_WHO_TO_GREET."
          shell: bash
          env:
            INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}
    
        # ...
    
        - name: Run goodbye.sh
          run: goodbye.sh
          shell: bash
    

https://github.com/Thoughtscript/example-composite-action

The above are characterized by having:

  1. inputs and/or outputs
  2. runs and using

GitHub Workflow

name: A workflow for my Hello World file
on: push
  jobs:
    build:
      name: Hello world action
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v1
        - uses: ./action-a
          with:
            MY_NAME: "Mona"

https://github.com/Thoughtscript/example-workflow

https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-composite-action

The Anatomy of a GitHub Action

Workflow > Job(s) > Step(s) > Action(s) defined in a YAML file.

  1. A Workflow defines one or more Jobs.
  2. A Job defines one or more Steps.
    • A Job has an associated Runner that executes the Job.
    • (Think Runnable or Callable in Java.)
  3. A Step defines one or more Actions.
    • A Task with multiple commands.
  4. An Action is a discrete command.
    • (Think RUN in Docker.)

GitHub Integration

Organizations and users typically integrate their GitHub Repositories with GitHub Actions:

  1. Define a workflow.yaml file in the root of some Source Code.
  2. The Source Code is checked into a GitHub Repository.
  3. The GitHub Repository is associated with GitHub Secrets or any integrations through the GitHub User Interface.
  1. https://docs.github.com/en/actions/about-github-actions/understanding-github-actions
  2. https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-docker-container-action
  3. https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-javascript-action
  4. https://docs.github.com/en/actions/sharing-automations/creating-actions/creating-a-composite-action
  5. https://learn.microsoft.com/en-us/collections/n5p4a5z7keznp5

Code samples:

  1. https://github.com/Thoughtscript/example-workflow
  2. https://github.com/Thoughtscript/example-js-action
  3. https://github.com/Thoughtscript/example-container-action
  4. https://github.com/Thoughtscript/example-composite-action

GitHub Actions: Advanced Topics

Triggers

Reference list for available trigger conditions: https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows.

Use like so:

name:
on:
  issues:
    types: [opened, edited, milestoned]

https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/triggering-a-workflow

Combining Actions

GitHub Actions can also be combined or composed.

Typified by a uses YAML black:

name: my_example
on:
  #...

jobs:
  tag:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Run test
      run: |
        pytest test.py

https://github.com/actions

Environment Variables

Can define Environment Variables that can be used elsewhere in the Workflow:

#...
env:
  AWS_REGION: MY_AWS_REGION              
  ECR_REPOSITORY: MY_ECR_REPOSITORY           
  ECS_SERVICE: MY_ECS_SERVICE                
#...

Default Environment Variables:

  1. Are prefixed with GITHUB_.
  2. Defined by GitHub and not within a Workflow.
  3. Have an associated Context property.

Default Environment Variables: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables

Advanced Expressions

GitHub Actions supports many complex Expressions, Operators, and Functions (as YAML keys or values, depending):

  1. Numeric Boolean: <=, >=, ==, !=, etc.
  2. Literals: ${{ 'I''m a string and I need tic marks around me in here!' }}, ${{ -9.2 }}
  3. Logical Boolean: &&, ||, !, etc.
  4. YAML Conditional Boolean: if: ${{ success() }}, etc.
  5. String: contains('Hello world', 'llo'), etc.
  6. Parsing: toJSON(value), etc.
  7. Dynamic Variable Setting: >>

https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions

  1. https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/triggering-a-workflow
  2. https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows
  3. https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables#default-environment-variables
  4. https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/evaluate-expressions-in-workflows-and-actions

GitHub Actions: Integration

GitHub Secrets

GitHub Actions can integrate with GitHub Secrets to define any Secrets, Credentials, or Tokens required by the CI/CD or Workflow:

  1. These are defined in the GitHub User Interface available through Settings > Secrets and variables > Actions > Actions secrets and variables.
  1. And passed as values into YAML like so: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}.

Some services handle the actual retrieval, refreshing, and obtaining of Tokens through the above.

Many Cloud Providers offer prepublished GitHub Actions that perform certain operations (such as logging into AWS) that can be used in a uses block.

In other cases one may need to define a command that calls some say OAUTH 2.0 REST API and stores the dynamic token (using >>) before making subsequent calls (per usual token auth flows):

name: Create issue on commit

on: [ push ]

jobs:
  my_issue:
    runs-on: ubuntu-latest
    # ...
    steps:
      - name: Create issue using REST API
        run: |
          curl --request POST \
          --url https://myoauthserverendpoint \ ...

https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions

Terraform

Terraform HCP can integrate with GitHub Actions (and with Terraform Cloud Providers).

https://developer.hashicorp.com/terraform/tutorials/automation/github-actions

AWS

https://aws.amazon.com/blogs/devops/integrating-with-github-actions-ci-cd-pipeline-to-deploy-a-web-app-to-amazon-ec2/

Azure

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      # Checkout the repo
      - uses: actions/checkout@main
      - uses: azure/login@v1
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# ...

https://learn.microsoft.com/en-us/azure/app-service/deploy-github-actions?tabs=openid%2Caspnetcore

  1. https://docs.github.com/en/actions/security-for-github-actions/security-guides/using-secrets-in-github-actions
  2. https://aws.amazon.com/blogs/devops/integrating-with-github-actions-ci-cd-pipeline-to-deploy-a-web-app-to-amazon-ec2/
  3. https://learn.microsoft.com/en-us/azure/app-service/deploy-github-actions?tabs=openid%2Caspnetcore

GitHub Actions: Enterprise

GitHub Actions supports many features for Enterprise operations.

Templates

  1. GitHub Actions Templates can be defined to encourage standards and reuse.
  2. These are similar to GitHub Pull Request Templates.
  3. These are basically prepopulated but blank YAML files that can be used as a starting point.

https://docs.github.com/en/actions/writing-workflows/using-workflow-templates

Organization Policies

  1. GitHub Actions Policies can be defined to restrict who can do what.
  2. These are similar to GitHub Organizational Policies.

https://docs.github.com/en/enterprise-cloud@latest/admin/enforcing-policies/enforcing-policies-for-your-enterprise/enforcing-policies-for-github-actions-in-your-enterprise

  1. https://docs.github.com/en/actions/writing-workflows/using-workflow-templates
  2. https://docs.github.com/en/enterprise-cloud@latest/admin/managing-github-actions-for-your-enterprise/getting-started-with-github-actions-for-your-enterprise/introducing-github-actions-to-your-enterprise
  3. https://docs.github.com/en/enterprise-cloud@latest/admin/enforcing-policies/enforcing-policies-for-your-enterprise/enforcing-policies-for-github-actions-in-your-enterprise

GitHub Actions: Misc.

Misc. study items:

  1. [skip ci], [ci skip], [no skip], [skip actions], [actions skip]
  2. Use | for a multiline string (to run multiple commands in a single step - not &&)
  3. Default permission levels that can be assigned to GITHUB_TOKEN: none,write,read.
  4. Multiple jobs will run in parallel by default.
  5. needs keyword specifies that one job requires another.
  6. $ vs ${{ ... }}
    runs:
      using: "composite"
      steps:
        - name: Set Greeting
          # Use the ENV value
          run: echo "Hello $INPUT_WHO_TO_GREET."
          shell: bash
          env:
            # Set the ENV value
            INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}
    
  7. Debugging syntax from within a Step: echo "::debug::Set the Octocat variable".
  8. OIDC is recommended for security hardening.
  9. Workflow triggering events:
  10. Status Check Functions:
  11. steps.<step_id>.outcome
  12. Branch Filters use Glob Patterns:
  13. Disabling