CI/CD in GithubAction

Posted on September 2, 2022
Tags: javascript

1 CI :exec after push local repo to github

CI is used to integrate tests, typically your github action will run tests on the code.

  1. Give name
name: SomeRandomName
  1. Run on push to main branch
on:
	push: 
		branches: [main]
  1. Define the job to start Steps) are the instructions to test and run code actions/checkout@v2 is what clones the repo to your VM

WARNING jobs will run in parallel, steps do not if you need a job to wait, add a “needs:…” parameter under

jobs:
	build-job:
		...
	publish-job:
		needs:build-job
jobs:
	yourJobName:
		runs-on: ubuntu-latest
		steps: 
			- uses: actions/checkout@v2
			- uses: actions/setup-node@v1
			  with:
				node-version: 12
				
			- run: npm ci  <--- npm install
			- run: npm test
			- run: npm run build

2 CD :exec on merge PullRequest

CD is used to push your updates to some cloud server so the internet can see your new updated website.

You have to connect to servers like AWS to deploy your website First you need a secret token from AWS on Github: Settings > Secrets > Add a new secret Now you can securely access with github action securely

on:
	pull_request:
		branches: [main]
jobs:
	yourJobName:
		runs-on: ubuntu-latest
		steps: 
			- uses: actions/checkout@v2
			- uses: actions/setup-node@v1
			  with:
				node-version: 12
				
			- run: npm ci  <--- npm install
			- run: npm test
			- run: npm run build
			- uses: bleh/someAWSaction@main
			  with: 
				args: deploy --only hosting
			  env:
				AWS_TOKEN: ${{ secrets.myAWS_TOKENNAME }}

3 Conclusion

  1. push local dev branch to remote github
  2. CI Starts test on new code
    • if tests fail -> fix bugs
    • if tests succeed -> Ask PR to merge dev with main branch
  3. Maintainer accepts PR
  4. main branch merges with dev
  5. CD Starts uploading new main branch to servers
  6. new update live on internet or server

4 Quirks

4.1 Concurrent behavior in run

run: |
	 bleh=4
	 echo $bleh

The echo will show no output because run will call the two commands concurrently.
To call sequentially, the commands need to be in two separate run.

4.2 Github environment variable

echo "somevar=3" >> $GITHUB_ENV
${{ env.somevar }}
$somevar

line 2 and 3 are the same
use $somevar over ${{ env.somevar }}

The local environment is decarded on separate runs, so its best to store variables in $GITHUB_ENV.