Code Monkey home page Code Monkey logo

gita's Introduction

Gita

Gita

GitHub Actions practice repository.

Basic concepts. Built following GitHub's tutorial.

Index

What's this?

GitHub Actions is a CI/CD tool, that allows you to set up workflows to be run automagically, whenever a trigger you set goes off (like whenever you push to the repository). Its main use is to automate builds, tests and deployments of your app.

Key Terms

Runner

A virtual machine that is used to run the things (jobs) defined in your workflow.

Workflow

A workflow is the set of things (jobs) you want to happen when other things (triggers) happen.

Job

Workflows define one or more jobs, which contain shell script or an Action. They are basically commands that will run in your runner.

Actions

Predefined workflow, by you or by the community, for common, repetitive workflows.

Workflow YAML file

The workflow is configured in a YAML file. The basic syntax is as follows:

name: name-to-appear-in-repository-Actions-tab
on: [push] # on creates a trigger to run the workflow. [push] means the action should run on every push to every branch
jobs: # groups all jobs in this workflow
  name-of-a-job-you-are-defining:
    runs-on: ubuntu-latest # defines the runner for the job. In this case, ubuntu
    steps: # groups all steps (actions or shell scripts) for the job
      - uses: actions/checkout@v2 # calls the v2 of actions/checkout. Should be used whenever a workflow runs against repository code
      # the @{version} accepts a version tag, a version SHA or a branch name.
      - uses: actions/setup-node@v2 # node setup community defined action.
      - uses: ./.github/actions/directory-with-user-defined-actions # user defined action.
      # note that ./ refers to the root of the repository.
      - uses: docker://alpine3.8 # to get an Action defined on a Docker Hub image
        with:
          node-version: '14' # installs node on the runner
      - run: npm install -g bats # run a command in runner. In this case, install a dependency
      - run: bats -v # checks the version of bats installed, to check if it was successfully installed.

Customizing Workflows

Env Variables

You can add environment variables to your workflow by adding an env field in the steps of a job:

jobs:
  example-job:
      steps:
        - name: Connect to PostgreSQL
          run: node client.js
          env:
            POSTGRES_HOST: postgres
            POSTGRES_PORT: 5432

Scripts

Some scripts you can run directly (like the npm script in the example). To use shell scripts for example, add use the run keyword followed by the path to the script, and shell followed by the name of the shell you wish to run on:

jobs:
  example-job:
    steps:
      - name: Run build script
        run: ./.github/scripts/build.sh
        shell: bash

Artifacts

Artifacts are files created during builds or tests in a workflow, and are accessible by all jobs within a same workflow. Actions and workflows called inside a run have write access to its artifacts.

The following example creates an artifact (output.log) using a shell command and accesses it from another action in the same job (upload-artifact):

jobs:
  example-job:
    name: Save output
    steps:
      - shell: bash
        run: |
          expr 1 + 1 > output.log
      - name: Upload output file
        uses: actions/upload-artifact@v2
        with:
          name: output-log-file
          path: output.log

Expressions

Expressions can be used to evaluate environment variables or access context. They are composed of literals, context references and functions.

To tell GitHub some text is an expression and not a string, soround it with ${{ }}. You can ommit this when using if.

Supported Data Types

bool, null, number and string (also object and array). Examples:

some-bool: ${{ true }}
some-integer: ${{ 3 }}
some-float: ${{ 1.1 }}
some-string: hi i'm a string
some-other-srting: ${{ 'another string' }} # double quote " won't work

Supported Operators

() [] . ! < <= > >= == != && ||

more info here.

Github equality comparisons

  • Dinamically typed (0 == "0");
  • Arrays and objects only equal if same instance;
  • NaN != NaN;
  • Case insensitive.

Functions

GitHub has a series of built-in functions you can use to manipulate strings and arrays. For a comprehensive list, look here.

Status Check Functions

These functions are used as arguments for an if conditional. You can also compare explicitly, e.g: ${{ job.status == 'success' }}

success()

Returns true if all steps before succeeded.

always()

Causes the step to execute, even if preceded by a (non-critical) failure

cancelled()

Returns true if the workflow was cancelled.

failure()

If any previous step failed.

Object Filters

Use * to get matching items from an object.

[
  { "name": "apple", "quantity": 1 },
  { "name": "orange", "quantity": 2 },
  { "name": "pear", "quantity": 1 }
]

The filter fruits.*.name returns the array [ "apple", "orange", "pear" ]

Contexts

Contexts are objects that store information about workflow runs, runner environment, jobs and steps. They are accessible through expression syntax. For more details, look here.

The following example uses the github context object to run a job is something is pushed to main:

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"

The function toJSON allows to pretty-print JSON objects to the log. Check out the example.

gita's People

Contributors

dependabot[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.