Skip to content

Update all GHA Actions from Node16->Node20 (#255) #705

Update all GHA Actions from Node16->Node20 (#255)

Update all GHA Actions from Node16->Node20 (#255) #705

Workflow file for this run

name: build
on:
workflow_dispatch:
push:
env:
PKG_NAME: "consul-ecs"
jobs:
get-go-version:
name: "Determine Go toolchain version"
runs-on: ubuntu-latest
outputs:
go-version: ${{ steps.get-go-version.outputs.go-version }}
steps:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
- name: Determine Go version
id: get-go-version
# We use .go-version as our source of truth for current Go
# version, because "goenv" can react to it automatically.
run: |
echo "Building with Go $(cat .go-version)"
echo "go-version=$(cat .go-version)" >> $GITHUB_OUTPUT
get-product-version:
runs-on: ubuntu-latest
outputs:
product-version: ${{ steps.get-product-version.outputs.product-version }}
ldflags: ${{ steps.get-product-version.outputs.ldflags }}
git-short-sha: ${{ steps.get-product-version.outputs.git-short-sha }}
steps:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
- name: get product version
id: get-product-version
run: |
make version
echo "product-version=$(make version)" >> $GITHUB_OUTPUT
echo "git-short-sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "ldflags=-s -w -X github.com/hashicorp/consul-ecs/version.GitCommit=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
generate-metadata-file:
needs: get-product-version
runs-on: ubuntu-latest
outputs:
filepath: ${{ steps.generate-metadata-file.outputs.filepath }}
steps:
- name: "Checkout directory"
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
- name: Generate metadata file
id: generate-metadata-file
uses: hashicorp/actions-generate-metadata@v1
with:
version: ${{ needs.get-product-version.outputs.product-version }}
product: ${{ env.PKG_NAME }}
repositoryOwner: "hashicorp"
- uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
with:
name: metadata.json
path: ${{ steps.generate-metadata-file.outputs.filepath }}
build-linux:
needs:
- get-go-version
- get-product-version
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux]
goarch: ["arm", "arm64", "386", "amd64"]
fail-fast: true
name: Go ${{ needs.get-go-version.outputs.go-version }} ${{ matrix.goos }} ${{ matrix.goarch }} build
steps:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
- name: Setup go
uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
with:
go-version: ${{ needs.get-go-version.outputs.go-version }}
- name: Build
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
LDFLAGS: ${{ needs.get-product-version.outputs.ldflags }}
CGO_ENABLED: "0"
run: |
go env
mkdir dist out
go build -ldflags="$LDFLAGS" -o dist/ .
zip -r -j out/${{ env.PKG_NAME }}_${{ needs.get-product-version.outputs.product-version }}_${{ matrix.goos }}_${{ matrix.goarch }}.zip dist/
if [ "${{ matrix.goarch }}" = "amd64" ]; then
bin=$(find dist -name consul-ecs)
$bin version
fi
- uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
with:
name: ${{ env.PKG_NAME }}_${{ needs.get-product-version.outputs.product-version }}_${{ matrix.goos }}_${{ matrix.goarch }}.zip
path: out/${{ env.PKG_NAME }}_${{ needs.get-product-version.outputs.product-version }}_${{ matrix.goos }}_${{ matrix.goarch }}.zip
build-docker-default:
name: Docker ${{ matrix.arch }} default release build
needs:
- get-product-version
- build-linux
runs-on: ubuntu-latest
strategy:
matrix:
arch: ["arm", "arm64", "386", "amd64"]
env:
repo: ${{ github.event.repository.name }}
version: ${{ needs.get-product-version.outputs.product-version }}
steps:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
- name: Docker Build (Action)
uses: hashicorp/actions-docker-build@v2
with:
# Add smoke test here. Below is a sample smoke test that runs the built image
# and validates the version.
smoke_test: |
TEST_VERSION="$(docker run "${IMAGE_NAME}" version | awk '{print $2}')"
echo "TEST_VERSION=${TEST_VERSION}"
echo "version=${version}"
if [ "${TEST_VERSION}" != "v${version}" ]; then
echo "Test FAILED"
exit 1
fi
echo "Test PASSED"
version: ${{ env.version }}
target: release-default
arch: ${{ matrix.arch }}
# The ECR tag used below is to allow us to test by pushing to a private ECR registry.
# If you want to pubish to ECR use the following syntax instead - public.ecr.aws/hashicorp/${{env.repo}}:${{env.version}}.
# Please note - if you haven't previously been publishing to ECR you will need to reach out to team-rel-eng
# - https://hashicorp.atlassian.net/wiki/spaces/RDXPOC/pages/2298218311/How+to+Push+a+Docker+image+to+ECR
tags: |
docker.io/hashicorp/${{ env.repo }}:${{ env.version }}
public.ecr.aws/hashicorp/${{ env.repo }}:${{ env.version }}
# dev_tags are tags that get automatically pushed whenever successful
# builds make it to the stable channel. The intention is for these tags
# to be used for early testing of new code prior to official releases
# going out. The stable channel implies that all tests and scans have
# completed successfully, so these images should be _stable_ but are not
# intended for production use.
#
# Here we have two example dev tags. The first (ending -dev) is a tag
# that will be updated over-and-over as new builds arrive in stable.
#
# The second (using the git SHA) will produce a new separate tag for
# each commit that is built. (These can still be overridden if the same
# commit is built successfully a second time, but that is a less likely
# scenario.) These kinds of dev tags are useful if you want to be able
# to use Docker images built from those specific commits.
#
# NOTE: dev_tags MUST publish to the 'hashicorppreview' DockerHub org, it
# will fail to any other DockerHub org or registry. You can optionally
# prepend docker.io
dev_tags: |
hashicorppreview/${{ env.repo }}:${{ env.version }}
hashicorppreview/${{ env.repo }}:${{ env.version }}-${{ github.sha }}
upload-dev-docker:
name: Upload dev image (hashicorpdev/consul-ecs:<commit>)
environment: dockerhub/hashicorpdev
needs:
- get-product-version
- build-docker-default
runs-on: ubuntu-latest
strategy:
matrix:
arch: ["amd64"]
env:
repo: ${{ github.event.repository.name }}
version: ${{ needs.get-product-version.outputs.product-version }}
target: release-default
git-short-sha: ${{ needs.get-product-version.outputs.git-short-sha }}
steps:
- name: download image artifact
uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7
with:
name: ${{ env.repo }}_${{ env.target }}_linux_${{ matrix.arch }}_${{ env.version }}_${{ github.sha }}.docker.dev.tar
- name: docker push
shell: bash
run: |
TAG=hashicorpdev/${{ env.repo }}:${{ env.git-short-sha }}
echo "==> Load docker image from tar archive"
docker load -i "${{ env.repo }}_${{ env.target }}_linux_${{ matrix.arch }}_${{ env.version }}_${{ github.sha }}.docker.dev.tar"
echo "==> Tag docker image $TAG"
docker tag "hashicorppreview/${{ env.repo }}:${{ env.version }}-${{ github.sha }}" "$TAG"
echo "==> Docker login"
echo ${{ secrets.DOCKER_PASS }} | docker login -u=${{ secrets.DOCKER_USER }} --password-stdin
echo "==> Push docker image $TAG"
docker push "$TAG"
- name: docker push (latest)
shell: bash
if: ${{ github.ref == 'refs/heads/main' }}
run: |
TAG=hashicorpdev/${{ env.repo }}:latest
echo "==> Tag docker image $TAG as :latest"
docker tag "hashicorpdev/${{ env.repo }}:${{ env.git-short-sha }}" "$TAG"
echo "==> Push docker image $TAG"
docker push "$TAG"
# This is job is required for branch protection as a required GitHub check
# because GitHub actions show up as checks at the job level and not the
# workflow level. This is currently a feature request:
# https://github.com/orgs/community/discussions/12395
#
# This job must:
# - be placed after the fanout of a workflow so that everything fans back in
# to this job.
# - "need" any job that is part of the fan out / fan in
# - include if: always() logic because we may have conditional jobs that this job
# needs, and this would potentially get skipped if a previous job got skipped.
# The if clause ensures it does not get skipped.
build-success:
needs:
- generate-metadata-file
- build-linux
- build-docker-default
- upload-dev-docker
runs-on: ubuntu-latest
if: always()
steps:
- name: evaluate upstream job results
run: |
# exit 1 if failure or cancelled result for any upstream job
# this ensures that we fail the PR check regardless of cancellation, rather than skip-passing it
# see https://docs.github.com/en/actions/using-jobs/using-conditions-to-control-job-execution#overview
if printf '${{ toJSON(needs) }}' | grep -E -i '\"result\": \"(failure|cancelled)\"'; then
printf "Tests failed or workflow cancelled:\n\n${{ toJSON(needs) }}"
exit 1
fi