Git Hub: Learn Auto Commits with Racoon Memes🦝 🗑
Before you start, you'll need:
1. a code editor(I used Vscode)
2. a github account
3. knowledge of secrets(github projects)
4. knowledge of PAT generation
5. The internet 🧑💻
Introduction 👋
When I first learned about Github I was blown away. Loved how it handled version control, security on projects, and collaboration with other developers. Like so many others I was taken back by the commit feature. In particular, the contributions feature was really cool. I really got a kick out of the visual representation of how often I made commits.
It was a great way for me to gauge my productivity. Which drove me to make more commits. It didn’t take much time before I realized, not every commitment would have to be meaningful. Which sort of defeats the purpose, as commits frequency should equal micro-enhancements to your project or task. They shouldn’t be viewed as video game achievement.
I have noticed over the last few months, most of my meaningful productivity comes from understanding the nuances in systems architecture, reading documentation, paired programming for debugging, or debugging solo. Most of the time none of this work is accounted for in commits. Furthermore, if you are working at an internship or job, chances are you’re not getting commits either as it’s a private codebase.
You often find it’s easy to throw up code, but it’s harder to make that code meaningful. You’ll get a lot of commits starting a project, but often the real work starts in maintaining and building on that code. All this valid work does not get accounted for in git commits. You will be able to showcase what you’ve built or how often you make commits in version control(so important for debugging), yet that’s not the whole picture.
Why Learn? 🤨
Simple it will make your life a little easier. Many people entering the industry have lives. We need to do laundry, cook, clean, shower, maybe even work to support our coding habits. Some folks have the privilege to always be learning and working while others simply do not. So why not give yourself a leg up in the race?
You’ll learn more about Git hub actions, further, your understanding of automation with these actions, feel a bit less guilt for not contributing every day, and walk away with one less thing to worth about right?
Diving in 🤿
Let’s start by creating a new project. Will create a new repo for this as well on git hub. Once you’ve made your first initial commit, will head to our repo page in the terminal.
Let’s make a text file at our root folder for this project, which will call “LATEST_UPDATE”. I’ll explain a bit later but this will act as an update log for our commits.
I’m choosing to name my repo “auto-commits”, but you can name your repo whatever you like! I’m also choosing to make this workflow on my main branch. In an actual project I may choose a different design, so just keep this in mind.
Will want to make a new workflow template, so let’s click on the blue “set up a workflow yourself”. This will send us to the next page.
Interesting page right, see how we are given this whole route to the .yml file? Github will make this file structure for us. It even gives us a super cool template to work off of, how helpful!
So there’s some weird syntax here(.yml), don’t worry it’s pretty easy to understand. If you’d like to take a second to learn more about .yml and GitHub check out the link I’ve provided below.
Wanna learn more about .yml + Github ? 🧐
https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
Let’s name this workflow, scroll to the top and enter a name you’d like. Make sure you name it something relevant to what you are trying to accomplish. Like so…
name: CI Auto Commit 🤩
Next will want to determine WHEN this action will run. I want this action to run. I decided on running it every day. Will need to use the schedule syntax, which will be nested like this…
name: CI Auto Commit on:
schedule:
"run on this schedule"
The schedule needs to know when to run. We do this using cron syntax. This may seem a bit scary, so check out the crontab.guru link below.
More on cron: https://en.wikipedia.org/wiki/CronLink to crontab.guru:Set your time: https://crontab.guru/
I decided on running my auto-commit… “At minute 50 past every hour from 10 through 11.”.
name: CI Auto Commit
on:
schedule:
- cron: "50 10-11 * * *" //<-- here 😳
Will now need to specify what we want to get done in jobs, name that job and decide what operating system will we be using. Let’s do this under “jobs:” Let’s do that next.
name: CI Auto Commit
on:
schedule:
- cron: "50 10-11 * * *"jobs:
auto-commit: //<-- name of job 🤩
runs-on: ubuntu-latest //<-- select operation system 😯
Cool. Let’s set up “steps:”. They are a sequence of tasks that will run in the workflow. I want first to use a GitHub action called checkout@v2. Well also need the ‘uses:’ syntax for this.
Read more about Checkout@v2 here:repo: https://github.com/actions/checkout
Will want to set persist-credentials to false, we don’t want to configure a token or SSH key with the local git config. We will also set the fetch-depth to zero as we won’t be fetching any commits. fetch-depth has a default of one, which isn’t want we want.
name: CI Auto Commit
on:
schedule:
- cron: "50 10-11 * * *"jobs:
auto-commit:
runs-on: ubuntu-latest steps:
- uses: actions/checkout@v2 //<--- github action 😳
with:
persist-credentials: false //<-- no local git config 🤩
fetch-depth: 0 //<-- manually set to zero 😯
Now we can run single shell commands. Will be run them using bash. It’s pretty straightforward and looks like this…
Looks like this... 👀 steps:
- name: "what it's called"
run: "Shell command"
We will work on updating our timestamp, making commits, and pushing the changes. Will need to generate a PAT and add it to our project's secrets for security purposes. If you haven’t done this yet, please do so.
Welcome back. Now that we got these ready, let’s move on to our first step. It’s gonna be a multi-liner so always start with a pipe or ‘|’. Remember we are writing in shell now NOT .yml.
name: CI Auto Commit
on:
schedule:
- cron: "50 10-11 * * *"jobs:
auto-commit:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v2
with:
persist-credentials: false
fetch-depth: 0 - name: Update timestamp
run: | //<-- multi-lined code 😯
d=`date '+%Y-%m-%dT%H:%M:%SZ'` //<-- shell date format 😯
echo $d > LATEST_UPDATE //<-- our timestamp file 😎
Next, let’s write our commits shell command. Will be using the secrets username syntax, which hides our secret. We also will add our username as well. Then, use the standard “git add .” command.
name: CI Auto Commit
on:
schedule:
- cron: "50 10-11 * * *"jobs:
auto-commit:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v2
with:
persist-credentials: false
fetch-depth: 0- name: Update timestamp
run: |
d=`date '+%Y-%m-%dT%H:%M:%SZ'`
echo $d > LATEST_UPDATE- name: Commit changes
run: |
git config --local user.email ${{ secrets.USER_EMAIL }} 🕵️
git config --local user.name "Sambi85"//<-- your username 😯
git add .
You could just a message like ‘commit ’ but I went a little further and made an array of cat sounds. I’m randomly select one of them and return them. The idea being, we should see a random message on an action. Feel free to change the message to whatever you like. Will fold that into a standard “git commit -m” command…. Meow! 🐈
name: CI Auto Commit
on:
schedule:
- cron: "50 10-11 * * *"jobs:
auto-commit:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v2
with:
persist-credentials: false
fetch-depth: 0- name: Update timestamp
run: |
d=`date '+%Y-%m-%dT%H:%M:%SZ'`
echo $d > LATEST_UPDATE- name: Commit changes
run: |
git config --local user.email ${{ secrets.USER_EMAIL }}
git config --local user.name "Sambi85"
git add . arr[0]="Meow"
arr[1]="Prrrrrrr"
arr[2]="Drrrrreow!!!"
arr[3]="Meow... mmm... Meow!!"
random=$[$RANDOM % ${#arr[@]}]. //<--- Crazy Cat Sounds 🐈 git commit -m "${arr[$random]}" //<-- standard git commit -m
Looks like it’s coming together nicely, all we have next is our push. Will be using “git-push-action” to accomplish this task.
Read more about git-push-action:repo: https://github.com/ad-m/github-push-action 😎
Just like our other GitHub action, will use “name:” and “uses:”. Remember that because this repo is a solo project/sandbox and will be showcasing our auto-commits we have the luxury of using certain git commands. That being said will use “git push — force” to push to our main branch. Will also use our GitHub token we set up in secrets. We also have to push to somewhere, so let’s set that up as well. We will use ‘Github.ref’ to point us there.
- name: Push changes
uses: ad-m/github-push-action@v0.6.0
with:
force: true //<-- git push --force 💪
directory: "." //<-- current directory 📌
github_token: ${{ secrets.AUTO_COMMITS }} //<-- secret 🤫
branch: ${{ github.ref }} //<-- where will push to 🗺
Congrats, you did it! Now let’s light this candle 🔥 . Click on the “Start Commit” button on the top right of your screen. It’s the pretty looking green button.
Nice! You’ll be given a small form. Plug in a name and description for the commit and hit “Commit new File”. Boom you are done.
Conclusion 🎉
Now you know how to make automated commits using Github actions. It’s a great way to take a little pressure off your career path. You’ve learned something useful to boot 🥾 . Why not take this tiny project a little further? Play around with the frequency of the commits or change up the scheduling. In discovery, we learn and grow! Have fun with it, it will only increase your understanding. Happy Coding & enjoy those auto commits 😊
Links
My Auto Commits Repo: https://github.com/Sambi85/auto-commits