• What is GitHub Actions and why is it used?

    • Answer: GitHub Actions is a CI/CD (Continuous Integration and Continuous Deployment) platform that allows you to automate, customize, and execute your software development workflows right in your GitHub repository. It is used to automate tasks such as building, testing, and deploying code, managing issues, and running code analysis.
  • What are workflows in GitHub Actions?

    • Answer: Workflows are automated processes defined in YAML files within the .github/workflows directory of a GitHub repository. They specify the events that trigger the automation and the series of jobs and steps to execute in response.
  • What are jobs and steps in GitHub Actions?

    • Answer: Jobs are a collection of steps that execute on the same runner. Each job runs in its own virtual environment and can include multiple steps. Steps are individual tasks that make up a job and can include actions, shell commands, or scripts.
  • What is a runner in GitHub Actions?

    • Answer: A runner is a server that executes your workflow's jobs. GitHub provides hosted runners with common environments, but you can also use self-hosted runners for more control over the hardware and software used to run jobs.
  • How do you trigger a workflow in GitHub Actions?

    • Answer: Workflows can be triggered by various events such as push, pull request, schedule, or manual triggers. For example, a workflow triggered by a push event might look like this:

      on: push
      
  • How do you define a workflow file in GitHub Actions?

    • Answer: A workflow file is defined in YAML syntax and is placed in the .github/workflows directory of your repository. Here’s an example of a simple workflow file:

      yamlCopy code
      name: CI
      
      on: [push, pull_request]
      
      jobs:
        build:
          runs-on: ubuntu-latest
      
          steps:
          - uses: actions/checkout@v2
          - name: Set up Node.js
            uses: actions/setup-node@v2
            with:
              node-version: '14'
          - run: npm install
          - run: npm run build
          ```
      
  • What is the uses keyword in GitHub Actions?

    • Answer: The uses keyword specifies an action to run as part of a step in a job. Actions can be from the GitHub Marketplace, public repositories, or local files in your repository. For example:

      - uses: actions/checkout@v2
      
  • How can you pass secrets to GitHub Actions workflows?

    • Answer: Secrets can be passed to workflows using GitHub’s secrets management. Secrets are stored securely in the repository settings and can be referenced in workflows using the secrets context. For example:

      env:
        MY_SECRET: ${{ secrets.MY_SECRET }}
      
  • How do you create a custom action in GitHub Actions?

    • Answer: A custom action can be created by defining an action in a repository. Custom actions can be written in JavaScript, Docker, or as composite actions. Here’s an example of a JavaScript action:
      • Create an action.yml file:

        yamlCopy code
        name: 'My Custom Action'
        description: 'An example custom action'
        runs:
          using: 'node12'
          main: 'index.js'
        inputs:
          name:
            description: 'Name to greet'
            required: true
            default: 'World'
        
        
      • Create an index.js file:

        javascriptCopy code
        const core = require('@actions/core');
        
        try {
          const name = core.getInput('name');
          console.log(`Hello ${name}!`);
        } catch (error) {
          core.setFailed(error.message);
        }
        
        
  • How do you handle workflow dependencies in GitHub Actions?

    • Answer: Workflow dependencies can be managed using the needs keyword to specify that a job should only run after another job completes successfully. For example:

      yamlCopy code
      jobs:
        build:
          runs-on: ubuntu-latest
          steps:
          - run: echo "Building..."
      
        test:
          runs-on: ubuntu-latest
          needs: build
          steps:
          - run: echo "Testing..."
      
      
  • What are reusable workflows in GitHub Actions and how do you use them?

    • Answer: Reusable workflows allow you to define a workflow in one repository and reuse it in other repositories. This promotes DRY (Don't Repeat Yourself) principles. Here’s how you use a reusable workflow:
      • In the reusable workflow repository (.github/workflows/reusable-workflow.yml):

        yamlCopy code
        name: reusable-workflow
        on:
          workflow_call:
            inputs:
              username:
                required: true
                type: string
        jobs:
          greet:
            runs-on: ubuntu-latest
            steps:
            - run: echo "Hello ${{ inputs.username }}!"
        
        
      • In the calling repository (.github/workflows/main.yml):

        yamlCopy code
        jobs:
          call-reusable-workflow:
            uses: owner/repo/.github/workflows/reusable-workflow.yml@main
            with:
              username: 'GitHub User'
        
        
  • How can you debug GitHub Actions workflows?

    • Answer: Debugging GitHub Actions workflows can be done by enabling debug logging, using echo statements to print values, and checking the detailed logs provided by GitHub Actions. To enable debug logging, you can set the ACTIONS_RUNNER_DEBUG and ACTIONS_STEP_DEBUG secrets to true.
  • What are some best practices for writing GitHub Actions workflows?

    • Answer: Some best practices include:
      • Modularizing workflows: Break down complex workflows into smaller, reusable workflows.
      • Using secrets: Store sensitive information securely using GitHub Secrets.
      • Caching dependencies: Speed up builds by caching dependencies.
      • Testing locally: Use tools like act to test workflows locally before pushing to GitHub.
      • Minimizing environment dependencies: Use containerized environments to ensure consistency across runs.
      • Writing clear and maintainable YAML: Use comments and consistent formatting to make workflows easy to read and maintain.