GitHub Actions to automate your build#
Deploying static frontend web apps (such as Vanilla, Angular, React, Vue, etc) in GitHub?
It's possible to just add, commit and push your code and automate things like npm run build
to setup Github Pages for your repository.
Introduction#
If you were manually deploying your application before using things like npm run build
and additional setups like creating a docs/ folder, then you came to the right place.
Getting Started#
Configure repository#
You must already have a GitHub repository before the following steps:
- Go to Settings tab
- Go to Pages under Code and Automation section
- Select
GitHub Actionsthe Source dropdown under Build and Deployment section
Add GitHub workflow#
Create ci.yml file in .github/workflows folder.
.
├─ .github\workflows
│ └─ ci.yml (1)
├─ ...
└─ README.md
- You can rename the file to any name you want.
(e.g.deploy.yml)
General Build Template#
When you have to generate a /dist folder, then you need this content (1)
| ci.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
Test Template#
When you have unit tests to run in background (i.e. Vitest, Jest) (1)
-
See example:
https://github.com/lightzane/try-vitest/blob/main/.github/workflows/test.ymlSee example run:
https://github.com/lightzane/try-vitest/actions/runs/8459849190/job/23176953453#step:5:1
| test.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
-
When using Jest, update accordingly:
npx jestOr when you have custom scripts in your
package.jsonnpm run test:cov
MkDocs Material Template#
GitHub Pages settings NOT required
When deploying MkDocs Material, there is no need to setup anything in repository settings.
Just create the ci.yml in your .github/workflows with the following content:
| ci.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
GitHub Pages
Remember when you were manually building your apps and saving it to /docs folder?
It is actually using the same GitHub Actions behind the scenes when you selected
the Deploy from a branch under Source dropdown.
Auto Release Tag Template#
You can do more than just deployment!
Such as automatically creating git tag or Releaes Tags for your GitHub repository.
See example:
https://github.com/lightzane/test-labs/blob/main/.github/workflows/release-tag.yml
| release-tag.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
Regarding the highlighted Line 34:
You can remove the -f flag if you want the pipeline to fail and so that you could be remind to update your version.
Or you can also handle this locally before even pushing your code.
💡 See this blog to see how you can prevent committing and pushing your code with the same version.
Conclusion#
There you have it! 🔥 You have used GitHub Actions to create workflows to automate the build and deployment of your application.
You just went through the basics of CI/CD, which stands for Continuous Integration and Continuous Deployment.
GitHub Actions is a feature in GitHub where it helps you automate your software workflows with CI/CD.
Learn more: https://github.com/features/actions