image:registry.hub.docker.com/library/golang:1.21# Official Docker image provided by Go. It contains pre-configured environment for building and running Go applications.
script:
-go get -t ./...# Installs dependencies, including those for testing
-go test -race -v ./...# Run tests with race detection enabled
job_build_artifact:
stage:build
rules:# Conditions under which this job should run
-if:$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"# Run this job only for merge request event into main.
when:always
image:registry.hub.docker.com/library/golang:1.21
script:
-go get -t ./...# Installs dependencies
-go build -o ./artifact.bin ./*.go# Builds the Go application and outputs a binary named artifact.bin
artifacts:
paths:# Specifies the paths to the files that are to be saved as artifacts after the job has been completed.
-./artifact.bin
expire_in:1 week
job_build_and_publish_image:
stage:publish
rules:# Conditions under which this job should run
-if:$CI_PIPELINE_SOURCE == "push" && $CI_MERGE_COMMIT_REF_NAME =~ /main/# Run this job only for merge request event into main.
when:always
image:docker:24.0.0# Official Docker image with Docker client installed
services:
-docker:24.0.0-dind# used to access the Docker daemon from within the CI job.
tags:
-docker-privileged
dependencies:
-job_build_artifact
before_script:
-docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"# Log in to the container registry
script:
-echo "Publishing artifact..."
-ls -al# List files to confirm artifact.bin exists