This document outlines the steps required for deploying a Continuous Integration/Continuous Delivery (CI/CD) setup using the docker/build-push-action. If a CI/CD pipeline has not been set up in the project, next steps will be outlined accordingly.

Prerequisites

Ensure you have the following files properly configured in your project:

  1. docker-compose.yml
  2. Dockerfile

Existing CI/CD Setup

If the CI/CD is already set up, the deployment process can be configured as follows.

Step 1: Define Your Dockerfile

The Dockerfile should be structured as follows:

# syntax=docker/dockerfile:1
FROM alpine
RUN echo "Hello world!"

This is a simple example where an Alpine base image is used, and a command is executed to print a message.

Step 2: Configure your docker-compose.yml

Ensure your docker-compose.yml defines the necessary services. Here’s a minimal setup for Nexus, a repository manager:

services:
  nexus:
    image: sonatype/nexus3:${NEXUS_VERSION:-latest}
    volumes:
      - "./data:/nexus-data"
    ports:
      - "8081:8081"
      - "8082:8082"

This file specifies the Nexus service, creating a persistent volume for its data storage and exposing ports 8081 and 8082.

Step 3: Set Up GitHub Actions Workflow

Create a GitHub Actions workflow in .github/workflows/docker-build-push.yml to automate the build and push process:

name: CI/CD

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - name: Check out code
        uses: actions/checkout@v2
      
      - name: Build and push Docker image
        uses: docker/build-push-action@v3
        with:
          context: .
          push: true
          tags: my-docker-image:latest

This workflow triggers on pushes to the main branch, checks out the code, and uses the Docker Build and Push Action to build the Docker image and push it to the specified registry.

No CI/CD Setup Yet

If there is currently no CI/CD setup in the project, the following steps can be adopted:

Step 1: Create a Dockerfile

If not present, create a Dockerfile. Use the template given prior:

# syntax=docker/dockerfile:1
FROM alpine
RUN echo "Hello world!"

Step 2: Create docker-compose.yml

If this file doesn’t exist, create it as shown:

services:
  nexus:
    image: sonatype/nexus3:${NEXUS_VERSION:-latest}
    volumes:
      - "./data:/nexus-data"
    ports:
      - "8081:8081"
      - "8082:8082"

Step 3: Set Up GitHub Actions Workflow

Create a GitHub Actions workflow file in .github/workflows/docker-build-push.yml:

name: CI/CD

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - name: Check out code
        uses: actions/checkout@v2
      
      - name: Build and push Docker image
        uses: docker/build-push-action@v3
        with:
          context: .
          push: true
          tags: my-docker-image:latest

Golang Module Details

If your project uses Golang, ensure your module name is included properly in your code:

module "github.com/docker/build-push-action/test/go"

When building the Go code, avoid using -mod=vendor, as this might conflict with the expected behavior of Go modules in the dependency management.

Conclusion

This outlines the methods for configuring a CI/CD pipeline for projects utilizing Docker and GitHub Actions. Follow these instructions based on the current status of your project to ensure efficient deployment and management of your dockerized applications.