From 28d812b95fbb49b4a10c55a001869ac537f0c6da Mon Sep 17 00:00:00 2001 From: JohnWalkerx Date: Sun, 26 Jun 2022 21:30:46 +0200 Subject: [PATCH] Adds some examples to build golang projects (#3) I added some examples to - use the CI to build golang projects - build and publish a 'latest' tagged docker container to DockerHub - build and publish a version tagged docker container from a version tag to DockerHub Co-authored-by: JohnWalkerx Reviewed-on: https://codeberg.org/Codeberg-CI/examples/pulls/3 Co-authored-by: JohnWalkerx Co-committed-by: JohnWalkerx --- README.md | 2 ++ golang/Dockerfile | 17 +++++++++++ golang/build-docker.yml | 64 +++++++++++++++++++++++++++++++++++++++++ golang/build.yml | 8 ++++++ 4 files changed, 91 insertions(+) create mode 100644 golang/Dockerfile create mode 100644 golang/build-docker.yml create mode 100644 golang/build.yml diff --git a/README.md b/README.md index baa55fc..16cafcb 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ Pull requests are accepted/welcome. | Link | Language | Build System | Comments | | :-- | :-- | :-- | :-- | | [C/make.yml](C/make.yml) | C | Make | Simple ci for building a Make based C project | +| [golang/build.yml](golang/build.yml) | golang | golang | Simple ci for building and test a Go project | +| [golang/build-docker.yml](golang/build-docker.yml) | golang | golang / kaniko | CI to build golang project and build various docker container and publish them on DockerHub | # More: https://codeberg.org/explore/repos?q=woodpecker-ci&topic=1 diff --git a/golang/Dockerfile b/golang/Dockerfile new file mode 100644 index 0000000..fba62d6 --- /dev/null +++ b/golang/Dockerfile @@ -0,0 +1,17 @@ +# Replace 'APPLICATIONNAME' with the name of your golang project + +FROM golang:1.17 AS certs + +FROM scratch + +# copy certs from golang:1.17 image +COPY --from=certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt + +# Copy prebuild Go binary into container +COPY ./APPLICATIONNAME /APPLICATIONNAME + +# Uncomment and ajust if golang application exposes an port +#EXPOSE 3000 + +# Run application +CMD ["/APPLICATIONNAME"] \ No newline at end of file diff --git a/golang/build-docker.yml b/golang/build-docker.yml new file mode 100644 index 0000000..7c9e396 --- /dev/null +++ b/golang/build-docker.yml @@ -0,0 +1,64 @@ +pipeline: + # Builds your Go application + build: + # Set your preferred Go version: + image: golang:1.17 + environment: + # Needed if you want to cross-compile and package the binary in a container. + - CGO_ENABLED=0 + # Select your target OS: + - GOOS=linux + # Select your target architecture: + - GOARCH=amd64 + commands: + - go get + - go build + + # Runs a dryrun to build the docker container + dryrun-docker: + image: plugins/kaniko + settings: + repo: DOCKERUSERNAME/DOCKERHUBREPO + tags: test + dockerfile: Dockerfile + # Just builds the container without a push to DockerHub. + no_push: true + when: + event: pull_request + + # Builds and publish a docker container to DockerHub from the main-branch + publish-docker-latest: + image: plugins/kaniko + settings: + repo: DOCKERUSERNAME/DOCKERHUBREPO + tags: latest + dockerfile: Dockerfile + username: + # Secret 'docker_username' needs to be set in the CI settings + from_secret: docker_username + password: + # Secret 'docker_password' needs to be set in the CI settings + from_secret: docker_password + when: + # Push new version of tag latest if new push on main-branch + event: push + branch: main + + # Builds and publishes a docker container of a tag + publish-docker-tag: + image: plugins/kaniko + settings: + repo: DOCKERUSERNAME/DOCKERHUBREPO + # Uses the tag from git for the container tag + tags: ${CI_COMMIT_TAG} + dockerfile: Dockerfile + username: + # Secret 'docker_username' needs to be set in the CI settings + from_secret: docker_username + password: + # Secret 'docker_password' needs to be set in the CI settings + from_secret: docker_password + when: + # Push new version when version tag is created + event: tag + tag: v* diff --git a/golang/build.yml b/golang/build.yml new file mode 100644 index 0000000..e52f883 --- /dev/null +++ b/golang/build.yml @@ -0,0 +1,8 @@ +pipeline: + build: + image: golang:1.17 + commands: + - go get + - go build + # Optional: Run go tests + - go test