Skip to content

Commit

Permalink
feat: Docker release (PR #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
KEINOS committed Mar 9, 2022
1 parent fd2f094 commit 1a17083
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 36 deletions.
9 changes: 9 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Dev Container for VSCode + Remote-Containers/GitHub Codespaces

Go development container directory for VSCode Remote-Containers and GitHub Codespaces for those who don't have or want to install Go v1.16+ locally.

- VSCode with Remote-Containers extension installed User:
- Press F1 and type `Reopen in Container` to reopen the repository in the container. (Docker is required)
- GitHub Codespaces User:
1. Open the [web site of the repository on GitHub](https://github.com/KEINOS/whereami).
2. Press `.`(dot) and on-browser VSCode should launch.
16 changes: 16 additions & 0 deletions .github/Dockerfile_release
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# =============================================================================
# Docker Container for DockerHub Release
# =============================================================================
# This Dockerfile is used in goreleaser. This will include the built binary and
# creates multiarch Docker image.
FROM golang:alpine AS build

RUN apk add --no-cache \
ca-certificates

FROM scratch

COPY ./whereami /usr/bin/whereami
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

ENTRYPOINT ["/usr/bin/whereami"]
5 changes: 2 additions & 3 deletions .github/Dockerfile → .github/Dockerfile_unit_test
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# =============================================================================
# Test Container for Vaious Go Versions
# =============================================================================

# Default version
ARG VARIANT="1.16-alpine"
# Default version: golang:alpine
ARG VARIANT="alpine"

# -----------------------------------------------------------------------------
# Main Stage
Expand Down
24 changes: 12 additions & 12 deletions .github/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
# version, during it's process the "go.mod" file will be gone temporarily. Thus,
# it will cause failure in the other container becaue of missing "go.mod" file.
#
# Recommended usage:
# docker-compose --file ./.github/docker-compose.yml run tidy && \
# docker-compose --file ./.github/docker-compose.yml run v1_17
#
# NOT recommended to run all tests at once whith "up":
# docker-compose --file ./.github/docker-compose.yml up
# - Recommended usage:
# $ docker-compose --file ./.github/docker-compose.yml run v1_17
# - To update go.mod:
# $ docker-compose --file ./.github/docker-compose.yml run tidy
# - NOT recommended to run all tests at once whith "up":
# $ docker-compose --file ./.github/docker-compose.yml up
version: "3.9"
services:
# Service tidy updates the go.mod to the latest
tidy:
build:
context: ..
dockerfile: ./.github/Dockerfile
dockerfile: ./.github/Dockerfile_unit_test
args:
VARIANT: 1.16-alpine
volumes:
Expand All @@ -29,7 +29,7 @@ services:
v1_14:
build:
context: ..
dockerfile: ./.github/Dockerfile
dockerfile: ./.github/Dockerfile_unit_test
args:
VARIANT: 1.14-alpine
volumes:
Expand All @@ -38,7 +38,7 @@ services:
v1_15:
build:
context: ..
dockerfile: ./.github/Dockerfile
dockerfile: ./.github/Dockerfile_unit_test
args:
VARIANT: 1.15-alpine
volumes:
Expand All @@ -47,7 +47,7 @@ services:
v1_16:
build:
context: ..
dockerfile: ./.github/Dockerfile
dockerfile: ./.github/Dockerfile_unit_test
args:
VARIANT: 1.16-alpine
volumes:
Expand All @@ -56,7 +56,7 @@ services:
v1_17:
build:
context: ..
dockerfile: ./.github/Dockerfile
dockerfile: ./.github/Dockerfile_unit_test
args:
VARIANT: 1.17-alpine
volumes:
Expand All @@ -65,7 +65,7 @@ services:
latest:
build:
context: ..
dockerfile: ./.github/Dockerfile
dockerfile: ./.github/Dockerfile_unit_test
args:
VARIANT: alpine
volumes:
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/codeQL-analysis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# Code vulnerability scanning of Go with CodeQL.
# It will detect the known vulnerability of the code.
name: "CodeQL"

on:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ jobs:
run: |
go mod download
go test -race -v ./...
go run ./cmd/whereami
10 changes: 6 additions & 4 deletions .github/workflows/go-versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unit testing on vaious Go versions, such as Go 1.16 and later.
#
# This workflow caches images built with Docker and docker-compose to speed up its execution.
name: go1.16+

on:
Expand All @@ -23,6 +26,7 @@ jobs:
- name: Create image tag from file hash
uses: KEINOS/gh-action-hash-for-cache@main
id: imagetag
# Udate the hash if Dockerfile is changed or the month has changed.
with:
path: |
./.github/Dockerfile
Expand All @@ -35,7 +39,7 @@ jobs:
path: ${{ env.PATH_CACHE }}
key: ${{ steps.imagetag.outputs.hash }}

- name: Load cached Docker images
- name: Load cached Docker images if any
if: steps.cache.outputs.cache-hit == 'true'
run: |
docker load --input ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}/github_v1_16_1.tar
Expand All @@ -53,11 +57,9 @@ jobs:
- name: Build Docker images if no-cache
if: steps.cache.outputs.cache-hit != 'true'
run: |
mkdir -p ${{ steps.imagetag.outputs.hash }}
: # Build container images
docker-compose --file ./.github/docker-compose.yml build
- name: Save built images if no-cache
- name: Save/export built images to cache dir if no-cache
if: steps.cache.outputs.cache-hit != 'true'
run: |
mkdir -p ${{ env.PATH_CACHE }}/${{ steps.imagetag.outputs.hash }}
Expand Down
26 changes: 25 additions & 1 deletion .github/workflows/release_bin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# =============================================================================
# This workflow will build the binaries and push the archives to the assets in
# the releases page if a release was created. Then pushes the tap config file
# for Homebrew tap. See: ../../.goreleaser.yml
# to update Homebrew tap. See: ../../.goreleaser.yml
name: Release

on:
Expand All @@ -20,11 +20,35 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
with:
# required for the changelog to work correctly
fetch-depth: 0

# Setup
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.17.x'
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

# Login to GitHub Container Registry and DockerHub
- name: Login to GHCR
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# Build and release
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
39 changes: 37 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# =============================================================================
# Configuration for `goreleaser`.
# GoReleaser Configuration to release various binaries
# =============================================================================
# It will:
# - Cross compile for all supported platforms. macOS, Linux and Windows.
# - Releases the artifacts to the assets of releases page in GitHub.
# - Creates Homebrew tap and formula for macOS and Linux system.
# - Creates Docker images and manifest file.
#
# For local-test run:
# $ goreleaser release --snapshot --skip-publish --rm-dist
# $ # *Note: Check the ./bin/ dir after ran.
#
# Make sure to check the documentation as well at:
# https://goreleaser.com/customization/
# =============================================================================
# Download Go modules
before:
hooks:
- go mod download
Expand Down Expand Up @@ -54,7 +61,7 @@ builds:
ldflags:
- -s -w -extldflags '-static' -X 'main.version={{.Version}}' -X 'main.commit={{ .ShortCommit }}'

# macOS universal binaries for both arm64 and amd64
# macOS universal binaries for both amd64(x86_64) and amd64(M1)
universal_binaries:
-
name_template: 'whereami'
Expand Down Expand Up @@ -107,3 +114,31 @@ brews:
# Smoke test to run after install
test: |
system "#{bin}/whereami --version"
# Build Docker image for amd64 and arm64
dockers:
- image_templates:
- "keinos/whereami:{{ .Version }}-amd64"
use: buildx
goarch: amd64
dockerfile: .github/Dockerfile_release
build_flag_templates:
- "--platform=linux/amd64"
- image_templates:
- "keinos/whereami:{{ .Version }}-arm64"
use: buildx
goarch: arm64
dockerfile: .github/Dockerfile_release
build_flag_templates:
- "--platform=linux/arm64"

# Build multiarch manifest file of Docker image
docker_manifests:
- name_template: keinos/whereami:{{ .Version }}
image_templates:
- keinos/whereami:{{ .Version }}-amd64
- keinos/whereami:{{ .Version }}-arm64
- name_template: keinos/whereami:latest
image_templates:
- keinos/whereami:{{ .Version }}-amd64
- keinos/whereami:{{ .Version }}-arm64
46 changes: 46 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# =============================================================================
# Docker Container for Local Build
# =============================================================================
# This Dockerfile will create a light weight container image that only contains
# the `whereami` binary.
# Use this container if you don't have Go installed or if you don't want to
# install the command locally.

# -----------------------------------------------------------------------------
# Build Stage
# -----------------------------------------------------------------------------
FROM golang:alpine AS build

RUN apk add --no-cache \
# alpine-sdk \
# build-base \
ca-certificates

COPY . /workspace

WORKDIR /workspace

ENV CGO_ENABLED 0

RUN ls -lah

RUN \
go build \
# Static linking and shrink size
-ldflags="-s -w -extldflags \"-static\"" \
# Outpath
-o /go/bin/whereami \
# Path to main
./cmd/whereami/main.go \
# Smoke test
&& /go/bin/whereami -h

# -----------------------------------------------------------------------------
# Main Stage
# -----------------------------------------------------------------------------
FROM scratch

COPY --from=build /go/bin/whereami /usr/bin/whereami
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt

ENTRYPOINT ["/usr/bin/whereami"]
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,38 @@ $ whereami
$ whereami -help
Usage of whereami:
-verbose
prints detailed information if any
prints detailed information if any. such as IPv6 and etc.
```

## Install

- Via [Homebrew](https://brew.sh/) for macOS, Linux and Windows WSL2. (Intel, AMD64, ARM64, M1)
- Manual download and install:
- [Latest Releases Page](https://github.com/KEINOS/whereami/releases/latest)
- **macOS** (Darwin=x86_64/M1), **Windows** (x86_64/ARM64), **Linux** (x86_64/ARM64/ARM v5, 6, 7)
- Download the archive of your OS and architecture then extract it. Place the extracted binary in your PATH with executable permission.

- Install via [Homebrew](https://brew.sh/):
- macOS, Linux and Windows WSL2. (x86_64/ARM64, M1)

```bash
brew install KEINOS/apps/whereami
```

- For manual download or other architectures like ARM v5, 6, 7 see:
- [Latest Releases Page](https://github.com/KEINOS/whereami/releases/latest)
- Install via `go install`:
- Go v1.16 or above.

```bash
go install github.com/KEINOS/whereami/cmd/whereami@latest
```

- Run via Docker:
- x86_64 (Intel/AMD) and ARM64/M1 architectures.

```bash
docker pull keinos/whereami
docker run --rm keinos/whereami
```

- Note:
- To avoid a large number of API requests to the service providers, **this application sleeps for one second** after printing the obtained global/public IP address.

Expand All @@ -40,12 +59,14 @@ Usage of whereami:
## Contribute

[![go1.16+](https://img.shields.io/badge/Go-1.16+-blue?logo=go)](https://github.com/KEINOS/whereami/actions/workflows/go-versions.yml "Supported versions")
[![Go Reference](https://pkg.go.dev/badge/github.com/KEINOS/whereami.svg)](https://pkg.go.dev/github.com/KEINOS/whereami/)
[![Go Reference](https://pkg.go.dev/badge/github.com/KEINOS/whereami.svg)](https://pkg.go.dev/github.com/KEINOS/whereami/ "View document")
[![Opened Issues](https://img.shields.io/github/issues/KEINOS/whereami?color=lightblue&logo=github)](https://github.com/KEINOS/whereami/issues "opened issues")
[![PR](https://img.shields.io/github/issues-pr/KEINOS/whereami?color=lightblue&logo=github)](https://github.com/KEINOS/whereami/pulls "Pull Requests")

- [GolangCI Lint](https://golangci-lint.run/) rules: [.golangci-lint.yml](https://github.com/KEINOS/whereami/blob/main/.golangci.yml)
- To run tests in a container:
- `docker-compose --file ./.github/docker-compose.yml run v1_17`
- This will run: `go test -cover ./...`, `golangci-lint run` and `golint ./...`
- This will run: `go test -cover -race ./...`, `golangci-lint run` and `golint ./...`
- Branch to PR: `main`
- It is recommended that [DraftPR](https://github.blog/2019-02-14-introducing-draft-pull-requests/) be done first to avoid duplication of work.

Expand All @@ -65,4 +86,4 @@ We would like to thank the following service providers that this command uses to
- [https://toolpage.org/](https://en.toolpage.org/tool/ip-address)
- [https://whatismyip.com/](https://www.whatismyip.com/)

> **This command requests these providers in random order and returns the first IP address with the same response**. (As soon as 3 of the same IP address are returned, the command stops and returns that IP address.)
> **This command requests these providers in random order and returns the first IP address with the same response**. (As soon as 3 of the same IP address are returned, the command stops and prints that IP address.)
2 changes: 1 addition & 1 deletion cmd/whereami/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func init() {
// Set global/public IP address detection service providers
listProvider = provider.GetAll()
// Define flag options
flag.BoolVar(&isVerbose, "verbose", false, "prints detailed information if any")
flag.BoolVar(&isVerbose, "verbose", false, "prints detailed information if any. such as IPv6 and etc.")
}

func main() {
Expand Down

0 comments on commit 1a17083

Please sign in to comment.