What is GitHub Actions and why is it used?
What are workflows in GitHub Actions?
.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?
What is a runner in GitHub Actions?
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?
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?
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?
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?
act to test workflows locally before pushing to GitHub.