|
| 1 | +# 🔨 Hands-on: My first workflow |
| 2 | + |
| 3 | +In this hands-on lab your will create your first GitHub Actions Workflow and learn how you can use Actions to automate tasks in your software development lifecycle. If you like more background information, please refer to the [GitHub Actions](https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions) pages on GitHub Docs. Good luck! 👍 |
| 4 | + |
| 5 | +This hands on lab consists of the following steps: |
| 6 | +- [Creating a repository](#creating-a-repository) |
| 7 | +- [Creating the workflow](#creating-the-workflow) |
| 8 | +- [Viewing your workflow results](#viewing-your-workflow-results) |
| 9 | + |
| 10 | +## Creating a repository |
| 11 | + |
| 12 | +Go to [The ActionsFundamentals repository in the ps-actions-sandbox organization](https://github.com/ps-actions-sandbox/ActionsFundamentals) and click <kbd>Use this template</kbd>: |
| 13 | + |
| 14 | +<img width="400" alt="2022-09-18_11-24-58" src="https://user-images.githubusercontent.com/5276337/190895393-6fa0fad9-e05c-4fea-8126-a291b087d663.png"> |
| 15 | + |
| 16 | +Select your GitHub user as the owner and name the repository. Leave the repo public to have unlimited action minutes: |
| 17 | + |
| 18 | +<img width="400" alt="2022-09-18_11-25-57" src="https://user-images.githubusercontent.com/5276337/190895398-751a1ec9-c1cf-497f-beb7-a6b53d4d911e.png"> |
| 19 | + |
| 20 | +Continue now in the new repository. |
| 21 | + |
| 22 | +## Creating the workflow |
| 23 | + |
| 24 | +Go to **Actions** | [New Workflow](/../../actions/new) and click on [set up a workflow yourself](/../../new/main?filename=.github%2Fworkflows%2Fmain.yml&workflow_template=blank). |
| 25 | + |
| 26 | +1. Rename the file `main.yml` in the `.github/workflows` directory to `github-actions-demo.yml`. |
| 27 | +<img width="400" alt="image" src="https://user-images.githubusercontent.com/5276337/174096754-4e2d7219-9caf-42e8-bfd9-c190762886d3.png"> |
| 28 | + |
| 29 | +2. Remove the template content - we want to create the workflow from scratch. |
| 30 | +3. Click <kbd>Ctrl</kbd>+<kbd>Space</kbd> and select name as the first element: |
| 31 | + |
| 32 | +<img width="400" alt="image" src="https://user-images.githubusercontent.com/5276337/174097468-8be92e37-7948-4895-b5ed-20a22c5773bc.png"> |
| 33 | + |
| 34 | +4. Set the workflow name to `GitHub Actions Demo`: |
| 35 | + |
| 36 | +```YAML |
| 37 | +name: GitHub Actions Demo |
| 38 | +``` |
| 39 | +
|
| 40 | +5. Add the triggers to the worklow with the help of <kbd>Ctrl</kbd>+<kbd>Space</kbd> and the documentation. We want the workflow to trigger: |
| 41 | + - on every push to the `main` branch, but not when there are changes to files in the `.github` folder. |
| 42 | + - on every pull request with `main` as the base branch |
| 43 | + - Every sunday at 6:15 UTC |
| 44 | + - Manually |
| 45 | + |
| 46 | +<details> |
| 47 | + <summary>Solution</summary> |
| 48 | + |
| 49 | +```YAML |
| 50 | +on: |
| 51 | + push: |
| 52 | + branches: [ main ] |
| 53 | + paths-ignore: [.github/**] |
| 54 | + pull_request: |
| 55 | + branches: [ main ] |
| 56 | + schedule: |
| 57 | + - cron: '15 6 * * 0' |
| 58 | + workflow_dispatch: |
| 59 | +``` |
| 60 | + |
| 61 | +</details> |
| 62 | + |
| 63 | +6. Create a job `Build` that runs on the latest Ubuntu image on GitHub hosted runners. Check the documentation of the [virtual environments](https://github.com/actions/virtual-environments/) what label to use and what version it is. The job should do the following things: |
| 64 | + - Output the name of the event that triggered the workflow |
| 65 | + - Output the name of the branch that the repository is currently referencing |
| 66 | + - List all files in the repository |
| 67 | + |
| 68 | +<details> |
| 69 | + <summary>Solution</summary> |
| 70 | + |
| 71 | +```YAML |
| 72 | +jobs: |
| 73 | + Build: |
| 74 | + runs-on: ubuntu-latest |
| 75 | + steps: |
| 76 | + - run: | |
| 77 | + echo "🎉 The job was triggered by event: ${{ github.event_name }}" |
| 78 | + echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ." |
| 79 | +
|
| 80 | + |
| 81 | +
|
| 82 | + - name: List files in the repository |
| 83 | + run: | |
| 84 | + echo "The repository ${{ github.repository }} contains the following files:" |
| 85 | + tree |
| 86 | +``` |
| 87 | + |
| 88 | +</details> |
| 89 | + |
| 90 | +7. Commit the workflow file - and trigger the workflow manually. It should not run automatically if your path filter works. Go to [Action](/../../Actions), select [GitHub Actions Demo](/../../actions/workflows/github-actions-demo.yml) and `Run workflow`: |
| 91 | + |
| 92 | +<img width="600" alt="image" src="https://user-images.githubusercontent.com/5276337/174105162-19f33fd1-8533-4860-9279-88fabec84451.png"> |
| 93 | + |
| 94 | + |
| 95 | +## Viewing your workflow results |
| 96 | + |
| 97 | +1. Click on your workflow run: |
| 98 | + |
| 99 | +<img width="600" alt="image" src="https://user-images.githubusercontent.com/5276337/174105747-0e205e0d-37cc-464c-905b-5b29be74fc75.png"> |
| 100 | + |
| 101 | +2. Click on the job 'Build': |
| 102 | + |
| 103 | +<img width="400" alt="image" src="https://user-images.githubusercontent.com/5276337/174105990-a1c204c6-fb7d-44a4-9343-6982899edb25.png"> |
| 104 | + |
| 105 | +3. Expand `Set up job` and note the log with the line number (line numbers are links). Check the information about the virtual environment and included software: |
| 106 | + |
| 107 | +<img width="600" alt="image" src="https://user-images.githubusercontent.com/5276337/174106759-c2a8f933-74cf-42a4-899b-b29dc67eccd7.png"> |
| 108 | + |
| 109 | +4. Expand your jobs and check that the output was correct. |
| 110 | + |
| 111 | +<img width="400" alt="image" src="https://user-images.githubusercontent.com/5276337/174107136-af9187c1-dbee-4109-9ddc-f2abd4830282.png"> |
| 112 | + |
| 113 | +5. Verify your other triggers by modifying the [README.md](/README.md) file: |
| 114 | + - Modify and commit: triggers build (`push`) |
| 115 | + - Modify and add `[skip ci]` (not triggering the workflow): |
| 116 | + <img width="350" alt="image" src="https://user-images.githubusercontent.com/5276337/174110845-93d4a38a-9c8a-4336-9b6a-9089ea9a1cfd.png"> |
| 117 | + |
| 118 | + - Modify the [README.md](/README.md) file and create a pull request (trigger: `pull_request`) |
| 119 | + |
| 120 | +## Summary |
| 121 | + |
| 122 | +In this hands-on you've learned to create you first workflow with triggers, jobs, steps, and expressions. Next you will write your [first GitHub Action](02-My-first-action.md). |
0 commit comments