Share E-Book

GitHub Actions in Action (MEAP - Manning Early Access Program) (Michael Kaufmann, Rob Bos, Marcel de Vries)(Z-Library)

Author

,
,

Rating No ratings yet

Log in to rate

OS
Language English

No Description

Format PDF
Size 7.2 MB
9
Views
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
(This page has no text content)
Page 2
(This page has no text content)
Page 3
GitHub Actions in Action 1. welcome 2. 1_Introduction_to_GitHub_Actions 3. 2_Hands-on:_My_first_Actions_Workflow 4. 3_Workflows 5. 4_GitHub_Actions 6. 5_Runners 7. 6_Self-hosted_runners
Page 4
welcome Thank you for purchasing the MEAP for GitHub Actions in Action. We hope it will give you a kick-start into what is in our opinion the best and most flexible workflow platform for engineers. The book consists of three parts. Part 1 explains the basics of GitHub actions. It uses a practical, hands-on approach – but it covers all basics with simple examples. Part 2 explains the workflow runtime in depth. This knowledge will help you completely understand the platform's architecture and security considerations. Part 3 covers the whole topic of CI/CD – the main use case for GitHub Actions. This part contains more complex real-life examples. It also dives into security, compliance, and optimizing performance and costs. Please let us know your thoughts in the liveBook Discussion forum on what's been written so far [and what you'd like to see in the rest of the book]. Your feedback will be invaluable in improving GitHub Actions in Action. Thanks again for your interest and for purchasing the MEAP! —Michael Kaufmann, Rob Bos, Marcel de Vries In this book welcome 1 Introduction to GitHub Actions 2 Hands-on: My first Actions Workflow 3 Workflows 4 GitHub Actions 5 Runners 6 Self-hosted runners
Page 5
1 Introduction to GitHub Actions This chapter covers the following topics Getting an introduction to the GitHub universe Understanding what GitHub Actions and Workflows are Learning about the possibilities of GitHub Actions that go beyond CI/CD pipelines Understanding licenses and pricing for GitHub and GitHub Actions GitHub is more than just a platform for hosting and sharing code. It has become the beating heart of the open-source community, with millions of developers from all over the world collaborating on projects of every type and size. Founded in 2008, GitHub has since grown to host over 200 million repositories and 100 million users, with a staggering 3.5 billion contributions made in the last year alone. And now, with GitHub Actions, developers have access to a powerful and flexible toolset for automating their workflows, from Continuous Integration (CI) and Continuous Deployment (CD) to custom automation tasks and beyond. GitHub Actions is much more than just a CI/CD tool – it's a comprehensive automation platform that can help streamline your entire development workflow. This book will show you how to make the most of GitHub Actions and take your development process to the next level. It is for everyone that wants to learn more about GitHub Actions – from complete beginners to already advanced users that want to bring their knowledge to the next level. You will learn how to use Actions effectively and secure, and it brings a lot of real- world examples for using it for CI/CD scenarios. 1.1 An introduction to the GitHub universe At the core of GitHub lies the essential component of version control, namely git. This system has played a significant role in transforming the way in
Page 6
which software is developed and is widely considered the standard for the versioning of code, which in this case does not just refer to program code. It includes infrastructure, configuration, documentation, and many other types of files. Git has risen to prominence due to its remarkable flexibility, which stems from its classification as a distributed version control system rather than a central one. As a result, developers can work while disconnected from the central repository, utilizing the full functionality of the version control system, and later synchronize changes with another repository. The efficacy of git's distributed architecture is attributed to its ability to store snapshots of files with changes in its database. GitHub has extended beyond its function as a hosting platform for git and has evolved into a comprehensive DevOps platform that supports collaborative coding through asynchronous means, such as pull requests and issues. The platform's capabilities have expanded into six broad categories, which include: collaborative coding, planning and tracking, workflows and CI/CD, developer productivity, client applications, and security. These categories encapsulate the key features that GitHub offers, making it a versatile and comprehensive DevOps platform that supports various stages of software development. From its inception, GitHub has prioritized a developer-centric approach, resulting in a platform that places utmost importance on webhooks and APIs. Developers can leverage either the REST API or the graph API to manipulate all aspects of the GitHub platform. Authentication is also a straightforward process, and developers can use GitHub as an identity provider to access their applications. This user-friendly approach facilitates seamless integration with other tools and platforms, making GitHub a versatile option not only for open-source projects but also for commercial products. GitHub's extensive ecosystem, which boasts over 100 million users, comprises the entire open- source community, who collaborate to expand and enrich its functionality.
Page 7
So, to understand the vastness of the GitHub ecosystem, one must also take into account the various integrations available: Planning and tracking: In addition to issues and milestones, GitHub offers GitHub Discussions, an entire forum for collaboration on ideas. Furthermore, GitHub Projects is a flexible planning solution that is fully integrated with issues and pull requests, and it supports nested backlogs, boards, and roadmaps. Moreover, GitHub integrates seamlessly with other popular planning and tracking solutions such as Azure Boards and Jira. Client applications: GitHub provides a fully-featured code editor that can be accessed directly in the browser. It also offers mobile applications for both iOS and Android platforms, enabling teams to collaborate from anywhere. Additionally, there is a cross-platform desktop application and an extensible CLI available. Furthermore, GitHub integrates smoothly with popular client applications such as Visual Studio, Visual Studio Code, and Eclipse. Moreover, it seamlessly integrates with popular chat platforms such as Slack and Teams. Security: GitHub provides a comprehensive solution for ensuring software supply-chain security, which includes several key features. For example, it generates Software Bills of Material (SBoMs) to keep track of all the components that are included in your software. And, with the Dependabot functionality, GitHub can alert you whenever vulnerabilities are detected in any of the dependencies you're using. Furthermore, GitHub can scan your repository to detect secrets, and it boasts a sophisticated code analysis engine called CodeQL. The platform also supports integrations with other security tools like Snyk, Veracode, or Checkmarx, and it can be integrated into Microsoft’s Defender for DevOps. Developer Productivity: In GitHub, developers can quickly create a customized containerized development environment using GitHub Codespaces. This allows new developers to be productive right away. Additionally, Copilot, an AI-powered assistant, can generate code based on the context of comments or other code. This can significantly increase productivity, with reports of up to 50% gains. GitHub also offers code search, the command palette, and other features that can further enhance developer productivity.
Page 8
Workflows and CI/CD: In the world of continuous integration and continuous delivery (CI/CD), GitHub is a popular platform that enjoys widespread support from most CI/CD tools in the market. Furthermore, GitHub provides a secure integration with all the major cloud providers for CI/CD workflows using Open ID Connect (OIDC). This ensures a secure and streamlined experience for developers who rely on cloud- based services. Additionally, GitHub Packages is equipped with a robust package registry that supports a wide range of package formats, providing a powerful and versatile tool for developers to manage and distribute their code packages. GitHub Actions serves as the automation engine for the GitHub ecosystem (see Figure 1.1). It allows users to automate various tasks, with a vast library of over 18,000 actions available in the marketplace. From issue triaging to automatic documentation generation, there is a building block – called Action – available to address nearly any task. With GitHub Actions, users can easily and securely automate their workflows. Figure 1.1 The GitHub ecosystem has thousands of integrations
Page 9
That’s why GitHub Actions is more than just CI/CD. It is an automation engine that can be used to automate any kind of manual tasks in engineering, and it is already used by millions of developers worldwide. It can be used to not only automate GitHub – but the entire GitHub Universe. 1.2 What are GitHub Actions and Workflows GitHub Actions is both the name of the workflow engine and the name of an individual, reusable, and easily sharable workflow step within GitHub. This can lead to confusion. Workflows are composed of YAML files that are stored in a specific repository location (.github/workflows). In Chapter 3, GitHub Action Workflows, you will gain a comprehensive understanding of GitHub Action workflows and the YAML syntax. Triggers initiate the workflow, and one or more jobs are included in the workflow. Jobs are executed on a workflow runner, which can be a machine or container with an installed runner service. GitHub offers runners with Linux, macOS, and Windows operating systems in various machine sizes, but you can also host
Page 10
your own runners. In Part 2, Workflow Runtime, you will learn about runners and the essential security measures to consider when hosting your own runners. Jobs execute in parallel by default, but the needs property can be used to chain jobs together. This enables you to fan out your workflow and run multiple jobs in parallel while waiting for all parallel jobs to complete before proceeding. Environments in GitHub Actions provide a way to protect jobs by defining protection rules such as manual approvals, wait timers, and protected secrets. With this, you can create visual workflows that track for example your entire release pipeline, giving you complete control over your deployment process. Please refer to Figure 1.2 for an example of a workflow with environments and approvals. Figure 1.2 A GitHub Workflow with environments and approvals A job is comprised of one or more steps that are executed sequentially. A step can take the form of a command line, script, or reusable step that is easily shareable, known as a GitHub Action. These Actions can be authored in JavaScript or TypeScript and executed in a NodeJS environment. Additionally, it is possible to run containers as Actions or create composite Actions that serve as a wrapper for one or multiple other Actions. Actions are covered in more depth in Chapter 4, GitHub Actions. In Figure 1.3 you can see an overview of the basic elements that make up a workflow and their syntax.
Page 11
Figure 1.3 The basic syntax and elements that make up a GitHub Actions workflow You will learn more about workflow syntax, YAML, GitHub Actions, and authoring and debugging workflows and Actions in Chapter 3, GitHub Action Workflows, and in Chapter 4, GitHub Actions. 1.3 GitHub Actions - More than CI/CD pipelines GitHub workflows are intended to automate various tasks. In addition to pushing code, there are numerous triggers available. A workflow can be activated when a label is added to an issue, when a pull request is opened, or when a repository is starred. In Listing 1.1 you can find an example workflow that applies labels to opened or edited issues based on the content of the body of the issue. Listing 1.1 A sample GitHub Actions workflow that can help triage GitHub issues name: Issue triage on: issues: types: [opened, edited] jobs:
Page 12
triage: runs-on: ubuntu-latest steps: - name: Label issue run: | if (contains(github.event.issue.body, 'bug')) { echo '::add-labels: bug'; } else if (contains(github.event.issue.body, 'feature')) { echo '::add-labels: feature'; } else { echo 'Labeling issue as needs-triage'; echo '::add-labels: needs-triage'; } This is just an example to show you the power of GitHub actions. GitHub does not automatically download or clone your repository when a workflow is executed. In many automation scenarios, the repository's code or files may not be required, and the workflow can be completed much faster without cloning the repository. If you intend to utilize GitHub Actions for CI/CD purposes, the first step in a job should be to download the code by utilizing the checkout action.: steps: - name: Checkout repository uses: actions/checkout@v3 This action will clone your repository, allowing you to build and test your solution. In Part 3, CI/CD with GitHub Actions, you will learn in depth how to use GitHub Actions for CI/CD in a secure and compliant way. 1.4 Hosting and pricing for GitHub and GitHub Actions GitHub (https://github.com) is hosted in data centers located in the United States. Signing up for GitHub is free and provides users with unlimited private and public repositories. While many features on GitHub are available for free on open-source projects, they may not be available for private
Page 13
repositories. Enterprises have a variety of options for hosting GitHub (see Figure 1.4). Figure 1.4 GitHub Enterprise Cloud, GitHub Enterprise Server, and GitHub Connect 1.4.1 GitHub Enterprise Cloud GitHub Enterprise Cloud (GHEC) is a Software as a Service (SaaS) offering from GitHub, and it is fully hosted on its cloud infrastructure in the United States. GHEC provides additional security features and supports single sign-on for users. With GHCE, users can host private and public repositories, including open-source projects within their enterprise environment. GHEC guarantees a monthly uptime Service Level Agreement (SLA) of 99.9%, which translates to a maximum downtime of 45 minutes per month. 1.4.2 GitHub Enterprise Server The GitHub Enterprise Server (GHES) is a system that can be hosted
Page 14
anywhere, either in a private data center or in a cloud environment like Azure, AWS, or GCP. Using GitHub Connect, it is possible to connect to GitHub.com, which enables the sharing of licenses and the use of open source on the server. GHES is based on the same source as GHEC, which means that all features eventually become available on the server a few months later. However, some features provided in the cloud must be managed independently on GHES. For instance, runners in GitHub Actions require self-hosted solutions, whereas the cloud provides GitHub-hosted runners. Managed Services are also available that provide hosting for GHES, including in an Azure data center within the user's region. This approach ensures full data residency and eliminates the need to manage the servers personally. Some Managed Services also include hosting for managed GitHub Actions runners. 1.4.3 Pricing for GitHub It is important to Understand the pricing model of GitHub and GitHub Actions when you start playing around with them, to not accidently burn though all you free minutes. GitHub's pricing model is based on a monthly per-user billing system and consists of three tiers: Free, Team, and Enterprise (see Figure 1.5). Figure 1.5 Overview of GitHub pricing triers
Page 15
Public repositories, and therefore open-source projects, are entirely free of charge and offer many features such as Actions, Packages, and various security features. Private repositories are also available for free, but with limited functionality, including 2,000 Action minutes and 500 MB of storage per month. To collaborate on private repositories with advanced features such as protected branches, codeowners, and enhanced pull request features, a team license is required. This license also includes access to Codespaces, although this feature requires a separate payment. Additionally, the team tier provides 3,000 free Action minutes per month and 2GB of monthly storage for packages. Free and Team tiers are only available on GitHub.com. If users require GitHub Enterprise Cloud or Server, the GitHub enterprise license must be purchased. This license includes all enterprise features, such as single sign- on, user management, auditing, and policies, along with 50,000 Action minutes and 50GB of storage for packages per month. It also allows for the purchase of additional add-ons, such as Advanced Security or Premium Support. 1.4.4 Pricing for GitHub Actions Hosted runners are provided for free to users with public repositories. The amount of storage and monthly build minutes available to users depends on
Page 16
their GitHub edition, as shown in Table 1.1: Table 1.1 Included storage and minutes for the different GitHub editions GitHub edition Storage Minutes Max concurrent jobs GitHub Free 500 MB 2,000 20 (5 for macOS) GitHub Pro 1 GB 3,000 40 (5 for macOS) GitHub Free for organizations 500 MB 2,000 20 (5 for macOS) GitHub Team 2 GB 3,000 60 (5 for macOS) GitHub Enterprise Cloud 50 GB 50,000 180 (50 for macOS) If you have purchased GitHub Enterprise through your Microsoft Enterprise Agreement, it is possible to link your Azure Subscription ID to your GitHub Enterprise account. This will allow you to use Azure Billing to pay for additional GitHub Actions usage beyond what is already included in your GitHub edition. It is important to note that jobs running on Windows and macOS runners consume more build minutes than those running on Linux. Windows consumes build minutes at a rate of 2x and macOS consumes build minutes at a rate of 10x, meaning that using 1,000 Windows minutes would use up 2,000 of the minutes included in your account, while using 1,000 macOS minutes would use up 10,000 minutes included in your account. This is due to the higher cost of build minutes on these operating systems. Users can pay for additional build minutes on top of what is included in their GitHub edition, with the following build minute costs for each operating system: Linux: $0.008 macOS: $0.08 Windows: $0.016 These prices are for the standard machines with 2 cores. The charges for extra storage are uniform for all runners, set at $0.25 per GB.
Page 17
In Chapter 5, Runners, you will learn in more detail, how minutes and extra storage are calculated. If you are a customer who is billed monthly, your account is subject to a default spending limit of $0 (USD), which restricts the use of extra storage or build minutes. However, if you pay by invoice, your account is given an unrestricted spending limit by default. If you set a spending limit above $0, any additional storage or minutes utilized beyond the included amounts in your account will be invoiced until the spending limit is reached. By setting up a spending limit, the Enterprise Administrators will receive e-mail notifications at reaching 75%, 90%, and 100% of the spending limit, on top of the default notifications for utilizing the same percentages of the included minutes in their monthly plan. You won't incur any costs when using self-hosted runners since you provide your own computing resources. It is important that you are aware of the costs when playing around with workflows. Especially if you try certain triggers. Best is to just use public repos for training purposes – in this case the workflows are free of charge in any case. 1.5 Conclusion In this chapter, you have learned about the GitHub ecosystem and the myriad of possibilities it offers for automating tasks, extending beyond just CI/CD, using GitHub Actions. You have become familiar with key terms and concepts related to Workflows and Actions, enabling you to better navigate and utilize these features. Additionally, you have explored the hosting options and pricing models available for both GitHub and GitHub Actions. Moving forward, the next chapter will provide an opportunity for practical application, as you embark on writing your first workflow. This initial exercise will serve as a useful foundation before delving further into the syntax and nuances of GitHub Action Workflows, which will be covered in Chapter 3.
Page 18
1.6 Summary The GitHub universe consists of a vast ecosystem of products, partners, and communities around the areas of collaborative coding, planning, and tracking, workflows and CI/CD, developer productivity, client applications, and security. GitHub Actions is a workflow engine to automate any kind of manual tasks in engineering in the GitHub ecosystem beyond CI/CD. GitHub Action workflows are YAML files in a repository in the folder .github/workflows and contain triggers, jobs, and steps. A GitHub Action is reusable workflow step that can be easily shared through the GitHub marketplace. GitHub Actions are free for public repositories and paid per minute for private ones if you use the GitHub hosted runners; but you have free included Actions minutes in all GitHub pricing tears. Private runners are always free – but the pricing for hosted runners varies depending on the machine size and type.
Page 19
2 Hands-on: My first Actions Workflow This chapter covers the following topics Creating a new workflow Using the workflow editor Using actions from the marketplace Running the workflow Before we dive into the details of the workflow and YAML syntax in Chapter 3, GitHub Workflows, it’s a good idea to familiarize ourselves with the workflow editor, gain some practical experience in creating a workflow, and test it out to see it in action. This hands-on approach will help us better understand the concepts and give us the ability to quickly try something out, if it is unclear. Don’t worry if there are parts of the workflow syntax that you don’t understand yet– we’ll be covering those in detail in the upcoming chapters. 2.1 Creating a new workflow Let's begin this hands-on lab by signing into your GitHub account. Then, follow the link https://github.com/new to create a new repository. To ensure you have unlimited Action minutes, create a new public repository in your user profile and name it ActionInAction. Initialize the repository with a readme so that we can retrieve the files in the workflow later on. Lastly, click on the Create repository button to complete the process (refer to Figure 2.1). Figure 2.1 Creating a new repository
Page 20
The repository You can find companion repositories in the GitHub Organization https://github.com/GitHubActionsInAction If you have already cloned the companion repository https://github.com/GitHubActionsInAction/Part1, you can also create a new workflow in this repository instead of creating a new one. Now, let's navigate to the Actions tab inside the repository. If this is a new repository and there are no workflows set up yet, you will automatically be redirected to the new Action page (Actions/new). This is the same page you
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List