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

AQIB HAFEEZAQIB HAFEEZ
2 min read

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

  1. 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.

  2. 403 Forbidden While Pushing to GHCR
    I got a 403 error due to incorrect permissions on GITHUB_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

0
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.