Tutorial: public ghrc.io container image (github respository) using github actions

Introduction: In the world of software development, sharing code and collaborating with others is crucial. One way to simplify this process is by creating a public ghrc.io container image for your GitHub repository. This allows you to share your code effortlessly with the community, making it easier for others to use and contribute to your project. In this tutorial, we will walk you through the simple steps to create your own public ghrc.io container image for your GitHub repository. Let’s get started!

Step 1: Prepare Your GitHub Repository Before we can create a ghrc.io container image, you need a GitHub repository to work with. If you don’t have one yet, go ahead and create a new repository or use an existing one.

Step 2: Organize Your Repository Ensure that your GitHub repository is well-structured and contains all the necessary files and configurations for your project. This includes a Dockerfile, which defines how your container image should be built.

Step 3: Create a Dockerfile A Dockerfile is a text file that contains instructions for building a Docker container image. You need to create a Dockerfile in the root directory of your GitHub repository if you haven’t already. Here’s a basic example of a Dockerfile:

# Use an official base image
FROM ubuntu:latest

# Set environment variables, install dependencies, and copy your application code

# Install any necessary dependencies
RUN apt-get update && apt-get install -y \
    python3 \

# Install your application dependencies
RUN pip3 install -r requirements.txt

# Expose a port (if your application uses one)

# Define the command to run your application
CMD ["python3", "app.py"]

Customize this Dockerfile to suit your project’s requirements.

Step 4: Commit and Push Changes After creating or updating your Dockerfile, commit and push the changes to your GitHub repository.

git add Dockerfile
git commit -m "Add Dockerfile"
git push

Step 5: Set Up GitHub Actions GitHub Actions allows you to automate workflows in your repository. To set up GitHub Actions:

  1. Go to the root directory of your GitHub repository.
  2. Click on the “Actions” tab at the top of your repository.

Step 6: Create a GitHub Actions Workflow

In GitHub Actions, workflows are defined in YAML files. You’ll need to create a .github/workflows directory in your repository (if it doesn’t exist) and add a YAML file for your workflow. Here’s an example YAML file named build-container.yml for building a container image:

name: Docker Push NSPK2P image


  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}:v01


    runs-on: ubuntu-latest

      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Docker Login
        run: echo "${{ secrets.GH_TOKEN }}" | docker login ${{ env.REGISTRY }} --username cloud-native-everything --password-stdin
      - name: Docker Build
        run: docker build -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} .
      - name: Docker Push
        run: docker push  ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

Make sure to replace IMAGE_NAME with the desired name and version for your container image (Im this case I am using the name of the repo, plus :v01).

Step 7: Configure Secrets

In the workflow file, we’re using a GitHub Personal Access Token (PAT) to authenticate with GitHub Container Registry. To set up the necessary secrets:

  1. Go to your GitHub repository.
  2. Click on “Settings” > “Secrets” > “New repository secret.”
  3. Name the secret GH_TOKEN and provide your PAT as the secret value.

More details over the next section “Create your Personal Access Token”

Step 8: Commit and Push Workflow

Commit the workflow YAML file (e.g., build-container.yml) to your repository, then push it to GitHub:

git add .
git commit -m "Add GitHub Actions workflow for building container image"
git push

Step 9: Run the Workflow

GitHub Actions will automatically run the workflow whenever there’s a push to the specified branch (in this example, the main branch). You can also manually trigger the workflow from the “Actions” tab.

Step 10: Monitor the Build

You can monitor the progress of your container image build in the “Actions” tab of your GitHub repository.

Step 11: Use Your Container Image

Once the workflow completes successfully, your container image will be available in GitHub Container Registry and can be used or deployed as needed.

Step 12: Set your image (package) as public

Directly in the Package settings in your repo you will see this section where you can make your package available for everyone.

Finally, every-time you push your changes to the repo the package will be updated.

Create your Personal Access Token

Creating a GitHub Personal Access Token (PAT) that you can use in your GitHub Actions workflows. This token is used to authenticate and authorize actions within your repository, such as pushing code, creating releases, or managing issues and pull requests.

Here’s how you can create a GitHub Personal Access Token for use in GitHub Actions:

  1. Log in to GitHub:
    • Navigate to GitHub and log in with your account.
  2. Access the Token Settings:
    • Click on your profile picture in the upper right corner of the page.
    • Select “Settings” from the dropdown menu.
    • In the left sidebar, click on “Developer settings”.
    • Then click on “Personal access tokens”.
  1. Generate a New Token:
    • Click on the “Generate new token” button.
    • Give your token a descriptive name under “Note” to remember what the token will be used for.
  1. Select Scopes:
    • Choose the scopes or permissions you want to grant this token. For GitHub Actions, you often need to select scopes like repo, workflow, write:packages, read:packages depending on what actions your workflow will perform.
    • For example, if your actions will only interact with public repositories, you might only need the public_repo access.
    • Be cautious with the scopes you select to follow the principle of least privilege — only grant the permissions you need.
  1. Generate the Token:
    • Once you’ve selected the appropriate scopes, click the “Generate token” button at the bottom of the page.
    • GitHub will create the token.
  2. Copy the Token:
    • Important: Make sure you copy the token now. You won’t be able to see it again once you navigate away from the page.
    • Store it securely, as you would with any password.
  3. Use the Token in GitHub Actions:
    • You can add this token as a secret in your repository’s settings.
    • Go to your repository on GitHub, click on “Settings” then on “Secrets” in the sidebar.
    • Click on “New repository secret”.
    • Name the secret (commonly named GH_TOKEN or something similar).
    • Paste the token value that you’ve copied into the “Value” field.
  1. Reference the Secret in Your Workflow:
    • In your workflow file (typically .github/workflows/main.yml or similar), use the secret as an environment variable or directly in the steps as needed.

By following these steps, you’ll have a token that you can use with GitHub Actions or any other place where you need to authenticate with GitHub programmatically. Remember to keep your tokens secure and to rotate them periodically for security.

Leave a Reply

Blog at WordPress.com.

%d bloggers like this: