Skip to content

Commit

Permalink
ci: add build GitHub action
Browse files Browse the repository at this point in the history
Signed-off-by: PoAn Yang <[email protected]>
  • Loading branch information
FrankYang0529 committed Apr 3, 2024
1 parent a108f98 commit ee904c2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 32 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: build
on:
push:
branches:
- master
- v*
tags:
- v*
pull_request:
jobs:
build:
name: Build images
runs-on: ubuntu-latest
container:
image: rancher/dapper:v0.6.0
steps:
# Git is not in Dapper container image. Add it manually for dirty check.
- name: Add Git
run: apk add -U git
- name: Checkout code
uses: actions/checkout@v4
- name: Declare branch
run: |
echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> "$GITHUB_ENV"
# For multi-platform support
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

# Build binaries
- name: Run dapper ci
run: dapper ci
- name: Copy bin folder to package
run: |
cp -r ./bin/* ./package/
- name: Login to Docker Hub
if: ${{ startsWith(github.ref, 'refs/heads/') || startsWith(github.ref, 'refs/tags/') }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

# rancher/terraform-provider-harvester image
- name: docker-publish
if: ${{ startsWith(github.ref, 'refs/heads/') }}
uses: docker/build-push-action@v5
with:
context: package/
push: true
platforms: linux/amd64,linux/arm64
tags: rancher/terraform-provider-harvester:${{ env.branch }}-head
file: package/Dockerfile
- name: docker-publish-with-tag
if: ${{ startsWith(github.ref, 'refs/tags/') }}
uses: docker/build-push-action@v5
with:
context: package/
push: true
platforms: linux/amd64,linux/arm64
tags: rancher/terraform-provider-harvester:${{ github.ref_name }}
file: package/Dockerfile
build-args: |
VERSION=${{ github.ref_name }}
11 changes: 7 additions & 4 deletions Dockerfile.dapper
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ RUN zypper -n rm container-suseconnect && \
RUN GO111MODULE=on go install golang.org/x/tools/cmd/[email protected]

## install golangci-lint
RUN if [ "${ARCH}" = "amd64" ]; then \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.57.1; \
fi
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.57.1

ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS
# The docker version in dapper is too old to have buildx. Install it manually.
RUN wget https://github.com/docker/buildx/releases/download/v0.13.1/buildx-v0.13.1.linux-${ARCH} && \
chmod +x buildx-v0.13.1.linux-${ARCH} && \
mv buildx-v0.13.1.linux-${ARCH} /usr/local/bin/buildx

ENV DAPPER_ENV REPO TAG DRONE_TAG
ENV DAPPER_SOURCE /go/src/github.com/harvester/terraform-provider-harvester
ENV DAPPER_OUTPUT ./bin ./dist
ENV DAPPER_DOCKER_SOCKET true
Expand Down
14 changes: 10 additions & 4 deletions package/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
FROM registry.suse.com/bci/bci-base:15.4
FROM registry.suse.com/bci/bci-base:15.5

ARG TARGETPLATFORM
RUN if [ "$TARGETPLATFORM" != "linux/amd64" ] && [ "$TARGETPLATFORM" != "linux/arm64" ]; then \
echo "Error: Unsupported TARGETPLATFORM: $TARGETPLATFORM" && \
exit 1; \
fi

# hadolint ignore=DL3037
RUN zypper -n rm container-suseconnect && \
zypper -n install unzip curl vim && \
zypper -n clean -a && rm -rf /tmp/* /var/tmp/* /usr/share/doc/packages/*

ARG ARCH=amd64
ENV ARCH ${TARGETPLATFORM#linux/}
ENV KERNEL_ARCH linux_${ARCH}
# install terraform
ENV TERRAFORM_VERSION 1.4.6
Expand All @@ -20,7 +26,7 @@ ARG PROVIDER_VERSION=0.0.0-dev
ENV PROVIDERS_DIR /root/.terraform.d/plugins/terraform.local/local/harvester
ENV PROVIDER_DIR ${PROVIDERS_DIR}/${PROVIDER_VERSION}/${KERNEL_ARCH}
RUN mkdir -p ${PROVIDER_DIR}
COPY ./terraform-provider-harvester ${PROVIDER_DIR}/terraform-provider-harvester_v${PROVIDER_VERSION}
COPY ./terraform-provider-harvester-${ARCH} ${PROVIDER_DIR}/terraform-provider-harvester_v${PROVIDER_VERSION}
RUN mkdir -p /data
COPY ./provider.tf /data/provider.tf
WORKDIR /data
WORKDIR /data
3 changes: 2 additions & 1 deletion scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ cd $(dirname $0)/..

mkdir -p bin
[ "$(uname)" != "Darwin" ] && LINKFLAGS="-extldflags -static -s"
CGO_ENABLED=0 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/terraform-provider-harvester
CGO_ENABLED=0 GOARCH=amd64 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/terraform-provider-harvester-amd64
CGO_ENABLED=0 GOARCH=arm64 go build -ldflags "-X main.VERSION=$VERSION $LINKFLAGS" -o bin/terraform-provider-harvester-arm64
32 changes: 22 additions & 10 deletions scripts/package
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,37 @@ else
PROVIDER_VERSION=${VERSION#"v"}
fi

TAG=${TAG:-${VERSION}${SUFFIX}}
TAG=${TAG:-${VERSION}}

if echo $TAG | grep -q dirty; then
TAG=dev
fi

mkdir -p dist/artifacts
cp bin/terraform-provider-harvester dist/artifacts/terraform-provider-harvester${SUFFIX}
cp bin/terraform-provider-harvester-* dist/artifacts/

cd ${PACKAGE_DIR}

REPO=${REPO:-rancher}
IMAGE=${REPO}/terraform-provider-harvester:${TAG}
DOCKERFILE=Dockerfile
if [ -e ${DOCKERFILE}.${ARCH} ]; then
DOCKERFILE=${DOCKERFILE}.${ARCH}
fi

rm -rf ./terraform-provider-harvester
cp ../bin/terraform-provider-harvester .

docker build --build-arg ARCH=${ARCH} --build-arg PROVIDER_VERSION=${PROVIDER_VERSION} -f ${DOCKERFILE} -t ${IMAGE} .
echo Built ${IMAGE}
rm -rf ./terraform-provider-harvester-*
cp ../bin/terraform-provider-harvester-* .

docker run --privileged --rm tonistiigi/binfmt --install all
buildx create --platform linux/arm64,linux/amd64 --use
buildx ls

# In old docker version, it doesn't support multiple values in --platform with --load.
# So use two command to build different platform image.
# Ref: https://github.com/docker/buildx/issues/59#issuecomment-616061869
buildx build --load \
--build-arg PROVIDER_VERSION=${PROVIDER_VERSION} \
--platform linux/amd64 \
-f ${DOCKERFILE} -t ${IMAGE} .
buildx build --load \
--build-arg PROVIDER_VERSION=${PROVIDER_VERSION} \
--platform linux/arm64 \
-f ${DOCKERFILE} -t ${IMAGE} .
echo Built ${IMAGE}
13 changes: 0 additions & 13 deletions scripts/version
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,3 @@ if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then
else
VERSION="${COMMIT}${DIRTY}"
fi

if [ -z "$ARCH" ]; then
ARCH=$(go env GOHOSTARCH)
fi

SUFFIX="-${ARCH}"

TAG=${TAG:-${VERSION}${SUFFIX}}
REPO=${REPO:-rancher}

if echo $TAG | grep -q dirty; then
TAG=dev
fi

0 comments on commit ee904c2

Please sign in to comment.