mirror of
https://github.com/Fechin/reference.git
synced 2026-12-01 01:14:29 +03:00
fix: trivial formatted posts
Every other file's format which was automatically converted correctly. Each has been manually checked and they should all be visually the same.
This commit is contained in:
97 files changed
+10250
-10850
No files matched your search
+162
-152
@@ -1,27 +1,33 @@
|
||||
---
|
||||
title: Github Actions
|
||||
date: 2023-11-28
|
||||
icon:
|
||||
icon:
|
||||
background: bg-[#808080]
|
||||
tags:
|
||||
- Github
|
||||
- CICD
|
||||
categories:
|
||||
- Toolkit
|
||||
intro: A quick reference to Github Actions for the automation of software workflows, allowing developers to build, test, and deploy code right from their GitHub repositories.
|
||||
plugins:
|
||||
intro:
|
||||
A quick reference to Github Actions for the automation of software workflows, allowing developers to build, test, and
|
||||
deploy code right from their GitHub repositories.
|
||||
plugins:
|
||||
- copyCode
|
||||
---
|
||||
|
||||
Getting Started
|
||||
------------
|
||||
## Getting Started
|
||||
|
||||
### Introduction
|
||||
[GitHub Actions](https://docs.github.com/en/actions) is a CI/CD platform that enables automation of software workflows, allowing developers to build, test, and deploy code right from their GitHub repositories.
|
||||
|
||||
----
|
||||
[GitHub Actions](https://docs.github.com/en/actions) is a CI/CD platform that enables automation of software workflows,
|
||||
allowing developers to build, test, and deploy code right from their GitHub repositories.
|
||||
|
||||
---
|
||||
|
||||
### Workflow Files
|
||||
Github Action Workflows are defined in special YAML files, typically stored in a `.github/workflows` directory in the github repository.
|
||||
|
||||
Github Action Workflows are defined in special YAML files, typically stored in a `.github/workflows` directory in the
|
||||
github repository.
|
||||
|
||||
```yaml
|
||||
name: hello-world
|
||||
@@ -39,9 +45,10 @@ jobs:
|
||||
- On GitHub.com, navigate to the main page of the repository.
|
||||
- Under your repository name, click `Actions`.
|
||||
- In the left sidebar, click the workflow you want to display, in this example "hello-world"
|
||||
----
|
||||
|
||||
### Workflow Syntax
|
||||
---
|
||||
|
||||
### Workflow Syntax
|
||||
|
||||
```yaml
|
||||
name: learn-github-actions
|
||||
@@ -54,25 +61,25 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '14'
|
||||
node-version: "14"
|
||||
- run: npm install -g bats
|
||||
- run: bats -v
|
||||
```
|
||||
|
||||
### Workflow Syntax Descriptions
|
||||
|
||||
| Line | Description |
|
||||
| :--- | :--- |
|
||||
| `name:` | Sets the name of the GitHub Actions workflow. It's a label used for identifying the workflow in the repository. |
|
||||
| `run-name:` | Sets a custom name for the run, using a GitHub context `${{ github.actor }}` to include the name of the user who initiated the run. |
|
||||
| `on:` | Specifies the event that triggers the workflow. In this case, the workflow is triggered on any `push` event to the repository. |
|
||||
| `jobs:` | Defines a group of jobs that will be executed as part of the workflow. Each job runs independently in the workflow. |
|
||||
| `check-bats-version:` | Identifier for a specific job within the workflow. This job is named `check-bats-version`. |
|
||||
| `runs-on:` | Specifies the type of machine to run the job on. Here, it's set to run on the latest version of Ubuntu. |
|
||||
| `steps:` | Contains a sequence of tasks (steps) that will be executed as part of the job. |
|
||||
| `uses:` | Used to specify an action to include as part of a step. For example, `actions/checkout@v4` checks out the repository, and `actions/setup-node@v3` sets up a Node environment. |
|
||||
| `with:` | Specifies additional parameters for the action. It's used in conjunction with `uses` to configure the action. |
|
||||
| `node-version:` | Contains parameter under `with`, specifying the version of Node.js to be set up by the `setup-node` action. In this case, it's set to version '14'. |
|
||||
| Line | Description |
|
||||
| :-------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `name:` | Sets the name of the GitHub Actions workflow. It's a label used for identifying the workflow in the repository. |
|
||||
| `run-name:` | Sets a custom name for the run, using a GitHub context `${{ github.actor }}` to include the name of the user who initiated the run. |
|
||||
| `on:` | Specifies the event that triggers the workflow. In this case, the workflow is triggered on any `push` event to the repository. |
|
||||
| `jobs:` | Defines a group of jobs that will be executed as part of the workflow. Each job runs independently in the workflow. |
|
||||
| `check-bats-version:` | Identifier for a specific job within the workflow. This job is named `check-bats-version`. |
|
||||
| `runs-on:` | Specifies the type of machine to run the job on. Here, it's set to run on the latest version of Ubuntu. |
|
||||
| `steps:` | Contains a sequence of tasks (steps) that will be executed as part of the job. |
|
||||
| `uses:` | Used to specify an action to include as part of a step. For example, `actions/checkout@v4` checks out the repository, and `actions/setup-node@v3` sets up a Node environment. |
|
||||
| `with:` | Specifies additional parameters for the action. It's used in conjunction with `uses` to configure the action. |
|
||||
| `node-version:` | Contains parameter under `with`, specifying the version of Node.js to be set up by the `setup-node` action. In this case, it's set to version '14'. |
|
||||
|
||||
### Events
|
||||
|
||||
@@ -85,49 +92,49 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run a script
|
||||
run: echo "This workflow runs on every push to the repository."
|
||||
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run a script
|
||||
run: echo "This workflow runs on every push to the repository."
|
||||
```
|
||||
|
||||
**Event Triggers**
|
||||
|
||||
| Event Name | Description |
|
||||
| :--- | :--- |
|
||||
| `push` | Triggers on a push to the repository. |
|
||||
| `pull_request` | Triggers on pull request events. |
|
||||
| `pull_request_review` | Triggers on pull request review events. |
|
||||
| `pull_request_review_comment` | Triggers on comments on pull request reviews. |
|
||||
| `pull_request_target` | For workflows in forked repositories. |
|
||||
| `fork` | Triggers when a repository is forked. |
|
||||
| `issue_comment` | Triggers on issue and PR comments. |
|
||||
| `issues` | Triggers on issue events. |
|
||||
| `label` | Triggers on label events. |
|
||||
| `milestone` | Triggers on milestone events. |
|
||||
| `deployment` | Triggers on deployment. |
|
||||
| `deployment_status` | Triggers on deployment status updates. |
|
||||
| `public` | Triggers when repo goes from private to public.|
|
||||
| `repository_dispatch` | Triggers on a custom repository event. |
|
||||
| `schedule` | Triggers on a defined schedule. |
|
||||
| `workflow_dispatch` | Allows manual triggering of the workflow. |
|
||||
| `workflow_run` | Triggers on the completion of another workflow.|
|
||||
| `create` | Triggers when a branch or tag is created. |
|
||||
| `delete` | Triggers when a branch or tag is deleted. |
|
||||
| `page_build` | Triggers on GitHub Pages build events. |
|
||||
| `release` | Triggers on release events. |
|
||||
| `watch` | Triggers when someone stars the repo. |
|
||||
| `registry_package` | Triggers on registry package events. |
|
||||
| `status` | Triggers on status updates to a Git commit. |
|
||||
| `project` | Triggers on project board events. |
|
||||
| `project_card` | Triggers on project card events. |
|
||||
| `project_column` | Triggers on project column events. |
|
||||
| `member` | Triggers on collaborator events. |
|
||||
| `gollum` | Triggers on wiki page updates. |
|
||||
| Event Name | Description |
|
||||
| :---------------------------- | :---------------------------------------------- |
|
||||
| `push` | Triggers on a push to the repository. |
|
||||
| `pull_request` | Triggers on pull request events. |
|
||||
| `pull_request_review` | Triggers on pull request review events. |
|
||||
| `pull_request_review_comment` | Triggers on comments on pull request reviews. |
|
||||
| `pull_request_target` | For workflows in forked repositories. |
|
||||
| `fork` | Triggers when a repository is forked. |
|
||||
| `issue_comment` | Triggers on issue and PR comments. |
|
||||
| `issues` | Triggers on issue events. |
|
||||
| `label` | Triggers on label events. |
|
||||
| `milestone` | Triggers on milestone events. |
|
||||
| `deployment` | Triggers on deployment. |
|
||||
| `deployment_status` | Triggers on deployment status updates. |
|
||||
| `public` | Triggers when repo goes from private to public. |
|
||||
| `repository_dispatch` | Triggers on a custom repository event. |
|
||||
| `schedule` | Triggers on a defined schedule. |
|
||||
| `workflow_dispatch` | Allows manual triggering of the workflow. |
|
||||
| `workflow_run` | Triggers on the completion of another workflow. |
|
||||
| `create` | Triggers when a branch or tag is created. |
|
||||
| `delete` | Triggers when a branch or tag is deleted. |
|
||||
| `page_build` | Triggers on GitHub Pages build events. |
|
||||
| `release` | Triggers on release events. |
|
||||
| `watch` | Triggers when someone stars the repo. |
|
||||
| `registry_package` | Triggers on registry package events. |
|
||||
| `status` | Triggers on status updates to a Git commit. |
|
||||
| `project` | Triggers on project board events. |
|
||||
| `project_card` | Triggers on project card events. |
|
||||
| `project_column` | Triggers on project column events. |
|
||||
| `member` | Triggers on collaborator events. |
|
||||
| `gollum` | Triggers on wiki page updates. |
|
||||
|
||||
### Jobs
|
||||
|
||||
Single Job:
|
||||
|
||||
```yaml
|
||||
name: Single Job
|
||||
on: [push]
|
||||
@@ -135,12 +142,13 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run a build script
|
||||
run: script/build
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run a build script
|
||||
run: script/build
|
||||
```
|
||||
|
||||
Multiple Jobs:
|
||||
|
||||
```yaml
|
||||
name: CI Workflow
|
||||
|
||||
@@ -150,49 +158,49 @@ jobs:
|
||||
job-1:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Runs job 1
|
||||
run: echo "Running Job 1"
|
||||
- uses: actions/checkout@v2
|
||||
- name: Runs job 1
|
||||
run: echo "Running Job 1"
|
||||
|
||||
job-2:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Runs job 2
|
||||
run: echo "Running Job 2"
|
||||
- uses: actions/checkout@v2
|
||||
- name: Runs job 2
|
||||
run: echo "Running Job 2"
|
||||
|
||||
job-3:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Runs job 3
|
||||
run: echo "Running Job 3"
|
||||
- uses: actions/checkout@v2
|
||||
- name: Runs job 3
|
||||
run: echo "Running Job 3"
|
||||
```
|
||||
|
||||
### Steps
|
||||
### Steps
|
||||
|
||||
```yaml
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
# step 1
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v2
|
||||
|
||||
# step 2
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: '14'
|
||||
|
||||
# step 3
|
||||
- name: Install dependencies
|
||||
run: npm install
|
||||
# step 1
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@v2
|
||||
|
||||
# step 4
|
||||
- name: Run tests
|
||||
run: npm test
|
||||
# step 2
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: "14"
|
||||
|
||||
# step 3
|
||||
- name: Install dependencies
|
||||
run: npm install
|
||||
|
||||
# step 4
|
||||
- name: Run tests
|
||||
run: npm test
|
||||
```
|
||||
|
||||
### Github Runners & Self Hosted Runners
|
||||
@@ -204,11 +212,11 @@ name: Workflow
|
||||
on: [push]
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest # default runner
|
||||
runs-on: ubuntu-latest # default runner
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run a script
|
||||
run: echo "Hello, world!"
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run a script
|
||||
run: echo "Hello, world!"
|
||||
```
|
||||
|
||||
[Self Hosted Runner](https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/about-self-hosted-runners)
|
||||
@@ -220,12 +228,12 @@ jobs:
|
||||
build:
|
||||
runs-on: self-hosted
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run a script
|
||||
run: echo "Hello from self-hosted runner!"
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run a script
|
||||
run: echo "Hello from self-hosted runner!"
|
||||
```
|
||||
|
||||
### Environment Variables
|
||||
### Environment Variables
|
||||
|
||||
Custom variable defined using environments.
|
||||
|
||||
@@ -240,8 +248,10 @@ jobs:
|
||||
run: echo "Value of CUSTOM_VAR is $CUSTOM_VAR"
|
||||
```
|
||||
|
||||
### Secrets
|
||||
To add a new secret in your github repository nagivate to `Repository` > `Settings` > `Security` > `Secrets and Variables` > `Actions` > `New Repository Secret`
|
||||
### Secrets
|
||||
|
||||
To add a new secret in your github repository nagivate to `Repository` > `Settings` > `Security` >
|
||||
`Secrets and Variables` > `Actions` > `New Repository Secret`
|
||||
|
||||
Example Secrets workflow:
|
||||
|
||||
@@ -254,13 +264,13 @@ jobs:
|
||||
example_job:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
- name: Use a secret
|
||||
run: echo "The secret is ${{ secrets.MY_SECRET }}"
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
- name: Use a secret
|
||||
run: echo "The secret is ${{ secrets.MY_SECRET }}"
|
||||
```
|
||||
|
||||
### Artifacts
|
||||
### Artifacts
|
||||
|
||||
To access your artifact navigate to `Repository` > `Actions` > `Workflow Run` > `Artifacts`
|
||||
|
||||
@@ -269,16 +279,16 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Build project
|
||||
run: make build
|
||||
- name: Upload build artifact
|
||||
uses: actions/upload-artifact@v3 # upload Artifacts prebuilt action
|
||||
with:
|
||||
name: my-artifact
|
||||
path: path/to/artifact
|
||||
- name: Build project
|
||||
run: make build
|
||||
- name: Upload build artifact
|
||||
uses: actions/upload-artifact@v3 # upload Artifacts prebuilt action
|
||||
with:
|
||||
name: my-artifact
|
||||
path: path/to/artifact
|
||||
```
|
||||
|
||||
### Caching Dependencies
|
||||
### Caching Dependencies
|
||||
|
||||
Dependency caches stores downloaded packages or compiled binaries of your workflows.
|
||||
|
||||
@@ -287,19 +297,19 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v2 # stores downloaded packages or compiled binaries
|
||||
with:
|
||||
path: |
|
||||
path/to/dependencies
|
||||
another/path
|
||||
key: ${{ runner.os }}-deps-${{ hashFiles('**/lockfile') }} # hash of the dependency lock file is generated in the OS
|
||||
- name: Install dependencies
|
||||
run: install-command
|
||||
- uses: actions/checkout@v2
|
||||
- name: Cache dependencies
|
||||
uses: actions/cache@v2 # stores downloaded packages or compiled binaries
|
||||
with:
|
||||
path: |
|
||||
path/to/dependencies
|
||||
another/path
|
||||
key: ${{ runner.os }}-deps-${{ hashFiles('**/lockfile') }} # hash of the dependency lock file is generated in the OS
|
||||
- name: Install dependencies
|
||||
run: install-command
|
||||
```
|
||||
|
||||
### Matrix Strategies
|
||||
### Matrix Strategies
|
||||
|
||||
```yaml
|
||||
jobs:
|
||||
@@ -309,20 +319,20 @@ jobs:
|
||||
matrix:
|
||||
node-version: [12.x, 14.x, 16.x]
|
||||
# matrix strategy runs enables you to run jobs across multiple combinations of environments and OS's
|
||||
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm install
|
||||
- run: npm test
|
||||
env:
|
||||
CI: true
|
||||
- uses: actions/checkout@v2
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v1
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm install
|
||||
- run: npm test
|
||||
env:
|
||||
CI: true
|
||||
```
|
||||
|
||||
### Conditions and Expressions
|
||||
### Conditions and Expressions
|
||||
|
||||
Branch Conditions:
|
||||
|
||||
@@ -331,11 +341,11 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Run build
|
||||
if: github.ref == 'refs/heads/main' # "Run build" step will only execute if the current branch is main.
|
||||
run: make build
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Run build
|
||||
if: github.ref == 'refs/heads/main' # "Run build" step will only execute if the current branch is main.
|
||||
run: make build
|
||||
```
|
||||
|
||||
Event Trigger Conditions:
|
||||
@@ -345,17 +355,16 @@ jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Run tests
|
||||
if: github.event_name == 'pull_request' # "Run tests" step is executed only when the workflow is triggered by a pull request event
|
||||
run: npm test
|
||||
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Run tests
|
||||
if: github.event_name == 'pull_request' # "Run tests" step is executed only when the workflow is triggered by a pull request event
|
||||
run: npm test
|
||||
```
|
||||
|
||||
### Workflow Commands
|
||||
### Workflow Commands
|
||||
|
||||
Depending on your OS, if you are running ubuntu-latest, bash commands should work
|
||||
Depending on your OS, if you are running ubuntu-latest, bash commands should work
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
@@ -365,22 +374,23 @@ steps:
|
||||
|
||||
### Concurrency
|
||||
|
||||
The concurrency field creates a group based on the `github.head_ref`. If a new run starts within the same concurrency group, it cancels any in-progress runs.
|
||||
The concurrency field creates a group based on the `github.head_ref`. If a new run starts within the same concurrency
|
||||
group, it cancels any in-progress runs.
|
||||
|
||||
```yaml
|
||||
jobs:
|
||||
my_job:
|
||||
runs-on: ubuntu-latest
|
||||
concurrency:
|
||||
concurrency:
|
||||
group: ${{ github.head_ref }}
|
||||
cancel-in-progress: true
|
||||
steps:
|
||||
- name: Run a script
|
||||
run: echo "Running script..."
|
||||
- name: Run a script
|
||||
run: echo "Running script..."
|
||||
```
|
||||
|
||||
Also see
|
||||
------------
|
||||
## Also see
|
||||
|
||||
- [YAML](/yaml) _(cheatsheets.zip)_
|
||||
- [Github](/github) _(cheatsheets.zip)_
|
||||
- [Github Actions Documentation](https://docs.github.com/en/actions)_(docs.github.com/en/actions)_
|
||||
- [Github Actions Documentation](https://docs.github.com/en/actions)_(docs.github.com/en/actions)_
|
||||
Reference in new issue
Block a user