diff --git a/.github/actions/branch-specific-config/action.yml b/.github/actions/branch-specific-config/action.yml new file mode 100644 index 0000000..9ea23bf --- /dev/null +++ b/.github/actions/branch-specific-config/action.yml @@ -0,0 +1,75 @@ +name: 'branch-specific-config' +description: 'Determine branch-specific configuration' +outputs: + branch-name: + description: "Branch name" + value: ${{ steps.branch-specific-config.outputs.branch-name }} + image-rev: + description: "Docker image revision" + value: ${{ steps.branch-specific-config.outputs.image-rev }} + env: + description: "Environment" + value: ${{ steps.branch-specific-config.outputs.env }} + tf-workingdir: + description: "Terraform working directory" + value: ${{ steps.branch-specific-config.outputs.tf-workingdir }} + tf-lockfile: + description: "Terraform lockfile" + value: ${{ steps.branch-specific-config.outputs.tf-lockfile }} + docker-load: + description: "Docker load" + value: ${{ steps.branch-specific-config.outputs.docker-load }} + docker-push: + description: "Docker push" + value: ${{ steps.branch-specific-config.outputs.docker-push }} + manifests-dir: + description: "Manifests directory for the environment" + value: ${{ steps.branch-specific-config.outputs.manifests-dir }} + +runs: + using: "composite" + steps: + - name: Branch specific config + id: branch-specific-config + run: | + set -euxo pipefail + + PREFIX="refs/heads/" + BRANCH_NAME="${GITHUB_REF#"$PREFIX"}" + + ret=0 + git ls-remote --exit-code origin staging || ret=$? + if [ "${BRANCH_NAME}" = "main" ] || [ "${ret}" -eq 2 ]; then + echo "Running production build or running build in repo without a staging branch" + IMAGE_REV="latest" + ENV="prod" + else + echo "Running staging build" + IMAGE_REV="staging" + ENV="stg" + fi + + TF_WD="terraform/workspaces/${ENV}" + + TF_LF="${TF_WD}/.terraform.lock.hcl" + + DOCKER_LOAD="false" + DOCKER_PUSH="true" + + if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then + echo "Running a pull request - Load Docker image only, no push" + DOCKER_LOAD="true" + DOCKER_PUSH="false" + fi + + MANIFESTS_DIR="manifests/${ENV}" + + echo "::set-output name=branch-name::$(echo $BRANCH_NAME)" + echo "::set-output name=image-rev::$(echo $IMAGE_REV)" + echo "::set-output name=env::$(echo $ENV)" + echo "::set-output name=tf-workingdir::$(echo $TF_WD)" + echo "::set-output name=tf-lockfile::$(echo $TF_LF)" + echo "::set-output name=docker-load::$(echo $DOCKER_LOAD)" + echo "::set-output name=docker-push::$(echo $DOCKER_PUSH)" + echo "::set-output name=manifests-dir::$(echo $MANIFESTS_DIR)" + shell: bash \ No newline at end of file