Create a GitHub Action and use it in a workflow.
Welcome to "Hello GitHub Actions"! 👋
What is GitHub Actions?: GitHub Actions is a flexible way to automate nearly every aspect of your team's software workflow. You can automate testing, continuously deploy, review code, manage issues and pull requests, and much more. The best part, these workflows are stored as code in your repository and easily shared and reused across teams. To learn more, check out these resources:
- The GitHub Actions feature page, see GitHub Actions.
- The "GitHub Actions" user documentation, see GitHub Actions.
What is a workflow?: A workflow is a configurable automated process that will run one or more jobs. Workflows are defined in special files in the .github/workflows
directory and they execute based on your chosen event. For this exercise, we'll use a push
event.
- To read more about workflows, jobs, and events, see "Understanding GitHub Actions".
- If you want to learn more about the
push
event before using it, see "push".
To get you started, we used actions to go ahead and made a branch and pull request for you.
- Open a new browser tab, and navigate to this same repository. Then, work on the steps in your second tab while you read the instructions in this tab.
- Navigate to the Code tab.
- From the main branch dropdown, click on the emoji-workflow branch.
- Navigate to the
.github/workflows/
folder, then select Add file and click on Create new file. - In the Name your file... field, enter
emoji.yml
. - Add the following content to the
emoji.yml
file:name: Check emoji shortcode on: push
- To commit your changes, click Commit new file.
- Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one.
Nice work! 🎉 You added a workflow file!
Here's what it means:
name: A workflow for my Hello World file
gives your workflow a name. This name appears on any pull request or in the Actions tab of your repository.on: push
indicates that your workflow will execute anytime code is pushed to your repository.
Next, we need to specify jobs to run.
What is a job?: A job is a set of steps in a workflow that execute on the same runner (a runner is a server than runs your workflows when triggered). Workflows have jobs, and jobs have steps. Steps are executed in order and are dependent on each other. We'll add steps in the next step of this exercise. To read more about jobs, see "Jobs".
In this step of our exercise, we will add a "build" job. We will specify ubuntu-latest
as the fastest and cheapest job runner available. If you want to read more about why we'll use that runner, see the code explanation for the line runs-on: ubuntu-latest
in the "Understanding the workflow file" article.
- Open your
emoji.yml
file. - Update the contents of the file to:
name: Check emoji shortcode on: push jobs: build: name: Check emoji shortcode runs-on: ubuntu-latest
- Click Start commit in the top right of the workflow editor.
- Type your commit message and commit your changes directly to your branch.
- Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one.
Nice work adding a job to your workflow! 💃
Workflows have jobs, and jobs have steps. So now we'll add steps to your workflow.
What are steps?: Actions steps will run during our job in order. Each step is either a shell script that will be executed, or an action that will be run. Each step must pass for the next step to run. Actions steps can be used from within the same repository, from any other public repository, or from a published Docker container image.
In our action, we will have steps for each of the following:
- Checkout the code with
git checkout
, using a pre-built checkout action. - Run a bash script to check Markdown files.
- Fail with (
exit 1
) if any Markdown file contains an emoji without using emoji shortcodes.
- Open your
emoji.yml
file. - Update the contents of the file to:
name: Check emoji shortcode on: push jobs: build: name: Check emoji shortcode runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: | if LC_ALL=C grep -R '[^ -~]' *.md; then echo "Use emoji shortcodes instead!" echo "See https://github.com/ikatyang/emoji-cheat-sheet/blob/master/README.md" exit 1 fi
- Click Start commit in the top right of the workflow editor.
- Type your commit message and commit your changes directly to your branch.
- Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one.
You're now able to write and run an Actions workflow! ✨
Merge your pull request so the action will be a part of the main
branch.
- In your repo, click on the Pull requests tab.
- Click on the Create emoji shortcode workflow pull request.
- Click Merge pull request, then click Confirm merge.
- Optionally, click Delete branch to delete your
emoji-workflow
branch. - Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one.
You've now got a fully functioning workflow! 😄
Your new action will run any time a new commit is created or pushed to the remote repository. Since you just created a commit, the workflow should have been triggered.
Seeing your action in action: The status of your action is shown in a pull request before you merge, look for All checks have passed when you try out the steps below. You can also view them from the Actions tab in your repository. From there, you will see all the actions that have run, and you can click on each action to view details and access log files.
- Make a new branch named
test-workflow
. - Commit any change to your branch, such as adding an emoji to your README.md file. If you want to see what happens if an action fails, add an emoji without using shortcode.
- View the pull request on your branch.
- See your action run on your pull request.
- Wait about 20 seconds for actions to run, then refresh this page (the one you're following instructions from) and an action will automatically close this step and open the next one.
Congratulations friend, you've completed this course!
Here's a recap of all the tasks you've accomplished in your repository:
- You've created your first GitHub Actions workflow file.
- You learned where to make your workflow file.
- You created an event trigger, a job, and steps for your workflow.
- You're ready to automate anything you can dream of.
- Learn more about GitHub Actions by reading "Learn GitHub Actions".
- Use actions created by others in awesome-actions.
- We'd love to hear what you thought of this course in our discussion board.
- Take another GitHub Skills course.
- Learn more about GitHub by reading the "Get started" docs.
- To find projects to contribute to, check out GitHub Explore.
Get help: Post in our discussion board • Review the GitHub status page
© 2022 GitHub • Code of Conduct • CC-BY-4.0 License