48
loading...
This website collects cookies to deliver better user experience
javad@fedora:~$ mkdir ~/golang-pipeline
javad@fedora:~$ cd ~/golang-pipeline
javad@fedora:~$ touch main.go
javad@fedora:~$ touch main_test.go
package main
import "fmt"
var version = "dev"
func main() {
fmt.Printf("Version: %s\n", version)
fmt.Println(hello())
}
func hello() string {
return "Hello Glang"
}
dev
and the string returned by the hello
function is intentionally wrong so that our tests fail. Later we will see how the GitHub action can be configured to automatically replace the version string at build time.package main
import "testing"
func TestHello(t *testing.T) {
want := "Hello Golang"
got := hello()
if want != got {
t.Fatalf("want %s, got %s\n", want, got)
}
}
javad@fedora:~$ go test
--- FAIL: TestHello (0.00s)
main_test.go:9: want Hello Golang, got Hello Glang
FAIL
exit status 1
FAIL hello 0.003s
javad@fedora:~$ go run main.go
Version: dev
Hello glang
.github/workflows
and add a file called push.yml.javad@fedora:~$ mkdir -p .github/workflows
javad@fedora:~$ touch .github/workflows/push.yml
name: golang-pipeline
on: push
jobs:
test:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags')
steps:
- uses: actions/checkout@v2
- name: Run Unit Tests
run: go test
deploy:
runs-on: ubuntu-latest
needs: test
if: startsWith(github.ref, 'refs/tags')
steps:
- name: Extract Version
id: version_step
run: |
echo "##[set-output name=version;]VERSION=${GITHUB_REF#$"refs/tags/v"}"
echo "##[set-output name=version_tag;]$GITHUB_REPOSITORY:${GITHUB_REF#$"refs/tags/v"}"
echo "##[set-output name=latest_tag;]$GITHUB_REPOSITORY:latest"
- name: Print Version
run: |
echo ${{steps.version_step.outputs.version_tag}}
echo ${{steps.version_step.outputs.latest_tag}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: PrepareReg Names
id: read-docker-image-identifiers
run: |
echo VERSION_TAG=$(echo ${{ steps.version_step.outputs.version_tag }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
echo LASTEST_TAG=$(echo ${{ steps.version_step.outputs.latest_tag }} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
push: true
tags: |
${{env.VERSION_TAG}}
${{env.LASTEST_TAG}}
build-args: |
${{steps.version_step.outputs.version}}
javad@fedora:~$ touch Dockerfile
FROM golang:1.16.4-buster AS builder
ARG VERSION=dev
WORKDIR /go/src/app
COPY main.go .
RUN go build -o main -ldflags=-X=main.version=${VERSION} main.go
FROM debian:buster-slim
COPY --from=builder /go/src/app/main /go/bin/main
ENV PATH="/go/bin:${PATH}"
CMD ["main"]
javad@fedora:~$ docker build -t golang-pipeline:dev .
Sending build context to Docker daemon 114.2kB
Step 1/9 : FROM golang:1.16.4-buster AS builder
1.16.4-buster: Pulling from library/golang
d960726af2be: Pull complete
e8d62473a22d: Pull complete
8962bc0fad55: Pull complete
65d943ee54c1: Pull complete
f2253e6fbefa: Pull complete
6d7fa7c7d5d3: Pull complete
e2e442f7f89f: Pull complete
Digest: sha256:ab32429d40c1b734ed4f036838ac516352182f9414563478fa88a1553c4a4414
Status: Downloaded newer image for golang:1.16.4-buster
---> 96129f3766cf
Step 2/9 : ARG VERSION=dev
---> Running in 819610ae34de
Removing intermediate container 819610ae34de
---> 9eec806043be
Step 3/9 : WORKDIR /go/src/app
---> Running in 46c0cf6a36b4
Removing intermediate container 46c0cf6a36b4
---> 1f1f66dfcc4f
Step 4/9 : COPY main.go .
---> 963a3bcf262b
Step 5/9 : RUN go build -o main -ldflags=-X=main.version=${VERSION} main.go
---> Running in c18447aa254a
Removing intermediate container c18447aa254a
---> 3d5364678624
Step 6/9 : FROM debian:buster-slim
buster-slim: Pulling from library/debian
69692152171a: Pull complete
Digest: sha256:f077cd32bfea6c4fa8ddeea05c53b27e90c7fad097e2011c9f5f11a8668f8db4
Status: Downloaded newer image for debian:buster-slim
---> 80b9e7aadac5
Step 7/9 : COPY --from=builder /go/src/app/main /go/bin/main
---> f96694436dca
Step 8/9 : ENV PATH="/go/bin:${PATH}"
---> Running in 0423a0a1b60a
Removing intermediate container 0423a0a1b60a
---> 8ac59bcbe805
Step 9/9 : CMD ["main"]
---> Running in 1491bcf193c6
Removing intermediate container 1491bcf193c6
---> 190ba9407211
Successfully built 190ba9407211
Successfully tagged golang-pipeline:dev
javad@fedora:~$ docker run --rm golang-pipeline:dev
Version: dev
Hello glang
--build-arg VERSION=1.0.0
while building the image. For example:javad@fedora:~$ docker build -t golang-pipeline:1.0.0 . --build-arg VERSION=1.0.0
Sending build context to Docker daemon 114.2kB
Step 1/9 : FROM golang:1.16.4-buster AS builder
---> 96129f3766cf
Step 2/9 : ARG VERSION=dev
---> Using cache
---> 9eec806043be
Step 3/9 : WORKDIR /go/src/app
---> Using cache
---> 1f1f66dfcc4f
Step 4/9 : COPY main.go .
---> Using cache
---> 963a3bcf262b
Step 5/9 : RUN go build -o main -ldflags=-X=main.version=${VERSION} main.go
---> Running in 352fb8de7969
Removing intermediate container 352fb8de7969
---> 0950776088fa
Step 6/9 : FROM debian:buster-slim
---> 80b9e7aadac5
Step 7/9 : COPY --from=builder /go/src/app/main /go/bin/main
---> 91938ce4f326
Step 8/9 : ENV PATH="/go/bin:${PATH}"
---> Running in 44541fe9de77
Removing intermediate container 44541fe9de77
---> c8924b6b45b3
Step 9/9 : CMD ["main"]
---> Running in 424b93b94419
Removing intermediate container 424b93b94419
---> 9da417e7399b
Successfully built 9da417e7399b
Successfully tagged golang-pipeline:1.0.0
javad@fedora:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
golang-pipeline 1.0.0 9da417e7399b 22 seconds ago 71.2MB
golang-pipeline dev 190ba9407211 3 minutes ago 71.2MB
golang 1.16.4-buster 96129f3766cf 2 weeks ago 862MB
debian buster-slim 80b9e7aadac5 2 weeks ago 69.2MB
-ldflags
to modify the version variable at compile time. The --build-arg
assigns the version to VERSION and is used in the following line:RUN go build -o main -ldflags=-X=main.version=${VERSION} main.go
javad@fedora:~$ git init
javad@fedora:~$ git add .
javad@fedora:~$ git commit -m "first commit"
javad@fedora:~$ git remote add origin [email protected]:USERNAME/golang-pipeline
javad@fedora:~$ git branch -M main
javad@fedora:~$ git push -u origin main
hello
function so that it returns Hello Golang
.javad@fedora:~$ go test
PASS
ok hello 0.003s
javad@fedora:~$ git add .
javad@fedora:~$ git commit - "Fix hello function return value"
javad@fedora:~$ git push
javad@fedora:~$ git tag v1.0.0
javad@fedora:~$ git push --tags