From aea9d8236ba961fe87c2e8687147200527732f8b Mon Sep 17 00:00:00 2001 From: Prabhav Thali Date: Wed, 12 May 2021 18:06:18 +0530 Subject: [PATCH] feat(CI): Adds Travis CI support Signed-off-by: Prabhav Thali --- .travis.yml | 147 +++++++++++++++++++++++++++++++++++ .travis/build_image.sh | 12 +++ .travis/pr_check.sh | 11 +++ .travis/publish_multiarch.sh | 18 +++++ make-release.sh | 30 ++----- 5 files changed, 195 insertions(+), 23 deletions(-) create mode 100644 .travis.yml create mode 100755 .travis/build_image.sh create mode 100755 .travis/pr_check.sh create mode 100755 .travis/publish_multiarch.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..e86fc6b76 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,147 @@ +language: go + +go: +- 1.12.8 + +os: linux +dist: focal + +git: + depth: false + +services: + - docker + +env: + global: + - TAG=nightly + - RECREATE_TAGS=false + - NO_COMMIT='' + - REGISTRY=quay.io + - ORGANIZATION=eclipse + - IMAGE=che-machine-exec + +install: + - git remote set-url origin https://$GITHUB_TOKEN@github.com/eclipse-che/che-machine-exec.git + - export SHORT_SHA=$(git rev-parse --short HEAD) + - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin + - echo "$QUAY_PASSWORD" | docker login quay.io -u "$QUAY_USERNAME " --password-stdin + +jobs: + include: + - stage: Code Coverage Report + if: type = push + arch: amd64 + install: + - export GO111MODULE="on" + - go get ${gobuild_args} ./... + script: + - | + echo "" > coverage.txt + export CHE_WORKSPACE_ID=test_id; go test -v ./... -coverprofile coverage.txt + bash <(curl -s https://codecov.io/bash) + + - &unit-test + stage: Unit tests and Docker build PR + if: type = pull_request + install: + - export GO111MODULE="on" + - go get ${gobuild_args} ./... + script: bash .travis/pr_check.sh + name: PR check on amd64 + arch: amd64 + - <<: *unit-test + name: PR check on arm64 + arch: arm64 + - <<: *unit-test + name: PR check on ppc64le + arch: ppc64le + - <<: *unit-test + name: PR check on s390x + arch: s390x + + - &nightly-build + stage: Build and push both short SHA tag and nightly tag + if: type = push AND branch = main AND env(TAG) = "nightly" + script: + - bash .travis/build_image.sh + name: Build image on amd64 + arch: amd64 + + - <<: *nightly-build + name: Build image on arm64 + arch: arm64 + + - <<: *nightly-build + name: Build image on ppc64le + arch: ppc64le + + - <<: *nightly-build + name: Build image on s390x + arch: s390x + + - stage: Publish multiarch image with nightly & short_sha tag + if: type = push AND branch = main AND env(TAG) = "nightly" + script: bash .travis/publish_multiarch.sh + + - stage: Check existing tags + if: type = api AND env(TAG) != "nightly" + arch: amd64 + script: + - | + set +e + VERSION=$TAG + EXISTING_TAG=$(git ls-remote --exit-code origin refs/tags/${VERSION}) + if [[ -n ${EXISTING_TAG} ]]; then + if [[ ${RECREATE_TAGS} == "true" ]]; then + echo "[INFO] Removing tag for ${VERSION} version. New tag will be recreated during release." + git push origin :$VERSION + else + echo "[ERROR] Cannot proceed with release - tag ${EXISTING_TAG} already exists." + exit 1 + fi + else + echo "[INFO] No existing tags detected for $VERSION" + fi + + - &release-build + stage: Build images for release version + if: type = api AND env(TAG) != "nightly" + script: + - bash .travis/build_image.sh + name: Build image on amd64 + arch: amd64 + + - <<: *release-build + name: Build image on arm64 + arch: arm64 + + - <<: *release-build + name: Build image on ppc64le + arch: ppc64le + + - <<: *release-build + name: Build image on s390x + arch: s390x + + - stage: Publish multiarch image with release tag + if: type = api AND env(TAG) != "nightly" + script: bash .travis/publish_multiarch.sh + + - stage: Tag release and notify on mattermost + if: type = api AND env(TAG) != "nightly" + script: + - git config --global user.name "Mykhailo Kuznietsov" + - git config --global user.email "mkuznets@redhat.com" + - bash make-release.sh --version $TAG --tag-release $NO_COMMIT + after_success: + - | + set -e + echo "{\"username\":\"che-bot\",\"channel\":\"eclipse-che-releases\",\"text\":\":white_check_mark: Che Machine Exec ${TAG} has been released: https://quay.io/eclipse/che-machine-exec:${TAG}\"}" > mattermost.json + curl -i -X POST -H 'Content-Type: application/json' -d @mattermost.json $MATTERMOST_WEBHOOK_URL + + after_failure: + - | + set -e + echo "{\"username\":\"che-bot\",\"channel\":\"eclipse-che-releases\",\"text\":\":no_entry_sign: Che Machine Exec ${TAG} release has failed: https://github.com/eclipse-che/che-machine-exec/actions/workflows/release.yml\"}" > mattermost.json + curl -i -X POST -H 'Content-Type: application/json' -d @mattermost.json $MATTERMOST_WEBHOOK_URL diff --git a/.travis/build_image.sh b/.travis/build_image.sh new file mode 100755 index 000000000..8a8778acb --- /dev/null +++ b/.travis/build_image.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -e + +# Build images +docker build -f build/dockerfiles/Dockerfile -t "${REGISTRY}/${ORGANIZATION}/${IMAGE}:$TAG-${TRAVIS_CPU_ARCH}" . +docker push "${REGISTRY}/${ORGANIZATION}/${IMAGE}:$TAG-${TRAVIS_CPU_ARCH}" + +# Tag image with short_sha in case of nightly build +if [[ "$TAG" == "nightly" ]]; then + docker tag "${REGISTRY}/${ORGANIZATION}/${IMAGE}:$TAG-${TRAVIS_CPU_ARCH}" "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${SHORT_SHA}-${TRAVIS_CPU_ARCH}" + docker push "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${SHORT_SHA}-${TRAVIS_CPU_ARCH}" +fi diff --git a/.travis/pr_check.sh b/.travis/pr_check.sh new file mode 100755 index 000000000..3d30daee3 --- /dev/null +++ b/.travis/pr_check.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +# Build che-machine-exec binary and execute unit tests +CGO_ENABLED=0 GOOS=linux go build -mod=vendor -a -ldflags '-w -s' -a -installsuffix cgo -o che-machine-exec . +export CHE_WORKSPACE_ID=test_id +go test ./... -test.v + +# Build image with pr-check tag +docker build -f build/dockerfiles/Dockerfile -t "${REGISTRY}/${ORGANIZATION}/${IMAGE}:pr-check-${TRAVIS_CPU_ARCH}" . diff --git a/.travis/publish_multiarch.sh b/.travis/publish_multiarch.sh new file mode 100755 index 000000000..dca0ca5aa --- /dev/null +++ b/.travis/publish_multiarch.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -e + +# Create amend with images built on individual architectures +AMEND="" +AMEND+=" --amend ${REGISTRY}/${ORGANIZATION}/${IMAGE}:$TAG-amd64"; +AMEND+=" --amend ${REGISTRY}/${ORGANIZATION}/${IMAGE}:$TAG-arm64"; +AMEND+=" --amend ${REGISTRY}/${ORGANIZATION}/${IMAGE}:$TAG-ppc64le"; +AMEND+=" --amend ${REGISTRY}/${ORGANIZATION}/${IMAGE}:$TAG-s390x"; + +# Create manifest and push multiarch image +docker manifest create "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${TAG}" $AMEND +docker manifest push "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${TAG}" + +if [[ "$TAG" == "nightly" ]]; then + docker manifest create "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${SHORT_SHA}" $AMEND + docker manifest push "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${SHORT_SHA}" +fi diff --git a/make-release.sh b/make-release.sh index aff070747..78f06e458 100755 --- a/make-release.sh +++ b/make-release.sh @@ -1,20 +1,15 @@ #!/bin/bash # Release process automation script. # Used to create branch/tag, update VERSION files -# and and trigger release by force pushing changes to the release branch +# and trigger release by force pushing changes to the release branch -# set to 1 to actually trigger changes in the release branch -TRIGGER_RELEASE=0 +# set to 1 to actually tag changes +TAG_RELEASE=0 NOCOMMIT=0 -REGISTRY="quay.io" -DOCKERFILE="build/dockerfiles/Dockerfile" -ORGANIZATION="eclipse" -IMAGE="che-machine-exec" - while [[ "$#" -gt 0 ]]; do case $1 in - '-t'|'--trigger-release') TRIGGER_RELEASE=1; shift 0;; + '-t'|'--tag-release') TAG_RELEASE=1; shift 0;; '-v'|'--version') VERSION="$2"; shift 1;; '-n'|'--no-commit') NOCOMMIT=1; shift 0;; esac @@ -23,8 +18,8 @@ done usage () { - echo "Usage: $0 --version [VERSION TO RELEASE] [--trigger-release]" - echo "Example: $0 --version 7.7.0 --trigger-release"; echo + echo "Usage: $0 --version [VERSION TO RELEASE] [--tag-release]" + echo "Example: $0 --version 7.7.0 --tag-release"; echo } if [[ ! ${VERSION} ]]; then @@ -32,14 +27,6 @@ if [[ ! ${VERSION} ]]; then exit 1 fi -releaseMachineExec() { - # docker buildx includes automated push to registry, so build using tag we want published, not just local ${IMAGE} - docker buildx build \ - --tag "${REGISTRY}/${ORGANIZATION}/${IMAGE}:${VERSION}" --push \ - -f ./${DOCKERFILE} . --platform "linux/amd64,linux/ppc64le,linux/arm64" | cat - echo "Pushed ${REGISTRY}/${ORGANIZATION}/${IMAGE}:${VERSION}" -} - # derive branch from version BRANCH=${VERSION%.*}.x @@ -73,10 +60,7 @@ if [[ ${NOCOMMIT} -eq 0 ]]; then git push origin "${BRANCH}" fi -if [[ $TRIGGER_RELEASE -eq 1 ]]; then - # push new branch to release branch to trigger CI build - releaseMachineExec - +if [[ $TAG_RELEASE -eq 1 ]]; then # tag the release git checkout "${BRANCH}" git tag "${VERSION}"