🔖 The following instructions will take on the assumption that you will be completing the steps on the GitHub UI. Feel free to use the terminal or any other GUIs you are comfortable with.
-
Log into your GitHub account and create a new repository
- Give it a name, e.g.
universe-automation
, and a description - It does not matter if you set the visiblity to Private or Public
- Select
Add a README file
- Click
Create repository
- Give it a name, e.g.
💡 For the purpose of this exercise, we will be committing directly to your default branch (e.g. main) so you don't need to create a new branch beforehand
-
Add these files to the root of your repository;
package.json
, andindex.js
, using one of the following approaches:-
If you have
node
installed, and you have cloned the repository onto your local machine, run the following commands:npm init -y npm install
- Create an
index.js
file, see content description below - Update your
package.json
file, see content description below - Not sure if you have
node
installed? Runnode -v
in your terminal and see if it returns a version number or not. - To avoid committing your
node_modules
directory which is created after runningnpm install
, add a.gitignore
file to the root of your repository containing one line:node_modules/
- Create an
-
If you do not have
node
installed, then you can create the project files manually and copy the content into each;-
package.json
: Contains your node project meta dataClick here to view file contents to copy:
💡 Replace the placeholders OWNER with your GitHub handle or organization name (if you created the repository in an organization), and REPOSITORY_NAME.
⚠️ Your package name cannot contain uppercase letters! If your values for OWNER and/or REPOSITORY contains uppercase letters, change them to lowercase before proceeding.{ "name": "@<owner>/<repository-name>", "version": "1.0.0", "description": "Demo repository for GitHub Universe 2020", "main": "index.js", "scripts": { "test": "echo \"No tests specified, but that's alright for now!\" && exit 0" }, "repository": { "type": "git", "url": "git+https://github.com/<owner>/<repository-name>.git" }, "keywords": [], "author": "", "license": "ISC", "bugs": { "url": "https://github.com/<owner>/<repository-name>/issues" }, "homepage": "https://github.com/<owner>/<repository-name>#readme", "dependencies": {} }
-
index.js
: Your source codeClick here to view file contents to copy:
console.log("Hello, GitHub Universe!")
-
-
Make sure your changes are committed to your default branch before proceeding
-
-
Create the following file to the default branch:
.github/workflows/ci.yml
- This will be the workflow file taking care of building and testing your source code
- The file content will be empty for now
-
After the above steps are finished, you should have the following files in your repository;
package.json
index.js
.github/workflows/ci.yml
- If you created the files using
npm
on your local machine, you should also havepackage-lock.json
.gitignore
-
🎉 If everything looks fine so far, it's time to start creating our CI workflow!
-
Go to
.github/workflows/ci.yml
and enter edit mode by clicking the pencil 📝 iconClick here to view file contents to copy:
##################################### # Automate your workflow # # GitHub Universe Workshop 2020 # ##################################### # This workflow will run CI on your codebase, label your PR, and comment on the result name: MYWORKFLOW on: pull_request: # the workflow will trigger on every pull request event jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: [12.x, 14.x] # matrix for building and testing your code across multiple node versions steps: - name: Checkout uses: actions/checkout@v2 - name: Build node version ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm run build --if-present - run: npm test label: runs-on: ubuntu-latest needs: build #this ensures that we only trigger the label job if ci is successful steps: - name: Checkout uses: actions/checkout@v2 - uses: actions/github-script@v3 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | github.issues.addLabels({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, labels: ['release'] }) comment: runs-on: ubuntu-latest needs: [build, label] steps: - name: Checkout uses: actions/checkout@v2 - name: Comment on the result uses: actions/github-script@v3 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | github.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: ` Great job **@${context.payload.sender.login}**! Your CI passed, and the PR has been automatically labelled. Once ready, we will merge this PR to trigger the next part of the automation :rocket: ` })
-
⚠️ yaml
syntax relies on indentation, please make sure that this is not changed -
Update the name of your workflow
-
Within the
comment
job, you can edit the body of the comment as you see fit -
Commit the changes to your default branch
-
Knowledge Check
-
How many jobs are there in the workflow?
Answer
The workflow contains three jobs:- a build-job,
- a label-job,
- a comment-job
-
What is the event that will trigger this workflow?
Answer
The workflow is triggered by any pull request events. -
Does this workflow use a build matrix?
Answer
Yes, this workflow will build and test across multiple node versions.
-
🎉 Awesome, now our CI workflow should be complete!
-
To test our CI workflow, we need to trigger it. And the event that triggers it is
pull_request
. So let's create a pull request to get this workflow running -
Go to your
README.md
file and enter edit mode -
Make some changes/add some text
-
Commit your changes to a new branch e.g.
update-readme
and click Commit changes -
In the pull request, leave the title and the body to default and click Create pull request
-
In your PR, click on the Checks tab
- You should now see your workflow kicking off, and executing all the steps defined in
.github/workflows/ci.yml
- You should now see your workflow kicking off, and executing all the steps defined in
-
After the workflow has completed, check that the following is true in your PR;
- A label
release
has been added - A comment is added to the PR with the text corresponding to what you defined in the
comment
job inside.github/workflows/ci.yml
- A label
-
If your workflow fails, inspect the log output:
- Which job failed?
- Did you update your
package.json
file correctly? - Does the log indicate any syntax errors with your CI workflow file?