|
| 1 | +name: "Publish supplier config to DynamoDB" |
| 2 | +description: "Reads config from a file store and publishes records to a DynamoDB table." |
| 3 | +author: "NHS Notify" |
| 4 | + |
| 5 | +inputs: |
| 6 | + source-path: |
| 7 | + description: "Path to the config store root directory" |
| 8 | + required: true |
| 9 | + target-env: |
| 10 | + description: "Target environment label (e.g. draft/int/prod)" |
| 11 | + required: true |
| 12 | + table-name: |
| 13 | + description: "DynamoDB table name" |
| 14 | + required: true |
| 15 | + force: |
| 16 | + description: "Bypass audit safety checks" |
| 17 | + required: false |
| 18 | + default: "false" |
| 19 | + dry-run: |
| 20 | + description: "Validate only (no AWS calls). Does not require credentials." |
| 21 | + required: false |
| 22 | + default: "false" |
| 23 | + |
| 24 | +runs: |
| 25 | + using: "composite" |
| 26 | + steps: |
| 27 | + - name: Resolve and download ddb-publish bundle |
| 28 | + shell: bash |
| 29 | + env: |
| 30 | + GH_TOKEN: ${{ github.token }} |
| 31 | + CALLER_REPO: ${{ github.repository }} |
| 32 | + ACTION_REPO: ${{ github.action_repository }} |
| 33 | + ACTION_REF: ${{ github.action_ref }} |
| 34 | + RELEASE_ASSET_NAME: ddb-publish-bundle.tgz |
| 35 | + WORKFLOW_ARTIFACT_NAME: ddb-publish-bundle |
| 36 | + run: | |
| 37 | + set -euo pipefail |
| 38 | +
|
| 39 | + download_dir="${RUNNER_TEMP}/ddb-publish" |
| 40 | + unpack_dir="${download_dir}/unpacked" |
| 41 | +
|
| 42 | + mkdir -p "${download_dir}" "${unpack_dir}" |
| 43 | +
|
| 44 | + echo "[ddb-publish] caller_repo=${CALLER_REPO}" |
| 45 | + echo "[ddb-publish] action_repo=${ACTION_REPO}" |
| 46 | + echo "[ddb-publish] action_ref=${ACTION_REF:-<empty>}" |
| 47 | +
|
| 48 | + if [[ -z "${ACTION_REF}" ]]; then |
| 49 | + echo "ERROR: github.action_ref is empty; unable to locate release asset or branch artifact." >&2 |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +
|
| 53 | + if [[ -z "${ACTION_REPO}" ]]; then |
| 54 | + echo "ERROR: github.action_repository is empty; unable to determine where to fetch bundle artifacts." >&2 |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | +
|
| 58 | + # Prefer release assets when the action ref is a tag with a matching release in the action repo. |
| 59 | + if gh release view "${ACTION_REF}" --repo "${ACTION_REPO}" >/dev/null 2>&1; then |
| 60 | + echo "[ddb-publish] Found release for ref '${ACTION_REF}' in '${ACTION_REPO}'. Downloading '${RELEASE_ASSET_NAME}'." |
| 61 | + gh release download "${ACTION_REF}" --repo "${ACTION_REPO}" --pattern "${RELEASE_ASSET_NAME}" --dir "${download_dir}" |
| 62 | + tar -xzf "${download_dir}/${RELEASE_ASSET_NAME}" -C "${unpack_dir}" |
| 63 | + echo "[ddb-publish] Bundle extracted from release asset." |
| 64 | + exit 0 |
| 65 | + fi |
| 66 | +
|
| 67 | + # Otherwise treat the ref as a branch-like ref and fetch the latest successful CI artifact from the action repo. |
| 68 | + branch="${ACTION_REF#refs/heads/}" |
| 69 | + echo "[ddb-publish] No release found for ref '${ACTION_REF}'. Falling back to latest workflow artifact on branch '${branch}' from '${ACTION_REPO}'." |
| 70 | +
|
| 71 | + run_id="" |
| 72 | + workflow_used="" |
| 73 | +
|
| 74 | + for workflow_file in "stage-3-build.yaml" "cicd-1-pull-request.yaml"; do |
| 75 | + echo "[ddb-publish] Looking for successful runs in workflow '${workflow_file}'." |
| 76 | +
|
| 77 | + set +e |
| 78 | + run_id_candidate="$(gh run list \ |
| 79 | + --repo "${ACTION_REPO}" \ |
| 80 | + --workflow "${workflow_file}" \ |
| 81 | + --branch "${branch}" \ |
| 82 | + --status success \ |
| 83 | + --json databaseId \ |
| 84 | + --jq '.[0].databaseId' 2>/tmp/ddb_publish_run_list_err.log)" |
| 85 | + rc=$? |
| 86 | + set -e |
| 87 | +
|
| 88 | + if [[ $rc -ne 0 ]]; then |
| 89 | + if grep -q "HTTP 404" /tmp/ddb_publish_run_list_err.log; then |
| 90 | + echo "[ddb-publish] Workflow '${workflow_file}' not found on default branch; trying next workflow candidate." |
| 91 | + continue |
| 92 | + fi |
| 93 | +
|
| 94 | + echo "ERROR: Failed to query workflow runs from '${ACTION_REPO}' for workflow '${workflow_file}'." >&2 |
| 95 | + cat /tmp/ddb_publish_run_list_err.log >&2 || true |
| 96 | + echo "HINT: ensure the token has read access to '${ACTION_REPO}' and workflow permissions allow actions read." >&2 |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | +
|
| 100 | + if [[ -n "${run_id_candidate}" && "${run_id_candidate}" != "null" ]]; then |
| 101 | + run_id="${run_id_candidate}" |
| 102 | + workflow_used="${workflow_file}" |
| 103 | + break |
| 104 | + fi |
| 105 | + done |
| 106 | +
|
| 107 | + if [[ -z "${run_id}" || "${run_id}" == "null" ]]; then |
| 108 | + echo "ERROR: Could not find a successful run on branch '${branch}' in '${ACTION_REPO}' containing artifact '${WORKFLOW_ARTIFACT_NAME}'. Checked workflows: stage-3-build.yaml, cicd-1-pull-request.yaml." >&2 |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | +
|
| 112 | + echo "[ddb-publish] Downloading artifact '${WORKFLOW_ARTIFACT_NAME}' from workflow run ${run_id} (${workflow_used}) in '${ACTION_REPO}'." |
| 113 | + gh run download "${run_id}" --repo "${ACTION_REPO}" --name "${WORKFLOW_ARTIFACT_NAME}" --dir "${download_dir}" |
| 114 | +
|
| 115 | + tarball_path="${download_dir}/${RELEASE_ASSET_NAME}" |
| 116 | + if [[ ! -f "${tarball_path}" ]]; then |
| 117 | + echo "ERROR: Downloaded artifact did not contain expected tarball '${RELEASE_ASSET_NAME}'." >&2 |
| 118 | + ls -la "${download_dir}" >&2 |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | +
|
| 122 | + tar -xzf "${tarball_path}" -C "${unpack_dir}" |
| 123 | + echo "[ddb-publish] Bundle extracted from workflow artifact." |
| 124 | +
|
| 125 | + - name: Run publisher |
| 126 | + shell: bash |
| 127 | + run: | |
| 128 | + set -euo pipefail |
| 129 | +
|
| 130 | + echo "[ddb-publish] Starting publish run" |
| 131 | + echo "[ddb-publish] source='${{ inputs.source-path }}' env='${{ inputs.target-env }}' table='${{ inputs.table-name }}' force='${{ inputs.force }}' dry-run='${{ inputs.dry-run }}'" |
| 132 | +
|
| 133 | + node "${RUNNER_TEMP}/ddb-publish/unpacked/index.cjs" \ |
| 134 | + --source "${{ inputs.source-path }}" \ |
| 135 | + --env "${{ inputs.target-env }}" \ |
| 136 | + --table "${{ inputs.table-name }}" \ |
| 137 | + $([[ "${{ inputs.force }}" == "true" ]] && echo "--force") \ |
| 138 | + $([[ "${{ inputs.dry-run }}" == "true" ]] && echo "--dry-run") |
| 139 | +
|
| 140 | + echo "[ddb-publish] Publish run completed" |
0 commit comments