skip to content

GitHub Action to build and push container to IBM Cloud Registry

GitHub Action to build and push container to IBM Cloud Registry

Overview

Today I will be showing an example GitHub Action that will use Podman to build and push a container image to the IBM Cloud Container Registry. If your container file is not named Dockerfile, or is not in the root directory of your repository, update the containerfiles section of the yaml file.

Prepare Github repository

You will need to add two Action Secrets for this workflow Action to run correctly.

  • IMAGE_NAME: The name of the container image. This will be in the form of namespace/container-image. Ex: ryantiffany/nginx
  • REGISTRY_PASSWORD: An IBM Cloud API Key that is used to authenticate podman with the container registry service.

Create Workflow Directory and Actions File

If the repository you are working with is not already using Github Actions, you will need to create a new workflow directory and actions file:

mkdir -p .github/workflows
touch .github/workflows/build-push-container.yaml

Open the .github/workflows/build-push-container.yaml file in your favorite text editor, paste in the following contents, and then save the file.

name: Use Podman to build and push container image to IBM Cloud Registry

on:
  - push
env:
  GITHUB_SHA: ${{ github.sha }}
  REGISTRY_URL: us.icr.io

jobs:
  build-push-update:
    name: Build image
    runs-on: ubuntu-latest

    steps:
    - name: Clone the repository
      uses: actions/checkout@v2

    - name: Buildah Action
      id: build-image
      uses: redhat-actions/buildah-build@v2
      with:
        image: ${{ secrets.IMAGE_NAME }}
        tags: latest ${{ github.sha }}
        containerfiles: |
          ./Dockerfile

    - name: Log in to the IBM Cloud Container registry
      uses: redhat-actions/podman-login@v1
      with:
        registry: ${{ env.REGISTRY_URL }}
        username: iamapikey
        password: ${{ secrets.REGISTRY_PASSWORD }}

    - name: Push to IBM Cloud Container Repository
      id: push-to-icr
      uses: redhat-actions/push-to-registry@v2
      with:
        image: ${{ steps.build-image.outputs.image }}
        tags: ${{ steps.build-image.outputs.tags }}
        registry: ${{ env.REGISTRY }}

    - name: Print image url
      run: echo "Image pushed to ${{ steps.push-to-icr.outputs.registry-paths }}"

Push Changes to Initiate Container Build and Push

git add .github/workflows/build-push-container.yaml
git commit -m "Add build and push container action"
git push

To track the progress of your Github Action open your browser to https://github.com/YOUR_GITHUB_USER/YOUR_REPO/actions.