Skip to content

Simple Go server containerization using GitHub Actions, Jenkins and AWS CodeBuild.

License

Notifications You must be signed in to change notification settings

devenes/containerization-dummy-go

Repository files navigation

CI/CD with Golang

Go Docker Build And Push

Part 1

  • Since we want to trigger the CI/CD process with the push we will perform within the GitHub ecosystem, we have pushed the source code to our own repo.

  • We used GitHub Actions, the Pipeline as Code tool offered by GitHub, to trigger the process with the push commit.

  • A script was written to perform the Docker build process with the trigger and to give the version number.

FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download

COPY *.go .

RUN go build -o /dummy-app
EXPOSE 8080

CMD ["/dummy-app"]
  • The username and password of the Docker Hub Registry, which is the environment we will push our Docker images to after we build, are defined as secrets in GitHub Repo Settings and used as environment variables in the GitHub Actions pipeline.

  • To trigger our Jenkins pipeline, we add the curl command to the end of our GitHub Actions pipeline and our token that we defined in Jenkins to send a request to the webhook link of the server.

    • Note: Token is defined as environment variable in secrets from GitHub repo settings.

In our Jenkins pipeline, we first stop and delete the running container and clean up the old images. Then we download the Image that we have determined with the current tag number and run it as a container.

We give 80 as the port number in the docker run command so that our Jenkins running on 8080 and our Go application do not conflict with each other.

The output of our operations looks like this:

Our image that we pushed to Docker Hub after we got the Build:

Part 2

Before starting to operate on CodeBuild, we give some permissions to reach the ECR and to examine the post-process logs in detail:

We selected our repo by logging into CodeBuild with GitHub. Then we edit the build spec file with the links we got from the ECR settings:

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t mydockerrepo .
      - docker tag mydockerrepo:latest 32976713052.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push 32976713052.dkr.ecr.us-east-1.amazonaws.com/mydockerrepo:latest

We see that our action steps have been successfully completed:

image

At the end of our Image Build process, we come to the repo we opened on ECR to see if our push to ECR was successful:

image