wiki/cloud.github-actions.html

149 lines
33 KiB
HTML
Raw Permalink Normal View History

2022-07-29 15:41:17 +00:00
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width"/><meta charSet="utf-8"/><title>GitHub Actions</title><script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script id="MathJax-script" async="" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script><script type="text/x-mathjax-config">
MathJax = {
tex: {
inlineMath: [ [&#x27;$&#x27;,&#x27;$&#x27;], [&#x27;\(&#x27;,&#x27;\)&#x27;] ],
displayMath: [ [&#x27;$$&#x27;,&#x27;$$&#x27;], [&#x27;[&#x27;,&#x27;]&#x27;] ]
},
options = {
processHtmlClass = &quot;math&quot;
}
}
</script><meta name="next-head-count" content="6"/><link rel="preload" href="/wiki/_next/static/css/52fc2ba29703df73922c.css" as="style"/><link rel="stylesheet" href="/wiki/_next/static/css/52fc2ba29703df73922c.css" data-n-g=""/><noscript data-n-css=""></noscript><link rel="preload" href="/wiki/_next/static/chunks/main-ae4733327bd95c4ac325.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/webpack-50bee04d1dc61f8adf5b.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/framework.9d524150d48315f49e80.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/commons.0e1c3f9aa780c2dfe9f0.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/pages/_app-8e3d0c58a60ec788aa69.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/940643274e605e7596ecea1f2ff8d83317a3fb76.4841a16762f602a59f00.js" as="script"/><link rel="preload" href="/wiki/_next/static/chunks/pages/%5B%5B...slug%5D%5D-1aa198f87ede1cd0e1dc.js" as="script"/></head><body><div id="__next"><main><h1>GitHub Actions</h1><section class="post-metadata"><span>Date: <!-- -->2021-06-20 18:58:48 +08:00</span><span>Date modified: <!-- -->2022-06-19 11:46:41 +08:00</span></section><nav class="toc"><ol class="toc-level toc-level-1"><li class="toc-item toc-item-h1"><a href="/wiki/cloud.github-actions#ecosystem" class="toc-link toc-link-h1">Ecosystem</a></li><li class="toc-item toc-item-h1"><a href="/wiki/cloud.github-actions#actions" class="toc-link toc-link-h1">Actions</a></li><li class="toc-item toc-item-h1"><a href="/wiki/cloud.github-actions#examples" class="toc-link toc-link-h1">Examples</a><ol class="toc-level toc-level-2"><li class="toc-item toc-item-h2"><a href="/wiki/cloud.github-actions#python-version-of-an-installation" class="toc-link toc-link-h2">Python version of an installation</a></li><li class="toc-item toc-item-h2"><a href="/wiki/cloud.github-actions#docker-container-integration" class="toc-link toc-link-h2">Docker container integration</a></li><li class="toc-item toc-item-h2"><a href="/wiki/cloud.github-actions#building-a-nix-binary-cache" class="toc-link toc-link-h2">Building a Nix binary cache</a></li><li class="toc-item toc-item-h2"><a href="/wiki/cloud.github-actions#building-packages-in-multiple-architectures" class="toc-link toc-link-h2">Building packages in multiple architectures</a></li></ol></li></ol></nav><p><img src="/wiki/assets/fds-visual-github-actions-description.png" placeholder="blur"/></p><ul><li><p>a CI/CD tool integrated into GitHub
</p></li><li><p>it is free for public repos but limited time per month for private repos
</p></li><li><p><a href="https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions">documentation</a> for it
</p></li><li><p>focused into creating workflows which can be separate and applied in different contexts
</p><ul><li><p>a workflow is activated from an event
</p></li><li><p>stored in <code class="inline-code">.github/workflows</code> in the remote repo
</p></li></ul></li><li><p>each workflow can run a job which are composed of steps
</p></li><li><p>each step make uses an action which is basically a script;
it can interact with the repo or do something else entirely without ever touching it
</p></li><li><p>you can use already defined actions or with your own
</p></li><li><p>if you want to explore other options, the <a href="https://github.com/marketplace">GitHub marketplace</a> allows searching for various third-party actions
</p></li></ul><h1 id="ecosystem">Ecosystem</h1><ul><li><p>there is also a <a href="https://docs.github.com/en/actions/guides">detailed guide</a> in introducing the overall concept
</p></li><li><p>the workflow allows you to create build artifacts;
you have fine-grained control such as letting you <a href="https://github.com/marketplace/actions/upload-a-build-artifact">upload certain files to be artifacts</a> and <a href="https://github.com/marketplace/actions/download-a-build-artifact">downloading them</a> after a workflow run;
it also enables sharing of data between jobs
</p></li><li><p>it can make automated releases like what I have done with <a href="https://github.com/foo-dogsquared/pop-launcher-plugin-duckduckgo-bangs/blob/d878e991dbb3269b4ea520e8c41bfa3e6346e4ab/.github/workflows/release.yml">one of my projects</a></p></li><li><p>it can create automated Git commits with <a href="https://github.com/marketplace/actions/git-auto-commit">Git Auto Commit</a>;
combine with the ability to set schedules, you can create an automatic sync following a project
</p></li><li><p>otherwise, you can also create automated release but have to go through a merge request;
for example, this is how <a href="https://github.com/simple-icons/simple-icons/blob/9020eb4a8163817813f90f493c66e8d6b565d31c/.github/workflows/create-release.yml">Simple Icons maintain their releases</a></p></li></ul><h1 id="actions">Actions</h1><ul><li><p>each of the job is mostly built with the actions;
the job can be controlled further through the workflow file;
see the <a href="https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions">Workflow Syntax</a> for more information
</p></li><li><p>related information such as the current branch, runners specifics, and so forth are stored in <a href="https://docs.github.com/en/actions/learn-github-actions/contexts">Contexts</a></p></li><li><p>like previously mentioned, you can search these actions in GitHub&#x27;s marketplace
</p></li><li><p>the actions are primarily defined with <code class="inline-verbatim">actions.yml</code> from the root which can serve as a documentation for the parameters
</p></li></ul><h1 id="examples">Examples</h1><p>With GitHub Actions being a massive ecosystem of integrations as of 2021-07-05, we have to find some examples in the worldwide community repos from there.
</p><h2 id="python-version-of-an-installation">Python version of an installation</h2><p>The following block is a minimal example checking the Python version in the installation.
</p><pre class="src-block"><code class="language-yaml">name: Python version
on: [push]
jobs:
check-python-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: python --version
</code></pre><h2 id="docker-container-integration">Docker container integration</h2><p>GitHub workflows can make use of containers for easier delivering of dependencies and reproducing the development environment (among other things).
This includes...
</p><ul><li><p><a href="https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry">GitHub&#x27;s own container registry</a>,
</p></li><li><p><a href="https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idcontainer">using a container to be used in a workflow</a> which is nice for building for multiple operating systems (e.g., multiple Linux distros)
</p></li><li><p><a href="https://docs.github.com/en/actions/using-containerized-services/about-service-containers">service containers</a> to make use of tools to your workflow,
</p></li><li><p>and even <a href="https://docs.github.com/en/actions/publishing-packages/publishing-docker-images">publishing containers to a registry from a workflow</a>.
</p></li></ul><p>In this example, we&#x27;ll push an image to docker.io registry.
Be sure to have the necessary credentials and set it to the workflow environment to successfully run this workflow.
</p><pre class="src-block"><code class="language-yaml">name: Docker build image
on: [push]
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/python-helloworld:latest
platforms: linux/amd64,linux/arm64
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
</code></pre><h2 id="building-a-nix-binary-cache">Building a Nix binary cache</h2><p>We&#x27;ll use <a href="/wiki/cloud.cachix">Cachix</a> as our binary cache service which has a free 10GB space (as of 2022-06-19).
This makes it easier to setup and distribute your own project built with <a href="/wiki/tools.nix">Nix package manager</a>.
</p><pre class="src-block"><code class="language-yaml">name: &quot;Push packages into Cachix cache&quot;
on:
pull_request:
push:
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.3.4
- uses: cachix/install-nix-action@v13
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: cachix/cachix-action@v10
with:
name: mycache
signingKey: &#x27;${{ secrets.CACHIX_SIGNING_KEY }}&#x27;
- run: nix-build
- run: nix-shell --run &quot;echo OK&quot;
</code></pre><h2 id="building-packages-in-multiple-architectures">Building packages in multiple architectures</h2><p>This makes use of job matrix allowing to easily create similar workflows with different configurations.
</p><p>We&#x27;ll use the GitHub Actions workflow file from the NUR template.
It is somewhat complex and it is doing a fine job showcasing some of GitHub Actions features.
</p><pre class="src-block"><code class="language-yaml">name: &quot;Build and populate cache&quot;
on:
pull_request:
push:
schedule:
- cron: &#x27;27 4 * * *&#x27;
jobs:
tests:
strategy:
matrix:
nurRepo:
- &#x27;&lt;YOUR_NUR_REPO&gt;&#x27;
cachixName:
- &#x27;&lt;YOUR_CACHIX_NAME&gt;&#x27;
nixPath:
- nixpkgs=channel:nixos-unstable
- nixpkgs=channel:nixpkgs-unstable
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2.3.4
- name: Install nix
uses: cachix/install-nix-action@v13
with:
nix_path: &quot;${{ matrix.nixPath }}&quot;
- name: Show nixpkgs version
run: nix-instantiate --eval -E &#x27;(import &lt;nixpkgs&gt; {}).lib.version&#x27;
- name: Setup cachix
uses: cachix/cachix-action@v10
if: ${{ matrix.cachixName != &#x27;&lt;YOUR_CACHIX_NAME&gt;&#x27; }}
with:
name: ${{ matrix.cachixName }}
signingKey: &#x27;${{ secrets.CACHIX_SIGNING_KEY }}&#x27;
- name: Check evaluation
run: |
nix-env -f . -qa \* --meta --xml \
--allowed-uris https://static.rust-lang.org \
--option restrict-eval true \
--option allow-import-from-derivation true \
--drv-path --show-trace \
-I nixpkgs=$(nix-instantiate --find-file nixpkgs) \
-I $PWD
- name: Build nix packages
run: nix run -I &#x27;nixpkgs=channel:nixos-unstable&#x27; nixpkgs.nix-build-uncached -c nix-build-uncached ci.nix -A cacheOutputs
- name: Trigger NUR update
if: ${{ matrix.nurRepo != &lt;YOUR_NUR_REPO&gt;&#x27; }}
run: curl -XPOST &quot;https://nur-update.herokuapp.com/update?repo=${{ matrix.nurRepo }}&quot;
</code></pre></main></div><script id="__NEXT_DATA__" type="application/json">{"props":{"pageProps":{"metadata":{"date":"\"2021-06-20 18:58:48 +08:00\"","date_modified":"\"2022-06-19 11:46:41 +08:00\"","language":"en","source":""},"title":"GitHub Actions","hast":{"type":"root","children":[{"type":"element","tagName":"nav","properties":{"className":"toc"},"children":[{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-1"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"ecosystem"},"children":[{"type":"text","value":"Ecosystem"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/cloud.github-actions#ecosystem"},"children":[{"type":"text","value":"Ecosystem"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"actions"},"children":[{"type":"text","value":"Actions"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/cloud.github-actions#actions"},"children":[{"type":"text","value":"Actions"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h1","properties":{"id":"examples"},"children":[{"type":"text","value":"Examples"}]}]},"properties":{"className":"toc-item toc-item-h1"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h1","href":"/cloud.github-actions#examples"},"children":[{"type":"text","value":"Examples"}]},{"type":"element","tagName":"ol","properties":{"className":"toc-level toc-level-2"},"children":[{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"python-version-of-an-installation"},"children":[{"type":"text","value":"Python version of an installation"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cloud.github-actions#python-version-of-an-installation"},"children":[{"type":"text","value":"Python version of an installation"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"docker-container-integration"},"children":[{"type":"text","value":"Docker container integration"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cloud.github-actions#docker-container-integration"},"children":[{"type":"text","value":"Docker container integration"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"building-a-nix-binary-cache"},"children":[{"type":"text","value":"Building a Nix binary cache"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cloud.github-actions#building-a-nix-binary-cache"},"children":[{"type":"text","value":"Building a Nix binary cache"}]}]},{"type":"element","tagName":"li","data":{"hookArgs":[{"type":"element","tagName":"h2","properties":{"id":"building-packages-in-multiple-architectures"},"children":[{"type":"text","value":"Building packages in multiple architectures"}]}]},"properties":{"className":"toc-item toc-item-h2"},"children":[{"type":"element","tagName":"a","properties":{"className":"toc-link toc-link-h2","href":"/cloud.github-actions#building-packages-in-multiple-architectures"},"children":[{"type":"text","value":"Building packages in multiple architectures"}]}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"img","properties":{"src":"/assets/fds-visual-github-actions-description.png"},"children":[]}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"element","tagName":"li","properties":{},"children":[{"type