Skip to content

Commit

Permalink
feat: setup-binary action (#170)
Browse files Browse the repository at this point in the history
* feat: setup-binary action

* pr comments

* pr comments

* add go-getter
  • Loading branch information
verbanicm authored Jul 26, 2023
1 parent df1e071 commit 2c9c188
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 0 deletions.
152 changes: 152 additions & 0 deletions .github/actions/setup-binary/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'Setup Binary'

description: |-
Use this action to install and cache a binary for use within a GitHub workflow. This action wraps the functionality of go-getter to provide advanced donwload and checksum capabilities.
inputs:
download_url:
description: 'The download URL for the binary. This must a valid go-getter URL. See https://github.com/hashicorp/go-getter#url-format'
required: true
install_path:
description: 'The path on disk in which to extract the binary, this cannot be equal to the current directory. Set `add_to_path` to true to add this to the $PATH.'
required: true
checksum:
description: 'The checksum for the downloaded artifact. See https://github.com/hashicorp/go-getter#checksumming'
required: false
cache_key:
description: 'The cache key to use when caching the downloaded binary. If this value is empty, caching will be disabled.'
required: false
add_to_path:
description: 'Add the install_path to the $PATH variable.'
default: 'false'
required: false

runs:
using: 'composite'
steps:
- name: 'Validate inputs'
shell: 'bash'
env:
DOWNLOAD_URL: '${{ inputs.download_url }}'
INSTALL_PATH: '${{ inputs.install_path }}'
run: |
# Validate inputs
errors=false
if [[ -z "${DOWNLOAD_URL}" ]]; then
errors=true
echo "::error ::download_url is a required input"
fi
if [[ -z "${INSTALL_PATH}" ]]; then
errors=true
echo "::error ::install_path is a required input"
elif [[ "${INSTALL_PATH}" == "." || "${INSTALL_PATH}" == "${GITHUB_WORKSPACE}" ]]; then
errors=true
echo "::error ::install_path is a required input"
fi
if [ "$errors" = true ]; then
exit 1
fi
- name: 'Restore cache binary'
id: 'cache-binary'
if: |
inputs.cache_key != ''
uses: 'actions/cache/restore@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8' # ratchet:actions/cache/restore@v3
with:
path: '${{ inputs.install_path }}'
key: 'setup-binary-${{ inputs.cache_key }}'

- name: 'Install hashicorp/go-getter'
id: 'go-getter'
if: |
steps.cache-guardian.outputs.cache-hit != 'true'
shell: 'bash'
env:
GOGETTER_VERSION: '1.7.1'
run: |-
# Install hashicorp/go-getter
if [[ "${RUNNER_OS}" == "Linux" ]]
then
TEMP_DIR="${RUNNER_TEMP}/go-getter"
RELEASE_URL="https://github.com/hashicorp/go-getter/releases/download/v${GOGETTER_VERSION}/go-getter_${GOGETTER_VERSION}_linux_amd64.zip"
elif [[ "${RUNNER_OS}" == "macOS" ]]
then
TEMP_DIR="${RUNNER_TEMP}/go-getter"
RELEASE_URL="https://github.com/hashicorp/go-getter/releases/download/v${GOGETTER_VERSION}/go-getter_${GOGETTER_VERSION}_darwin_amd64.zip"
elif [[ "${RUNNER_OS}" == "Windows" ]]
then
TEMP_DIR="${RUNNER_TEMP}\go-getter"
RELEASE_URL="https://github.com/hashicorp/go-getter/releases/download/v${GOGETTER_VERSION}/go-getter_${GOGETTER_VERSION}_windows_amd64.zip"
else
echo "::error ::Unsupported operating system ${RUNNER_OS}"
exit 1
fi
if [[ -f "${TEMP_DIR}/go-getter" ]]; then
echo "Skipping, go-getter is already downloaded"
else
echo "Downloading hashicorp/go-getter from ${RELEASE_URL}"
mkdir -p "${TEMP_DIR}" && cd "${TEMP_DIR}"
curl -sfL "${RELEASE_URL}" -o go-getter.zip
unzip go-getter.zip
fi
echo "path=${TEMP_DIR}" >> $GITHUB_OUTPUT
- name: 'Download binary'
if: |
steps.cache-binary.outputs.cache-hit != 'true'
shell: 'bash'
env:
GOGETTER_PATH: '${{ steps.go-getter.outputs.path }}'
INSTALL_PATH: '${{ inputs.install_path }}'
DOWNLOAD_URL: '${{ inputs.download_url }}'
CHECKSUM: '${{ inputs.checksum }}'
run: |-
# Download binary
if [[ ! -z "${CHECKSUM}" ]]; then
SEPARATOR="?"
if [[ "" == *"?"* ]]; then
SEPARATOR="&"
fi
DOWNLOAD_URL="${DOWNLOAD_URL}${SEPARATOR}checksum=${CHECKSUM}"
fi
"${GOGETTER_PATH}/go-getter" "${DOWNLOAD_URL}" "${INSTALL_PATH}"
- name: 'Save cache binary'
if: |
inputs.cache_key != '' && steps.cache-binary.outputs.cache-hit != 'true'
uses: 'actions/cache/save@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8' # ratchet:actions/cache/save@v3
with:
path: '${{ inputs.install_path }}'
key: 'setup-binary-${{ inputs.cache_key }}'

- name: 'Add binary to path'
if: |
contains(fromJSON('["true", "True", "TRUE", "1", "T", "t"]'), inputs.add_to_path)
shell: 'bash'
env:
INSTALL_PATH: '${{ inputs.install_path }}'
run: |-
# Add binary to path
echo "Adding ${INSTALL_PATH} to PATH"
echo "${INSTALL_PATH}" >> $GITHUB_PATH
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'ci'

on:
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/go-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'go-lint'

on:
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'go-test'

on:
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/java-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'java-lint'

on:
Expand Down
77 changes: 77 additions & 0 deletions .github/workflows/setup-binary-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'setup-binary-test'

on:
pull_request:
paths:
- '.github/actions/setup-binary/**'
jobs:
setup-binary-test:
strategy:
fail-fast: false
matrix:
include:
- os: 'ubuntu-latest'
name: 'linux'
ext: 'tar.gz'
checksum: '1b0ac92898af5a853dc6f8e4432e9241d6efb22eda48253424236e2e5bc6ccd0b5d18d8c5a46ea096a27bba0027c359396736b83b729b6b51852f71eb1a17702'
- os: 'windows-latest'
name: 'windows'
ext: 'zip'
checksum: 'e64c57fb786d1fc5949542e21903dc4e15ab083a7d0b533a1cb115b689a4f72e5d76af50848c937f103264130913fb974dec9afea7f311897d7f776f6423268a'
- os: 'macos-latest'
name: 'darwin'
ext: 'tar.gz'
checksum: '5ee5467091c7058c018cd83fbae8295b18c710135a8bb197eda6705b0562b9849df8519e82e09d79895a7bd7fadd60f3cad01f1509a8de2bcd8638f9b057a476'
runs-on: '${{ matrix.os }}'
steps:
- name: 'Checkout'
uses: 'actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9' # ratchet:actions/checkout@v3

- name: 'Path'
id: 'path'
shell: 'bash'
run: |
echo "path=${HOME}/.abc" >> $GITHUB_OUTPUT
- name: 'Setup binary (save cache)'
uses: './.github/actions/setup-binary' # ratchet:exclude
with:
install_path: '${{ steps.path.outputs.path }}'
download_url: 'https://github.com/abcxyz/abc/releases/download/v0.0.1-alpha4/abc_0.0.1-alpha4_${{ matrix.name }}_amd64.${{ matrix.ext }}'
checksum: '${{ matrix.checksum }}'
cache_key: '${{ runner.os }}_${{ runner.arch }}_abc_${{ github.sha }}'

- name: 'Setup binary (restore cache)'
uses: './.github/actions/setup-binary' # ratchet:exclude
with:
install_path: '${{ steps.path.outputs.path }}'
download_url: 'https://github.com/abcxyz/abc/releases/download/v0.0.1-alpha4/abc_0.0.1-alpha4_${{ matrix.name }}_amd64.${{ matrix.ext }}'
checksum: '${{ matrix.checksum }}'
cache_key: '${{ runner.os }}_${{ runner.arch }}_abc_${{ github.sha }}'
add_to_path: true

- name: 'Test'
shell: 'bash'
run: |
abc -version
GOT=$(abc -version 2>&1) # abcxyz/pkg/cli writes help to stderr
WANT="abc 0.0.1-alpha4"
if [[ "${GOT}" != "${WANT}"* ]]; then
echo "::error ::Expected ${GOT} to contain ${WANT}"
exit 1
fi
14 changes: 14 additions & 0 deletions .github/workflows/terraform-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'terraform-lint'

on:
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/want-lgtm-all.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'want-lgtm-all'

on:
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/yaml-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'yaml-lint'

on:
Expand Down
14 changes: 14 additions & 0 deletions .yamllint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

rules:
anchors:
forbid-duplicated-anchors: true
Expand Down

0 comments on commit 2c9c188

Please sign in to comment.