- How to fork the repository
- Creating a workflow file
- Triggering a workflow
- Verifying changes before merging codes
- Creating pull request
- Setting branch protection
- Deploying to heroku
- Let's change some codes
- How could we fix when some test failed?
- More Learning Hubs
- More Agoda
- Click on
Fork
button (on the top-right) - Select your Github account
- Go to
Code
tab in Github repository - Create a folder named
.github/workflows
inside the repository by clickingCreate new file
, paste.github/workflows
into text box - Create a workflow file inside
.github/workflows
, filename can be anything but extension must be.yml
e.g..github/workflows/cicd-workflow.yml
# workflow name name: Nodejs # event of the workflow on: push jobs: build: name: Nodejs # specify a host machine for the job e.g. ubuntu-latest, windows-latest runs-on: ubuntu-latest # define environment variables using in the job env: IMAGE_NAME: nodejs-example IMAGE_TAG: latest steps: # checkout codes to a build server - uses: actions/checkout@v1 # build docker image - name: Docker build run: docker build -t $IMAGE_NAME:$IMAGE_TAG . # run tests - name: Test run: docker run --rm -i $IMAGE_NAME:$IMAGE_TAG yarn test
- Click
Commit new file
button
NOTE: You can create more than one workflow in the repository.
Workflow will be triggered by the event defined above.
For example,
push
event will be triggered when someone push some codes into the repositorypull_request
event will be triggered when someone open a pull request
After pushing your changes, go to Actions
tab in your Github repository. The changes will be verified and you should see the green check in front of the workflow name when it is finished.
- Go back to
Code
tab - Go to
.github/workflows/cicd-workflow.yml
, edit the file by clicking at pencil icon - Change the event of the workflow from
on: push
to beon: pull_request
name: Nodejs
on: pull_request
- Select
Start commit
dropdown (top-right) and clickCommit changes
button - Go back to
Actions
tab and you should not see a new build running
- Go to
README.md
file in root path, edit the file by clicking at pencil icon - Change the repository name in the first line from
![](https://github.com/agoda-com/cicd/workflows/Nodejs/badge.svg)
to be
![](https://github.com/<your_github_account>/cicd/workflows/Nodejs/badge.svg)
Commit changes
by selectingCreate a new branch for this commit and start a pull request.
- Click
Propose file change
button - You will see the difference of your changes and
Create pull request
- Go to
Actions
tab and wait forpull_request
workflow to be triggered - Go to
Pull requests
tab, you will see the check namedNodejs (pull_request)
is running
Protect the master branch from having commits not tested merged by requiring status checks to pass before merging a pull request
- Select
Settings
of your github repository - Go to
Branches
in the left menu and click onAdd rule
button
- Branch name pattern:
master
- Check on
Require status checks to pass before merging
- Select
Nodejs
workflow
- Go back to
Pull requests
, you will seeRequired
badge inside the check - Wait for all checks to pass and click on
Merge pull request
button
- Sign up in Heroku: https://signup.heroku.com/login
- Verify your account in registered e-mail
- Log in to your account
- Select
New
(on the top-right) and thenCreate new app
- Fill
App Name
e.g.<your_name>-cicd
- Click on your avatar on the top-right corner
- Select
Account settings
- Scroll down to
API Key
andGenerate API Key...
- Copy
API Key
- Select
Settings
of your github repository - Go to
Secrets
andAdd a new secret
- Name: HEROKU_API_KEY
- Value:
<your_heroku_api_key>
- Go back to
Code
tab and go inside.github/workflows
directory - Select
Create new file
, filename can be anything but extension must be.yml
e.g..github/workflows/master-cicd.yml
name: Nodejs
on:
push:
# the job will be triggered when there are changes in master
branches:
- master
jobs:
build:
name: Nodejs
runs-on: ubuntu-latest
# environment variables for Heroku
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
HEROKU_APP: <your_name>-cicd
steps:
- uses: actions/checkout@v1
# login to Heroku with Heroku API key
- name: Heroku login
uses: "actions/heroku@master"
with:
args: "container:login"
secrets: |
$HEROKU_API_KEY
# push codes to Heroku
- name: Heroku push
uses: "actions/heroku@master"
with:
args: "container:push -a $HEROKU_APP web"
secrets: |
$HEROKU_API_KEY
# release
- name: Heroku release
uses: "actions/heroku@master"
with:
args: "container:release -a $HEROKU_APP web"
secrets: |
$HEROKU_API_KEY
- Commit new file by selecting
Create a new branch for this commit and start a pull request.
- Name your branch to be understandable e.g.
add-master-workflow
- Click
Propose new file
button - You will see the difference of your changes and
Create pull request
- Wait for
pull_request
workflow to be triggered and run test - When everything is green, you can merge this pull request to
master
master
workflow and deployment to Heroku will be triggered following steps defined above- Your deployment result can be seen at http://<your_app_name>.herokuapp.com/
NOTE: This will be triggered only when code is pushed to master branch.
- Go back to
Code
tab and selectserver.js
file - Edit the file by clicking at pencil
- Update greeting text from
Hello Everyone!!
toHello <your_name>!!
Commit changes
by selectingCreate a new branch for this commit and start a pull request.
- Name your branch to be understandable e.g.
update-greeting
- Click
Commit changes
button andCreate pull request
- Wait for
pull_request
workflow to be triggered and run test
- Go back to
Code
tab - Select
update-greeting
branch - Go to
tests
directory and selecterver.test.js
file - Edit the file by clicking at pencil
- Update greeting text from
Hello Everyone
toHello <your_name>
Commit changes
by selectingCommit directly to the update-greeting branch.
- Click
Commit changes
button - The previous pull request is triggered
- If everything is green, you can merge this pull request to
master
- Wait for
master
workflow and deployment to Heroku to be triggered - Your deployment result can be seen at http://<your_app_name>.herokuapp.com/