From 81be4f932e5ee71699d4f406184241f06f5c2a82 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Fri, 6 Sep 2024 10:51:12 +0200 Subject: [PATCH 1/3] Init github action --- .github/PULL_REQUEST_TEMPLATE.md | 17 ++++++ .github/workflows/release-go-task.yml | 78 ++++++++++++++++++++++++ .github/workflows/test-go-task.yml | 85 +++++++++++++++++++++++++++ DistTasks.yml | 52 ++++++++++++++++ Taskfile.yml | 47 +++++++++++++++ 5 files changed, 279 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/workflows/release-go-task.yml create mode 100644 .github/workflows/test-go-task.yml create mode 100644 DistTasks.yml create mode 100644 Taskfile.yml diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..be43eec --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,17 @@ +### Motivation + + +### Change description + + +### Additional Notes + + +### Reviewer checklist + +* [ ] PR address a single concern. +* [ ] PR title and description are properly filled. +* [ ] Changes will be merged in `main`. +* [ ] Changes are covered by tests. +* [ ] Logging is meaningful in case of troubleshooting. +* [ ] History is clean, commit messages are meaningful and are well formatted. diff --git a/.github/workflows/release-go-task.yml b/.github/workflows/release-go-task.yml new file mode 100644 index 0000000..34de3a8 --- /dev/null +++ b/.github/workflows/release-go-task.yml @@ -0,0 +1,78 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/release-go-task.md +name: Release + +env: + # As defined by the Taskfile's PROJECT_NAME variable + PROJECT_NAME: aws-s3-integration + # As defined by the Taskfile's DIST_DIR variable + DIST_DIR: dist + ARTIFACT_NAME: dist + +on: + push: + tags: + - "[0-9]+.[0-9]+.[0-9]+*" + +jobs: + create-release-artifacts: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Create changelog + uses: arduino/create-changelog@v1 + with: + tag-regex: '^[0-9]+\.[0-9]+\.[0-9]+.*$' + filter-regex: '^\[(skip|changelog)[ ,-](skip|changelog)\].*' + case-insensitive-regex: true + changelog-file-path: "${{ env.DIST_DIR }}/CHANGELOG.md" + + - name: Install Task + uses: arduino/setup-task@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + version: 3.x + + - name: Build + run: task dist:all + + - name: Upload artifacts + uses: actions/upload-artifact@v2 + with: + if-no-files-found: error + name: ${{ env.ARTIFACT_NAME }} + path: ${{ env.DIST_DIR }} + + create-release: + runs-on: ubuntu-latest + + steps: + - name: Download artifact + uses: actions/download-artifact@v2 + with: + name: ${{ env.ARTIFACT_NAME }} + path: ${{ env.DIST_DIR }} + + - name: Identify Prerelease + # This is a workaround while waiting for create-release action + # to implement auto pre-release based on tag + id: prerelease + run: | + wget -q -P /tmp https://github.com/fsaintjacques/semver-tool/archive/3.0.0.zip + unzip -p /tmp/3.0.0.zip semver-tool-3.0.0/src/semver >/tmp/semver && chmod +x /tmp/semver + if [[ "$(/tmp/semver get prerel "${GITHUB_REF/refs\/tags\//}")" ]]; then echo "IS_PRE=true" >> $GITHUB_OUTPUT; fi + + - name: Create Github Release and upload artifacts + uses: ncipollo/release-action@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + bodyFile: ${{ env.DIST_DIR }}/CHANGELOG.md + draft: false + prerelease: ${{ steps.prerelease.outputs.IS_PRE }} + # NOTE: "Artifact is a directory" warnings are expected and don't indicate a problem + # (all the files we need are in the DIST_DIR root) + artifacts: ${{ env.DIST_DIR }}/* diff --git a/.github/workflows/test-go-task.yml b/.github/workflows/test-go-task.yml new file mode 100644 index 0000000..6c8a059 --- /dev/null +++ b/.github/workflows/test-go-task.yml @@ -0,0 +1,85 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/test-go-task.md +name: Test Go + +env: + GO_VERSION: "1.22" + +on: + create: + push: + paths: + - ".github/workflows/test-go-task.ya?ml" + - "**/go.mod" + - "**/go.sum" + - "Taskfile.ya?ml" + - "**.go" + pull_request: + paths: + - ".github/workflows/test-go-task.ya?ml" + - "**/go.mod" + - "**/go.sum" + - "Taskfile.ya?ml" + - "**.go" + workflow_dispatch: + repository_dispatch: + +jobs: + run-determination: + runs-on: ubuntu-latest + outputs: + result: ${{ steps.determination.outputs.result }} + steps: + - name: Determine if the rest of the workflow should run + id: determination + run: | + RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x" + # The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead. + if [[ \ + "${{ github.event_name }}" != "create" || \ + "${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX \ + ]]; then + # Run the other jobs. + RESULT="true" + else + # There is no need to run the other jobs. + RESULT="false" + fi + + echo "result=$RESULT" >> $GITHUB_OUTPUT + + test: + name: test (${{ matrix.module.path }} - ${{ matrix.operating-system }}) + needs: run-determination + if: needs.run-determination.outputs.result == 'true' + + strategy: + fail-fast: false + + matrix: + operating-system: + - ubuntu-latest + module: + - path: ./ + codecov-flags: unit + + runs-on: ${{ matrix.operating-system }} + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Install Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Install Task + uses: arduino/setup-task@v1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + version: 3.x + + - name: Run tests + env: + GO_MODULE_PATH: ${{ matrix.module.path }} + run: task go:test diff --git a/DistTasks.yml b/DistTasks.yml new file mode 100644 index 0000000..a7f9b44 --- /dev/null +++ b/DistTasks.yml @@ -0,0 +1,52 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/release-go-task/DistTasks.yml +version: "3" + +# This taskfile is ideally meant to be project agnostic and could be dropped in +# on other Go projects with minimal or no changes. +# +# To use it simply add the following lines to your main taskfile: +# includes: +# dist: ./DistTasks.yml +# +# The following variables must be declared in the including taskfile for the +# build process to work correctly: +# * DIST_DIR: the folder that will contain the final binaries and packages +# * PROJECT_NAME: the name of the project, used in package name +# * VERSION: the version of the project, used in package name and checksum file +# * LD_FLAGS: flags used at build time +# * PROVISIONING_BINARIES_FOLDER: provisioning binaries folder. Remember to REMOVE binaries folder as soon as it is removed from the project +# +# The project MUST contain a LICENSE.txt file in the root folder or packaging will fail. + +vars: + CONTAINER: "docker.elastic.co/beats-dev/golang-crossbuild" + GO_VERSION: "1.22" + CHECKSUM_FILE: "{{.VERSION}}-checksums.txt" + +tasks: + all: + desc: Build for distribution for all platforms + cmds: + - task: Linux_64bit + + Linux_64bit: + desc: Builds Linux 64 bit binaries + dir: "{{.DIST_DIR}}" + cmds: + - | + docker run -v `pwd`/..:/home/build -w /home/build \ + -e CGO_ENABLED=0 \ + {{.CONTAINER}}:{{.CONTAINER_TAG}} \ + --build-cmd "{{.BUILD_COMMAND}}" \ + -p "{{.BUILD_PLATFORM}}" + + tar cz -C {{.PLATFORM_DIR}} {{.PROJECT_NAME}} -C ../.. LICENSE.txt -f {{.PACKAGE_NAME}} + sha256sum {{.PACKAGE_NAME}} >> {{.CHECKSUM_FILE}} + + vars: + PLATFORM_DIR: "{{.PROJECT_NAME}}_linux_amd64" + BUILD_COMMAND: "go build -o {{.DIST_DIR}}/{{.PLATFORM_DIR}}/{{.PROJECT_NAME}} {{.LDFLAGS}}" + BUILD_PLATFORM: "linux/amd64" + CONTAINER_TAG: "{{.GO_VERSION}}-main" + PACKAGE_PLATFORM: "Linux_64bit" + PACKAGE_NAME: "{{.PROJECT_NAME}}_{{.VERSION}}_{{.PACKAGE_PLATFORM}}.tar.gz" diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..4f2a27d --- /dev/null +++ b/Taskfile.yml @@ -0,0 +1,47 @@ +# See: https://taskfile.dev/#/usage +version: "3" + +includes: + dist: ./DistTasks.yml + +vars: + # Path of the project's primary Go module: + DEFAULT_GO_MODULE_PATH: ./ + DEFAULT_GO_PACKAGES: + sh: | + echo $(cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"') + PROJECT_NAME: "arduino-cloud-cli" + DIST_DIR: "dist" + # build vars + COMMIT: + sh: echo "$(git log --no-show-signature -n 1 --format=%h)" + TIMESTAMP: + sh: echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" + TIMESTAMP_SHORT: + sh: echo "{{now | date "20060102"}}" + TAG: + sh: echo "$(git tag --points-at=HEAD 2> /dev/null | head -n1)" + VERSION: "{{if .NIGHTLY}}nightly-{{.TIMESTAMP_SHORT}}{{else if .TAG}}{{.TAG}}{{else}}{{.PACKAGE_NAME_PREFIX}}git-snapshot{{end}}" + CONFIGURATION_PACKAGE: github.com/arduino/arduino-cloud-cli/version + +tasks: + # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/go-task/Taskfile.yml + go:build: + desc: Build the Go code + dir: "{{.DEFAULT_GO_MODULE_PATH}}" + cmds: + - GOOS=linux CGO_ENABLED=0 go build -o bootstrap -tags lambda.norpc lambda.go + + # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-go-task/Taskfile.yml + go:test: + desc: Run unit tests + dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}" + cmds: + - | + go test \ + -v \ + -short \ + -run '{{default ".*" .GO_TEST_REGEX}}' \ + {{default "-timeout 10m -coverpkg=./... -covermode=atomic" .GO_TEST_FLAGS}} \ + -coverprofile=coverage_unit.txt \ + {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}} From 7fafdb8f67ce217cab946bd98e8db08fad767b49 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Fri, 6 Sep 2024 11:49:36 +0200 Subject: [PATCH 2/3] fix-up --- .gitignore | 2 ++ DistTasks.yml | 4 ++-- Taskfile.yml | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 09320fc..79581e5 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ vendor/ .tool-versions cover.out + +dist/ diff --git a/DistTasks.yml b/DistTasks.yml index a7f9b44..0f2e1c4 100644 --- a/DistTasks.yml +++ b/DistTasks.yml @@ -20,7 +20,7 @@ version: "3" vars: CONTAINER: "docker.elastic.co/beats-dev/golang-crossbuild" - GO_VERSION: "1.22" + GO_VERSION: "1.22.6" CHECKSUM_FILE: "{{.VERSION}}-checksums.txt" tasks: @@ -45,7 +45,7 @@ tasks: vars: PLATFORM_DIR: "{{.PROJECT_NAME}}_linux_amd64" - BUILD_COMMAND: "go build -o {{.DIST_DIR}}/{{.PLATFORM_DIR}}/{{.PROJECT_NAME}} {{.LDFLAGS}}" + BUILD_COMMAND: "GOOS=linux CGO_ENABLED=0 go build -o {{.DIST_DIR}}/{{.PLATFORM_DIR}}/bootstrap -tags lambda.norpc lambda.go" BUILD_PLATFORM: "linux/amd64" CONTAINER_TAG: "{{.GO_VERSION}}-main" PACKAGE_PLATFORM: "Linux_64bit" diff --git a/Taskfile.yml b/Taskfile.yml index 4f2a27d..7950530 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -10,7 +10,7 @@ vars: DEFAULT_GO_PACKAGES: sh: | echo $(cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"') - PROJECT_NAME: "arduino-cloud-cli" + PROJECT_NAME: "aws-s3-integration" DIST_DIR: "dist" # build vars COMMIT: @@ -22,7 +22,7 @@ vars: TAG: sh: echo "$(git tag --points-at=HEAD 2> /dev/null | head -n1)" VERSION: "{{if .NIGHTLY}}nightly-{{.TIMESTAMP_SHORT}}{{else if .TAG}}{{.TAG}}{{else}}{{.PACKAGE_NAME_PREFIX}}git-snapshot{{end}}" - CONFIGURATION_PACKAGE: github.com/arduino/arduino-cloud-cli/version + CONFIGURATION_PACKAGE: github.com/arduino/aws-s3-integration/version tasks: # Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/go-task/Taskfile.yml From ff575302b5764ad437e9abae30b4a7dc90cb2403 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Fri, 6 Sep 2024 14:32:52 +0200 Subject: [PATCH 3/3] Moved to zip archive to match lambda deployment --- .gitignore | 2 ++ DistTasks.yml | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 79581e5..eb0b33f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ vendor/ cover.out dist/ + +coverage_unit.txt diff --git a/DistTasks.yml b/DistTasks.yml index 0f2e1c4..df7df6b 100644 --- a/DistTasks.yml +++ b/DistTasks.yml @@ -40,7 +40,10 @@ tasks: --build-cmd "{{.BUILD_COMMAND}}" \ -p "{{.BUILD_PLATFORM}}" - tar cz -C {{.PLATFORM_DIR}} {{.PROJECT_NAME}} -C ../.. LICENSE.txt -f {{.PACKAGE_NAME}} + cp {{.PLATFORM_DIR}}/bootstrap bootstrap + cp ../LICENSE LICENSE + zip {{.PACKAGE_NAME}} bootstrap LICENSE + rm bootstrap LICENSE sha256sum {{.PACKAGE_NAME}} >> {{.CHECKSUM_FILE}} vars: @@ -49,4 +52,4 @@ tasks: BUILD_PLATFORM: "linux/amd64" CONTAINER_TAG: "{{.GO_VERSION}}-main" PACKAGE_PLATFORM: "Linux_64bit" - PACKAGE_NAME: "{{.PROJECT_NAME}}_{{.VERSION}}_{{.PACKAGE_PLATFORM}}.tar.gz" + PACKAGE_NAME: "{{.PROJECT_NAME}}_{{.VERSION}}_{{.PACKAGE_PLATFORM}}.zip"