Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SERV-1031] Add TestContainers to CI Build #11

Merged
merged 15 commits into from
Apr 11, 2024
25 changes: 14 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is a basic workflow to help you get started with Actions

name: CICD
name: CI Test

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
Expand All @@ -11,28 +11,31 @@ on:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# The "build" workflow
build:
name: Build and Test

# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v4

# Setup Go
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.21.0' # The Go version to download (if necessary) and use.

- name: Build and Run
go-version: '1.22.1' # The Go version to download (if necessary) and use.

- name: Install Dependencies
run: go get -u github.com/testcontainers/[email protected]

- name: Build and Run Service
run: |
go build -o myapp main.go
./myapp &
sleep 5
pkill myapp

go mod tidy
docker build -t service-template .
docker run -d -p 8888:8888 service-template

- name: Test with the Go CLI
run: go test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ go.work

# Add IDE files
.idea/

# Build artifacts
service
15 changes: 8 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ARG GO_VERSION=1.20
ARG GO_VERSION=1.22.1
ARG ALPINE_VERSION=3.19

# Use the official Go image as the base image for the build stage
FROM golang:${GO_VERSION}-alpine AS build
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build

LABEL org.opencontainers.image.source="https://github.com/uclalibrary/service-template"
LABEL org.opencontainers.image.description="A template for UCLA Library services written in Go"
Expand All @@ -13,20 +14,20 @@ WORKDIR /app
COPY . .

# Compile application
RUN go build -o /godocker
RUN go build -o /service

##
## STEP 2 - DEPLOY
##
FROM scratch
FROM alpine:${ALPINE_VERSION}

WORKDIR /

# Copy the executable from the build stage
COPY --from=build /godocker /godocker
COPY --from=build /service /service

# Expose the port on which the application will run
EXPOSE 8080
EXPOSE 8888

# Specify the command to be used when the image is used to start a container
ENTRYPOINT ["/godocker"]
ENTRYPOINT [ "/service" ]
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
# Hello World
This is a simple Hello World go program that utilizes Gin. It is meant to help us, the Services team, learn about building a Go program that uses best practices we will have during development of larger programs
# Service Template

This is a simple GoLang service template that utilizes Echo. It is meant to serve as the foundation for a variety of microservices.

## Building the Project

To run the project run:

`go run main.go`

## Creating a Docker image

To run on Docker first build the Docker image:

`docker build -t hello-world . `
`docker build -t service-template .`

To specify what version of Go you would like to use with the Docker image:

`docker build --build-arg GO_VERSION=[YOUR_VERSION] -t hello-world.`
`docker build --build-arg GO_VERSION=[YOUR_VERSION] -t service-template .`

To run the Docker image:

`docker run -p 8080:8080 hello-world`
`docker run -d -p 8888:8888 service-template`

## Compiling on ACT
At the moment, the GA workflow is simple. Since we are listening for http requests, the workflow will not terminate until explicitly told to do so. The workflow will be updated as the application changes.

We use [ACT](https://github.com/nektos/act) to build the project. Our GitHub Actions' workflow (which is also used locally by ACT) is pretty simple.

To get started, ensure that [ACT is installed](https://nektosact.com/installation/index.html) on your system.

Now that ACT is installed you can see the workflow run locally by running:
Now that ACT is installed, you can see the workflow run locally by running:

`act -j build`

## Contact

`act -j build`
If you have any questions or suggestions, feel free to [open a ticket](https://github.com/UCLALibrary/service-template/issues) on project's GitHub repo.
58 changes: 54 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,73 @@
module github.com/UCLALibrary/hello-world-go.git
module github.com/UCLALibrary/service-template.git

go 1.22.1

require (
github.com/labstack/echo/v4 v4.11.4
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.28.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.13 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v25.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/lufia/plan9stats v0.0.0-20231016141302-07b5767bb0ed // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/shirou/gopsutil/v3 v3.24.1 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tklauser/go-sysconf v0.3.13 // indirect
github.com/tklauser/numcpus v0.7.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.48.0 // indirect
go.opentelemetry.io/otel v1.23.1 // indirect
go.opentelemetry.io/otel/metric v1.23.1 // indirect
go.opentelemetry.io/otel/trace v1.23.1 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/exp v0.0.0-20240222230149-e579c86f598f // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.18.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240205150955-31a09d347014 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240221002015-b0ce06bbee7c // indirect
google.golang.org/grpc v1.62.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading