๐ Setting Up CI/CD with GitHub Actions to Push a GoLang Docker Image to GHCR


Recently, I worked on a GoLang project where I set up a complete CI/CD pipeline using GitHub Actions to build and push my Docker image to GitHub Container Registry (GHCR). In this blog, Iโll walk you through how I achieved it, along with some troubleshooting tips I learned along the way.
๐ GitHub Repo: nodejs-app-CICD-githubaction
๐ ๏ธ Tech Stack
GoLang
Docker
GitHub Actions
GitHub Container Registry (GHCR)
โ What I Achieved
Wrote a Dockerfile for my GoLang app
Created a GitHub Actions workflow for CI/CD
Successfully built and pushed Docker images to GHCR
Handled authentication with
GITHUB_TOKEN
Troubleshot and fixed real-world CI/CD issues
๐ Project Structure
โโโ main.go
โโโ Dockerfile
โโโ .github
โ โโโ workflows
โ โโโ docker.yml
โโโ README.md
โ๏ธ GitHub Actions Workflow
My GitHub Actions file looks like this (.github/workflows/docker.yml
):
name: Build and Push Docker Image
on:
push:
branches: [ main ]
# workflow_dispatch: # Commented to disable manual trigger
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
run: |
docker build -t ghcr.io/${{ github.repository }}:latest .
docker push ghcr.io/${{ github.repository }}:latest
๐ Troubleshooting I Faced
Incorrect Dockerfile Path
My workflow was failing because the path to the Dockerfile was not correct. Once I updated the correct path, the build worked perfectly.403 Forbidden While Pushing to GHCR
I got a 403 error due to incorrect permissions onGITHUB_TOKEN
. I fixed it by making sure my repository had the correct settings:Settings > Actions > General > Workflow permissions
Set to:
Read and write permissions
๐ฆ GHCR Docker Image
Once everything worked, the image was successfully pushed to:
ghcr.io/your-username/your-repo-name:latest
๐ Lessons Learned
Always double-check paths in GitHub workflows
Permissions really matter when working with tokens
GitHub Actions is powerful and smooth once configured properly
๐ GitHub Repository
Feel free to check out the full code here:
๐ https://github.com/AqibHafeez473/nodejs-app-CICD-githubaction
๐ฌ Final Words
This was a great hands-on experience to strengthen my DevOps and CI/CD concepts. If you're working with GoLang and want to containerize your app with automated deployments, GitHub Actions + GHCR is a great combo to get started.
If you found this helpful, feel free to connect or leave feedback ๐
๐ Tags
#GoLang
#DevOps
#Docker
#GitHubActions
#CI/CD
#GHCR
#Programming
#Hashnode
Subscribe to my newsletter
Read articles from AQIB HAFEEZ directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

AQIB HAFEEZ
AQIB HAFEEZ
DevOps Engineer, Linux, Git, GitHub, GitLab, CI/CD pipelines, Jenkins, Docker, Kubernetes, Ansible & AWS. Practical experience in these DevOps tools enhances my passion for streamlined workflows, automated processes, and holistic development solutions. Proficient in digital and Facebook marketing, aiming to merge technical acumen with marketing finesse.