Practice for CD/CI with GitHub
Goal
- Understand the CD/CI on GitHub
Specification
Write a function leapyear that returns true or false depending on whether its input integer is a leap year or not.
A leap year is defined as one that is divisible by 4, but is not otherwise divisible by 100 unless it is also divisible by 400.
For example, 2001 is a typical common year and 1996 is a typical leap year, whereas 1900 is an atypical common year and 2000 is an atypical leap year.
Template
- leapyear.go
package main
func leapyear(y int) bool {
return true
}
- leapyear_test.go
package main
import (
"testing"
)
func TestLeapYear01(t *testing.T) {
expected := leapyaer(1)
actual := true
if expected != actual {
t.Errorf("Error")
}
}
Instructions
- Use TDD (Test-Driven Development) to implement the function.
- Manage TODO by GitHub Issue.
- Use mob programming (one person types, others guide) by all members.
- Use an online server (openvscode-server) to edit the code.
- Use GitHub Actions for CI.
Steps
1. Initialize the project
- Access the online server:
- Open the URL provided on the channel of MS Teams.
- Note: The server cannot be edited by multiple people at the same time, so only one person should edit it at a time.
- Initialize Git on the online server:
git config --global user.name "teamXX" git config --global user.email "teamXX@example.com" # use dummy email address
- Create a new repository on GitHub named
leapyear.- Go to GitHub and create a new repository named
leapyear. - Initialize it with a README.md file.
- Add other members of your team as collaborators to the repository.
- Go to GitHub and create a new repository named
- Clone the repository to the online server:
- Use the following command to clone the repository:
git clone https://github.com/yourusername/leapyear.git cd leapyear
- Use the following command to clone the repository:
- Initialize the project
go mod init leapyear- Create the
leapyear.goandleapyear_test.gofiles
- Commit the initial setup:
- Use the following commands to commit the initial setup:
git add leapyear.go leapyear_test.go README.md git commit -m "Initial setup with leapyear function and tests" git push origin main
- Use the following commands to commit the initial setup:
2. Setting up GitHub Issues for tasks
- List up the tasks for leapyear function (1 task and 4 subtasks):
- Make leapyear function
- Return false for any year
- Return true for the year is divisible by 4
- Return false for the year is divisible by 4 but it is divisible by 100
- Return true for the year is divisible by both 4 and 100 but it is divisible by 400
- Post each task to a GitHub Issue:
- Create a new issue for the leapyear function.
- Add subtasks as comments or checklist items in the issue.
- [ ] Make leapyear function - [ ] Return false for any year - [ ] Return true for the year is divisible by 4 - [ ] Return false for the year is divisible by 4 but it is divisible by 100 - [ ] Return true for the year is divisible by both 4 and 100 but it is divisible by 400
3. Setting up GitHub Actions for CI
- Create a
.github/workflows/ci.ymlfile in your repository.- This file will define the CI workflow for your project.
- Add the following content to
ci.yml
name: Go CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.18.x'
- name: Cache Go modules
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: $-go-$
restore-keys: |
$-go-
- name: Install dependencies
run: go mod tidy
- name: Run tests
run: go test -v ./...
This workflow will run on every push and pull request to the
mainbranch. It checks out the code, sets up Go, caches Go modules, installs dependencies, and runs tests.
- Commit the
ci.ymlfile:- Use the following commands to commit the CI configuration:
git add .github/workflows/ci.yml git commit -m "Add GitHub Actions CI configuration" git push origin main
- Use the following commands to commit the CI configuration:
4. Implementing the leapyear function
- Make a feature branch for the leapyear function:
- Use the following command to create a new branch:
git checkout -b develop
- Use the following command to create a new branch:
- Do TDD Cycles for a subtask
- Pick up a subtask from the GitHub Issue.
- Write a test for the subtask in
leapyear_test.go - Run the test to see it fail (red).
- Implement the function in
leapyear.goto make the test pass (green). - Refactor the code if necessary.
- Repeat the cycle until the subtask is complete.
- Commit and push your changes to GitHub:
- Use the following commands to commit and push your changes:
git add leapyear.go leapyear_test.go git commit -m "Implement leapyear function for subtask" git push origin develop - Check a list of GitHub Issues
- Use the following commands to commit and push your changes:
- Repeat 2 and 3 for each subtask until all subtasks are complete.
5. Merging the feature branch
- Create a pull request from develop to main:
- Go to GitHub → Pull Requests → New Pull Request
- Base branch: main, Compare branch: develop
- Review the pull request:
- Confirm tests pass
- Review code and checklist completion
- Merge the PR
- If the PR is linked to an issue, include
Closes #issue_numberin the PR description to automatically close the issue. - Click “Merge pull request”
- Optionally delete the
developbranch
- If the PR is linked to an issue, include
Submit the Exercise
- After completing the exercise, submit the URL to your GitHub repository where you implemented the leapyear function and the CI configuration. In the Exercise title field, select Practice for CD/CI with GitHub.