Lab: Custom Workflow
Introduction
In this lab, you will create a workflow using a starter file and customize it to trigger on specific events. You will also add steps to the workflow to interact with GitHub Actions and automate tasks.
Estimated Duration: 20-30 minutes
Instructions
Step 1: Create a Custom Workflow Using the Starter File
- Navigate to the Custom Workflow Starter File.
-
Copy the content of the starter file:
name: Intro - Custom Workflow
- In your repository, create a new workflow file under
.github/workflows
and name itintro-custom-workflow.yml
. - Paste the copied content into the new file.
- Commit the workflow file to the
main
branch.
Step 2: Customize the Workflow Triggers
- Open the
intro-custom-workflow.yml
file. -
Add the following
on
section to define the workflow triggers:on: workflow_dispatch: push: paths: - '.github/workflows/intro-custom-workflow.yml'
- Commit the updated workflow to the
main
branch. - Trigger the workflow manually from the Actions tab or modify the
intro-custom-workflow.yml
file and commit the changes to see the workflow run.
Step 3: Add Steps to Your Workflow
- Open the
intro-custom-workflow.yml
file again. -
Add the following
jobs
section to define tasks that the workflow will perform:jobs: execute: runs-on: ubuntu-latest steps: - run: echo "🎉 The job was automatically triggered by a $ event." - run: echo "🐧 This job is now running on a $ server hosted by GitHub!" - run: echo "🔎 The name of your branch is $ and your repository is $." - name: Check Code uses: actions/checkout@v4 - run: echo "💡 The $ repository has been cloned to the runner." - run: echo "🖥️ The workflow is now ready to test your code on the runner." - name: List Files In Directory run: | ls $ - run: echo "🍏 This job's status is $." - name: Adding Markdown run: echo "### Hello world! :rocket:" >> "$GITHUB_STEP_SUMMARY" # This step uses GitHub's hello-world-javascript-action - name: Hello World uses: actions/hello-world-javascript-action@main with: who-to-greet: 'Mona the Octocat' id: hello # This step prints an output (time) from the previous step's action. - name: Echo Greeting's Time run: echo 'The time was $.'
- Commit the changes to the
main
branch. - Trigger the workflow by modifying the file or using the Actions tab.
Step 4: Understanding the Workflow
The intro-custom-workflow.yml
workflow includes the following sections:
Name
The workflow is named Intro - Custom Workflow
.
Trigger (on
)
workflow_dispatch
:- Allows manual triggering of the workflow from the Actions tab.
push
:- Runs the workflow when the
.github/workflows/intro-custom-workflow.yml
file is modified and committed.
- Runs the workflow when the
Job (jobs.execute
)
-
runs-on
:- Specifies that the job will run on an
ubuntu-latest
GitHub-hosted runner.
- Specifies that the job will run on an
-
Steps:
- Log Metadata:
- Outputs information about the event, runner, and repository.
- Check Out Code:
- Uses the
actions/checkout@v4
action to clone the repository into the runner.
- Uses the
- Run Commands:
- Lists the repository’s files and logs the job’s status.
- Add Markdown Summary:
- Appends a message to the workflow’s Summary tab.
- Hello World Action:
- Greets “Mona the Octocat” using the
hello-world-javascript-action
.
- Greets “Mona the Octocat” using the
- Output Time:
- Displays the timestamp generated by the Hello World action.
- Log Metadata:
Step 5: Run the Workflow
- Go to the Actions tab in your repository.
- You should see the newly created workflow listed there.
-
To run the workflow:
- If it’s triggered by a push event, simply make a change to the
.github/workflows/intro-custom-workflow.yml
file and commit the change. - Alternatively, if you want to run it manually, click the Run workflow button from the Actions tab and select the main branch.
- If it’s triggered by a push event, simply make a change to the
- The workflow will start running, and you can monitor its progress.
Step 6: View the Results
- Once the workflow has run, click on the workflow run to view its details.
- Inside the workflow details page, click on the run job to view the job’s specifics.
- Expand the step titled Hello World to view the output, which will include a greeting to Mona the Octocat and the timestamp from the action.
- You can also review the other steps, like the repository metadata and the file listing.
Optional: Trigger the Workflow on Any Push
If you want to run the workflow on any push to the repository:
-
Update the
on
section as follows:on: workflow_dispatch: push: branches: - main
-
Commit the changes to the
main
branch.
Summary
In this lab, you:
- Created a workflow using the Custom Workflow Starter File.
- Customized the workflow to trigger on specific events.
- Added steps to perform tasks like checking out code, listing files, and greeting Mona the Octocat.
- Ran the workflow and viewed the results.
This lab demonstrated how to create, customize, and execute a workflow using GitHub Actions.