diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 00000000..c52c5374 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,72 @@ +name: build + +on: + push: + branches: + - "*" + pull_request: + branches: + - "*" + +jobs: + build: + runs-on: ubuntu-latest + steps: + - + uses: actions/checkout@master + with: + fetch-depth: 1 + - + name: Set Username/Repo as ENV vars + run: echo "USER_REPO"=$(echo "$GITHUB_REPOSITORY" | awk '{print tolower($1)}' | sed -e "s/:refs//") >> $GITHUB_ENV + - + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Local docker build (non-root image) + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + push: false + platforms: linux/amd64 + target: release + build-args: | + VERSION=latest-dev + GIT_COMMIT=${{ github.sha }} + REPO_URL=https://github.com/${{ env.USER_REPO }} + load: true + tags: openfaas/faas-cli:${{ github.sha }} + - + name: Local docker build (root image) + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + push: false + platforms: linux/amd64 + target: release + build-args: | + VERSION=latest-dev + GIT_COMMIT=${{ github.sha }} + REPO_URL=https://github.com/${{ env.USER_REPO }} + tags: openfaas/faas-cli:${{ github.sha }}-root + - + name: Copy binary to host + run: | + docker create --name faas-cli openfaas/faas-cli:${{ github.sha }} && \ + mkdir -p ./bin && \ + docker cp faas-cli:/usr/bin/faas-cli ./bin && \ + docker rm -f faas-cli + - + name: Build samples + run: ./build_samples.sh + - + name: Test version + run: ./test/version.sh ./bin/faas-cli + - + name: Test the cli and the function behaviour + run: make test-templating diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml new file mode 100644 index 00000000..fc6bbe20 --- /dev/null +++ b/.github/workflows/publish.yaml @@ -0,0 +1,98 @@ +name: publish + +on: + push: + tags: + - '*' + +jobs: + publish: + runs-on: ubuntu-latest + steps: + - + uses: actions/checkout@master + with: + fetch-depth: 1 + - + name: Get tags + id: get_tag + run: echo ::set-output name=TAG::${GITHUB_REF#refs/tags/} + - + name: Set Username/Repo and ImagePrefix as ENV vars + run: | + echo "USER_REPO"=$(echo "$GITHUB_REPOSITORY" | awk '{print tolower($1)}' | sed -e "s/:refs//") >> $GITHUB_ENV && \ + echo "IMAGE_PREFIX"=$(echo "ghcr.io/$GITHUB_REPOSITORY" | awk '{print tolower($1)}' | sed -e "s/:refs//") >> $GITHUB_ENV + - + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Login to Github Container Registry + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.DOCKER_PASSWORD }} + - + name: Build and Push container images (non-root) + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm/v7,linux/arm64 + target: release + build-args: | + VERSION=${{ steps.get_tag.outputs.TAG }} + GIT_COMMIT=${{ github.sha }} + REPO_URL=https://github.com/${{ env.USER_REPO }} + push: true + tags: | + ${{ env.IMAGE_PREFIX }}:${{ github.sha }} + ${{ env.IMAGE_PREFIX }}:${{ steps.get_tag.outputs.TAG }} + ${{ env.IMAGE_PREFIX }}:latest + - + name: Build and Push container images (root) + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm/v7,linux/arm64 + target: root + build-args: | + VERSION=${{ steps.get_tag.outputs.TAG }} + GIT_COMMIT=${{ github.sha }} + REPO_URL=https://github.com/${{ env.USER_REPO }} + push: true + tags: | + ${{ env.IMAGE_PREFIX }}:${{ github.sha }}-root + ${{ env.IMAGE_PREFIX }}:${{ steps.get_tag.outputs.TAG }}-root + ${{ env.IMAGE_PREFIX }}:latest-root + - + name: Build binaries for multiple environments + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile.redist + platforms: linux/amd64 + build-args: | + VERSION=${{ steps.get_tag.outputs.TAG }} + GIT_COMMIT=${{ github.sha }} + load: true + push: false + tags: | + openfaas/faas-cli:${{ steps.get_tag.outputs.TAG }} + - + name: Copy binaries to host + run: ./build_redist.sh ${{ steps.get_tag.outputs.TAG }} + - + name: Create SHA of binaries + run: ./ci/hashgen.sh + - + name: Upload binaries and their SHA to Github Release + uses: alexellis/upload-assets@0.2.2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + asset_paths: '["./bin/faas-cli*"]' diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a4d606a7..00000000 --- a/.travis.yml +++ /dev/null @@ -1,58 +0,0 @@ -sudo: required -language: generic - -services: -- docker - -before_script: -- curl -sSLf https://get.docker.com | sed s/sleep\ 20/sleep\ 0/g | sudo -E sh - -script: - - make build - - make build_samples - - ./test/version.sh ./faas-cli - - make build_redist - -after_success: - - if [ ! -z "$TRAVIS_TAG" ] ; then - if [ -z "$DOCKER_NS" ] ; then - export DOCKER_NS=openfaas; - fi; - - docker tag $DOCKER_NS/faas-cli:latest-dev $DOCKER_NS/faas-cli:$TRAVIS_TAG; - docker tag $DOCKER_NS/faas-cli:latest-dev $DOCKER_NS/faas-cli:latest; - docker tag $DOCKER_NS/faas-cli:latest-dev-root $DOCKER_NS/faas-cli:${TRAVIS_TAG}-root; - docker tag $DOCKER_NS/faas-cli:latest-dev-root $DOCKER_NS/faas-cli:latest-root; - echo $DOCKER_PASSWORD | docker login -u=$DOCKER_USERNAME --password-stdin; - docker push $DOCKER_NS/faas-cli:$TRAVIS_TAG; - docker push $DOCKER_NS/faas-cli:latest; - docker push $DOCKER_NS/faas-cli:${TRAVIS_TAG}-root; - docker push $DOCKER_NS/faas-cli:latest-root; - - fi; - -before_deploy: - - make test-templating - - ./ci/hashgen.sh - -deploy: - provider: releases - api_key: - secure: "XSwuWxddw6Stz/ZtrfQJBovTL7ZyQM8kf7zCtzS6Pl1gatgpa/E/ZflVICgEArGZu2Ipaj8TfOJjQ0cgZlA3MSt+DxNXpZjHJbeids+GFQd+/xuxvyUDI9mCabE2hLM0Drqy4Psro2Rz8S995cIoCmAZM5FpGQ2xgjy6c2cIpeO1F3wnLExTe+TLBBYOP2yEdkLNem12EOnomsCnmG0Cq15nJKq0zOZ1dzIIOhKTY/9poH0s3W3lpbXYYVLGhRJ6GENwX1+HX7yM71AOAYPdXF4AV5p1jVCI74c1tnUGgysYzo9CHIgB4RME6ElJrYzOA9bBBX5mHPmwvTkfUmt2Dp/Oq4ZhqWKfAp4dFjEV2iZpKg4V8+neeiLlgHnTHur/tCxBJLywH+FuoLMghKjs10wT6gAeKa098cEJzXrnRxwRa/jHOIMJLi5BflbfS1QbjHaQMrdHgQCwQlj2ZpBcCM/MWrOgeWT72o6gAfeg4r4Gzj0MHlKr82LCY27QVUeit1iodUL2eODd6DURgt8jfRVOjEgATrV0xewsTp2ENHWccxpqfceGAjyUow6HLhjNvBHwcaUgMjTp2oAUPIDCqVp7oXyOEoHy27hyNEqxHCh0MjPY/HDoW1I+1vhBH47xewbLgNrxeT7Znm+SO6q5Ak0OIWcf/5vfLOOq3BOImno=" - file: - - faas-cli - - faas-cli-darwin - - faas-cli-armhf - - faas-cli.exe - - faas-cli-arm64 - - faas-cli.sha256 - - faas-cli-darwin.sha256 - - faas-cli-armhf.sha256 - - faas-cli.exe.sha256 - - faas-cli-arm64.sha256 - skip_cleanup: true - on: - tags: true - -env: - - GO111MODULE=off diff --git a/Dockerfile b/Dockerfile index 3984fe13..4580d152 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,15 @@ -FROM teamserverless/license-check:0.3.6 as license-check +FROM teamserverless/license-check:0.3.9 as license-check # Build stage -FROM golang:1.13 as builder +FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.13-alpine as builder + +ARG TARGETPLATFORM +ARG BUILDPLATFORM +ARG TARGETOS +ARG TARGETARCH + +ARG GIT_COMMIT +ARG VERSION ENV GO111MODULE=on ENV GOFLAGS=-mod=vendor @@ -9,6 +17,7 @@ ENV CGO_ENABLED=0 WORKDIR /usr/bin/ +RUN apk --no-cache add git COPY --from=license-check /license-check /usr/bin/ WORKDIR /go/src/github.com/openfaas/faas-cli @@ -21,31 +30,39 @@ RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*"))" # ldflags -X injects commit version into binary RUN /usr/bin/license-check -path ./ --verbose=false "Alex Ellis" "OpenFaaS Author(s)" -RUN go test $(go list ./... | grep -v /vendor/ | grep -v /template/|grep -v /build/|grep -v /sample/) -cover +RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ + go test $(go list ./... | grep -v /vendor/ | grep -v /template/|grep -v /build/|grep -v /sample/) -cover -RUN VERSION=$(git describe --all --exact-match `git rev-parse HEAD` | grep tags | sed 's/tags\///') \ - && GIT_COMMIT=$(git rev-list -1 HEAD) \ - && CGO_ENABLED=0 GOOS=linux go build --ldflags "-s -w \ +RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 \ + go build --ldflags "-s -w \ -X github.com/openfaas/faas-cli/version.GitCommit=${GIT_COMMIT} \ -X github.com/openfaas/faas-cli/version.Version=${VERSION} \ - -X github.com/openfaas/faas-cli/commands.Platform=x86_64" \ + -X github.com/openfaas/faas-cli/commands.Platform=${TARGETARCH}" \ -a -installsuffix cgo -o faas-cli # CICD stage -FROM alpine:3.12 as root +FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.12 as root + +ARG REPO_URL + +LABEL org.opencontainers.image.source $REPO_URL RUN apk --no-cache add ca-certificates git WORKDIR /home/app -COPY --from=builder /go/src/github.com/openfaas/faas-cli/faas-cli /usr/bin/ +COPY --from=builder /go/src/github.com/openfaas/faas-cli/faas-cli /usr/bin/ ENV PATH=$PATH:/usr/bin/ ENTRYPOINT [ "faas-cli" ] # Release stage -FROM alpine:3.12 as release +FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine:3.12 as release + +ARG REPO_URL + +LABEL org.opencontainers.image.source $REPO_URL RUN apk --no-cache add ca-certificates git @@ -55,11 +72,11 @@ RUN addgroup -S app \ WORKDIR /home/app -COPY --from=builder /go/src/github.com/openfaas/faas-cli/faas-cli /usr/bin/ +COPY --from=builder /go/src/github.com/openfaas/faas-cli/faas-cli /usr/bin/ RUN chown -R app:app ./ USER app ENV PATH=$PATH:/usr/bin/ -ENTRYPOINT ["faas-cli"] +ENTRYPOINT ["faas-cli"] \ No newline at end of file diff --git a/Dockerfile.redist b/Dockerfile.redist index 1c394001..986b8b50 100644 --- a/Dockerfile.redist +++ b/Dockerfile.redist @@ -3,6 +3,9 @@ FROM teamserverless/license-check:0.3.6 as license-check # Build stage FROM golang:1.13 as builder +ARG GIT_COMMIT +ARG VERSION + ENV GO111MODULE=on ENV GOFLAGS=-mod=vendor ENV CGO_ENABLED=0 diff --git a/README.md b/README.md index 994e3e84..28800cbf 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ ## faas-cli -[![Build Status](https://travis-ci.com/openfaas/faas-cli.svg?branch=master)](https://travis-ci.com/openfaas/faas-cli) +[![Build Status](https://github.com/openfaas/faas-cli/workflows/build/badge.svg?branch=master)](https://github.com/openfaas/faas-cli/actions) [![Go Report Card](https://goreportcard.com/badge/github.com/openfaas/faas-cli)](https://goreportcard.com/report/github.com/openfaas/faas-cli) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![OpenFaaS](https://img.shields.io/badge/openfaas-serverless-blue.svg)](https://www.openfaas.com) diff --git a/build_integration_test.sh b/build_integration_test.sh index 7b685a4b..47f86746 100755 --- a/build_integration_test.sh +++ b/build_integration_test.sh @@ -1,6 +1,6 @@ #!/bin/bash -cli="./faas-cli" +cli="./bin/faas-cli" template="python3" get_package() { diff --git a/build_redist.sh b/build_redist.sh index dcf55fe7..cccfd03a 100755 --- a/build_redist.sh +++ b/build_redist.sh @@ -6,11 +6,11 @@ if [ $1 ] ; then eTAG=$1 fi -docker build --build-arg http_proxy=$http_proxy --build-arg https_proxy=$https_proxy -t openfaas/faas-cli:$eTAG . -f Dockerfile.redist && \ - docker create --name faas-cli openfaas/faas-cli:$eTAG && \ - docker cp faas-cli:/home/app/faas-cli . && \ - docker cp faas-cli:/home/app/faas-cli-darwin . && \ - docker cp faas-cli:/home/app/faas-cli-armhf . && \ - docker cp faas-cli:/home/app/faas-cli-arm64 . && \ - docker cp faas-cli:/home/app/faas-cli.exe . && \ - docker rm -f faas-cli +docker create --name faas-cli openfaas/faas-cli:${eTAG} && \ +mkdir -p ./bin && \ +docker cp faas-cli:/home/app/faas-cli ./bin && \ +docker cp faas-cli:/home/app/faas-cli-darwin ./bin && \ +docker cp faas-cli:/home/app/faas-cli-armhf ./bin && \ +docker cp faas-cli:/home/app/faas-cli-arm64 ./bin && \ +docker cp faas-cli:/home/app/faas-cli.exe ./bin && \ +docker rm -f faas-cli diff --git a/build_samples.sh b/build_samples.sh index 33a79c8e..3f41be81 100755 --- a/build_samples.sh +++ b/build_samples.sh @@ -1,5 +1,5 @@ #!/bin/sh -./faas-cli build # --squash=true +./bin/faas-cli build # --squash=true docker images |head -n 4 diff --git a/ci/hashgen.sh b/ci/hashgen.sh index 25bab9ad..da454eff 100755 --- a/ci/hashgen.sh +++ b/ci/hashgen.sh @@ -1,3 +1,3 @@ #!/bin/sh -for f in faas-cli*; do shasum -a 256 $f > $f.sha256; done \ No newline at end of file +for f in bin/faas-cli*; do shasum -a 256 $f > $f.sha256; done \ No newline at end of file diff --git a/commands/store.go b/commands/store.go index d10d55e8..11f0d5e8 100644 --- a/commands/store.go +++ b/commands/store.go @@ -20,8 +20,15 @@ var ( storeAddress string verbose bool storeDeployFlags DeployFlags - //Platform platform variable updated at build time + //Platform platform variable set at build time Platform string + // if the CLI is built using buildx, then the Platform value needs to be mapped to + // one of the supported values used in the store. + shortPlatform = map[string]string{ + "linux/arm/v6": "armhf", + "linux/amd64": "x86_64", + "linux/arm64": "arm64", + } ) const ( @@ -131,7 +138,12 @@ func getPlatform() string { func getTargetPlatform(inputPlatform string) string { if len(inputPlatform) == 0 { - return getPlatform() + currentPlatform := getPlatform() + target, ok := shortPlatform[currentPlatform] + if ok { + return target + } + return currentPlatform } return inputPlatform }