Skip to content

Commit ae1298e

Browse files
committed
feat: Add pages from code-cookbook
1 parent e84055d commit ae1298e

File tree

8 files changed

+624
-0
lines changed

8 files changed

+624
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definitions
2+
> Copied from Github UI or docs
3+
4+
## Personal access tokens
5+
6+
aka "PAT"
7+
8+
> Tokens you have generated that can be used to access the GitHub API.
9+
>
10+
> Make sure to copy your new personal access token now. You won’t be able to see it again!
11+
12+
If you have one token across repos, then if need to regenerate, then you'd have to go and update everywhere.
13+
14+
So you might want to create a PAT for each application as a one to one mapping.
15+
16+
17+
## Secrets
18+
19+
> Secrets are environment variables that are **encrypted** and only exposed to selected actions. Anyone with **collaborator** access to this repository can use these secrets in a workflow.
20+
>
21+
> Secrets are not passed to workflows that are triggered by a pull request from a fork
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
layout: listing
3+
logo: githubactions
4+
---
5+
# GitHub Actions
6+
> Guide to syntax for GitHub Actions workflows
7+
8+
9+
## Intro
10+
11+
12+
## About
13+
14+
This section covers how to use GitHub Actions in a CI/CD flow such as you run automated tests, deploy and publish your repo. All running for free in the cloud whenever the workflow is triggered such as with a push or merged Pull Request.
15+
16+
To use GH Actions, you must create a workflow YAML file with appropriate fields set including triggers and steps. You can write shell commands as _steps_ and you can use use actions from the GH Marketplace to help you setup your environment (install a language and packages) and perform test and deploy steps, without you having to write a lot of code.
17+
18+
19+
## Getting started
20+
21+
Here are some other areas to start with:
22+
23+
- Workflow syntax and samples / use-cases around them
24+
- [Jobs](jobs.md)
25+
- [Triggers](triggers.md)
26+
- [Resources](resources.md) for external links such as to docs, to help you get into how Actions work and the syntax
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Jobs cheatsheet
2+
> Syntax for jobs in GH actions workflow file
3+
4+
{% raw %}
5+
6+
See [Workflow syntax for Github Actions](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions) in Github docs.
7+
8+
> A workflow run is made up of one or more jobs. Jobs run in **parallel** by default.
9+
>
10+
> To run jobs sequentially, you can define dependencies on other jobs using the `jobs.<job_id>.needs` keyword.
11+
>
12+
> Each job runs in an environment specified by runs-on.
13+
14+
15+
## Job IDs
16+
17+
Each job must have a unique ID as below. This must start with a letter or `_` and contain only alphanumeric characters, `-`, or `_`.
18+
19+
```yaml
20+
jobs:
21+
my_first_job:
22+
name: My first job
23+
24+
my_second_job:
25+
name: My second job
26+
```
27+
28+
Using `runs-on`.
29+
30+
```yaml
31+
jobs:
32+
my_first_job:
33+
runs-on: ubuntu-latest
34+
name: My first job
35+
36+
my_second_job:
37+
runs-on: ubuntu-latest
38+
name: My second job
39+
```
40+
41+
42+
## Needs
43+
44+
By default jobs run in parallel.
45+
46+
Here we setup a sequence of jobs that only run if the previous one passed.
47+
48+
[Job Needs](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idneeds) docs.
49+
50+
> Identifies any jobs that must complete successfully before this job will run. It can be a string or array of strings. If a job fails, all jobs that need it are skipped unless the jobs use a conditional statement that causes the job to continue.
51+
52+
- `main.yml`
53+
```yaml
54+
jobs:
55+
job1:
56+
57+
job2:
58+
needs: job1
59+
60+
job3:
61+
needs: [job1, job2]
62+
```
63+
64+
See [job output](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjobs_idoutputs) in the docs for how to persist or [upload and download artifications](https://docs.github.com/en/actions/guides/storing-workflow-data-as-artifacts#passing-data-between-jobs-in-a-workflow). If you don't do this, then a `build` directory from one job can't be used by the other job.
65+
66+
Using job output in a dependent job:
67+
68+
- `main.yml`
69+
```yaml
70+
jobs:
71+
job1:
72+
runs-on: ubuntu-latest
73+
74+
# Map a step output to a job output
75+
outputs:
76+
output1: ${{ steps.step1.outputs.test }}
77+
output2: ${{ steps.step2.outputs.test }}
78+
79+
steps:
80+
- id: step1
81+
run: echo "::set-output name=test::hello"
82+
- id: step2
83+
run: echo "::set-output name=test::world"
84+
85+
job2:
86+
runs-on: ubuntu-latest
87+
88+
needs: job1
89+
90+
steps:
91+
- run: echo ${{needs.job1.outputs.output1}} ${{needs.job1.outputs.output2}}
92+
```
93+
94+
95+
## If statement
96+
97+
This step only runs when the event type is a pull_request and the event action is unassigned.
98+
99+
- `main.yml`
100+
```yaml
101+
steps:
102+
- name: My first step
103+
if: ${{ github.event_name == 'pull_request' && github.event.action == 'unassigned' }}
104+
run: echo This event is a pull request that had an assignee removed.
105+
```
106+
- `main.yml`
107+
```yaml
108+
steps:
109+
- name: Print a greeting
110+
env:
111+
MY_VAR: Hi there! My name is
112+
FIRST_NAME: Mona
113+
MIDDLE_NAME: The
114+
LAST_NAME: Octocat
115+
run: |
116+
echo $MY_VAR $FIRST_NAME $MIDDLE_NAME $LAST_NAME.
117+
```
118+
119+
This can be useful if you want just one workflow file and one job but want to skip a deploy steps at the end.
120+
121+
Read more
122+
123+
- [Context and Expression Syntax](https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions)
124+
- [If steps](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idsteps).
125+
126+
127+
## Related
128+
129+
### Env
130+
131+
```yaml
132+
env:
133+
SERVER: production
134+
```
135+
136+
### Defaults
137+
138+
```yaml
139+
defaults:
140+
foo: bar
141+
```
142+
143+
```yaml
144+
defaults:
145+
run:
146+
shell: bash
147+
working-directory: scripts
148+
```
149+
150+
{% endraw %}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Resources
2+
> External links around GH Actions, from basic to advanced usage
3+
4+
If you are new to GitHub Actions, I recommend going over my blog posts on dev.to - [Intro to GitHub Actions series](https://dev.to/michaelcurrin/series/9032)
5+
6+
7+
## Overview
8+
9+
- [GitHub Actions Marketplace](https://github.com/marketplace)
10+
- [GitHub Actions](https://github.com/actions) org on Github
11+
- [GitHub Actions](https://github.community/c/github-actions) community forums
12+
13+
14+
## Documentation
15+
16+
- [GitHub Actions Documentation](https://docs.github.com/en/actions)
17+
- [Configuring and managing workflows](https://docs.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow)
18+
- A good step-by-step tutorial for setting up GH Actions
19+
- [Environment variables](https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables)
20+
- _Getting Started_ section
21+
- [Core concepts](https://docs.github.com/en/actions/getting-started-with-github-actions/core-concepts-for-github-actions)
22+
- _Reference_ section
23+
- [Workflow syntax](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions)
24+
- [Events that trigger workflows](https://docs.github.com/en/actions/reference/events-that-trigger-workflows)
25+
- [Context and expression syntax](https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions)
26+
- [Workflow commands](https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions)
27+
- e.g. Setting color to green or showing a warning message.
28+
```yaml
29+
- name: Set selected color
30+
run: echo '::set-env name=SELECTED_COLOR::green'
31+
```
32+
- This can be useful in workflows and when writing commands in the shell file for an action.
33+
34+
35+
## Limitations
36+
37+
- [Intro to GitHub Actions blog post](https://gabrieltanner.org/blog/an-introduction-to-github-actions)
38+
> Actions are completely free for every open-source repository and include **2000 free build minutes per month** for all your private repositories which is comparable with most CI/CD free plans. If that is not enough for your needs you can pick another plan or go the self-hosted route.
39+
- [Usage limits](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#usage-limits)
40+
> On the free tier you can have _20 concurrent jobs_. I guess 20 on one repo or 20 repos with one each.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Skip
2+
> How to skip some or all of the build steps
3+
4+
{% raw %}
5+
6+
## Ignore paths
7+
8+
Don't run the workflow if changes were only made to certain paths, such as the `docs` directory.
9+
10+
Remember to use quotes so stop YAML evaluation of the `**` glob.
11+
12+
### Paths ignore attribute
13+
14+
Use `paths-ignore` attribute.
15+
16+
From [example ignoring paths](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#example-ignoring-paths) in the docs.
17+
18+
```yaml
19+
on:
20+
push:
21+
paths-ignore:
22+
- "docs/**"
23+
```
24+
25+
If your docs directory has a lot of files in it that you edit often, then it is worth skipping that at least.
26+
27+
You don't have to be thorough. Like I wouldn't put your `LICENSE` file in there as that hardly changes.
28+
29+
### Paths attribute
30+
31+
This will only build `.js` files and ignore others.
32+
33+
```yaml
34+
on:
35+
push:
36+
paths:
37+
- "**.js"
38+
```
39+
40+
This is dangerous because you might leave your workflow file, `Makefile`, package files, etc. So can avoid being too restrictive accidentally by using the exclusion in the previous section instead.
41+
42+
### Positive and negative paths
43+
44+
Or using a `!` symbol in the `paths` attribute. The order matters. Here we include a directory and then exclude a subdirectory.
45+
46+
```yaml
47+
on:
48+
push:
49+
paths:
50+
- "sub-project/**"
51+
- "!sub-project/docs/**"
52+
```
53+
54+
55+
## Skip step
56+
57+
Add a condition to skip if the commit message containts the phrase `[ci skip]`.
58+
59+
```yaml
60+
jobs:
61+
deploy_docs:
62+
if: "!contains(github.event.commits[0].message, '[ci skip]')"
63+
64+
steps:
65+
# ...
66+
```
67+
68+
{% endraw %}

0 commit comments

Comments
 (0)