Skip to content

Commit ca695d2

Browse files
masl2stevebux
authored andcommitted
Add metadata stage to determine versions
1 parent c74e0a8 commit ca695d2

File tree

1 file changed

+75
-14
lines changed

1 file changed

+75
-14
lines changed

.github/workflows/manual-proxy-environment-deploy.yaml

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,92 @@ permissions:
2323
contents: read
2424

2525
jobs:
26+
metadata:
27+
name: "Set CI/CD metadata"
28+
runs-on: ubuntu-latest
29+
timeout-minutes: 1
30+
outputs:
31+
build_datetime_london: ${{ steps.variables.outputs.build_datetime_london }}
32+
build_datetime: ${{ steps.variables.outputs.build_datetime }}
33+
build_timestamp: ${{ steps.variables.outputs.build_timestamp }}
34+
build_epoch: ${{ steps.variables.outputs.build_epoch }}
35+
nodejs_version: ${{ steps.variables.outputs.nodejs_version }}
36+
python_version: ${{ steps.variables.outputs.python_version }}
37+
terraform_version: ${{ steps.variables.outputs.terraform_version }}
38+
version: ${{ steps.variables.outputs.version }}
39+
is_version_prerelease: ${{ steps.variables.outputs.is_version_prerelease }}
40+
does_pull_request_exist: ${{ steps.pr_exists.outputs.does_pull_request_exist }}
41+
pr_number: ${{ steps.pr_exists.outputs.pr_number }}
42+
steps:
43+
- name: "Checkout code"
44+
uses: actions/checkout@v5
45+
- name: "Set CI/CD variables"
46+
id: variables
47+
run: |
48+
datetime=$(date -u +'%Y-%m-%dT%H:%M:%S%z')
49+
BUILD_DATETIME=$datetime make version-create-effective-file dir=.
50+
version=$(head -n 1 .version 2> /dev/null || echo unknown)
51+
echo "build_datetime_london=$(TZ=Europe/London date --date=$datetime +'%Y-%m-%dT%H:%M:%S%z')" >> $GITHUB_OUTPUT
52+
echo "build_datetime=$datetime" >> $GITHUB_OUTPUT
53+
echo "build_timestamp=$(date --date=$datetime -u +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT
54+
echo "build_epoch=$(date --date=$datetime -u +'%s')" >> $GITHUB_OUTPUT
55+
echo "nodejs_version=$(grep "^nodejs\s" .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
56+
echo "python_version=$(grep "^python\s" .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
57+
echo "terraform_version=$(grep "^terraform\s" .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT
58+
echo "version=$(echo $version)" >> $GITHUB_OUTPUT
59+
echo "is_version_prerelease=$(if [[ $version == *-* ]]; then echo "true"; else echo "false"; fi)" >> $GITHUB_OUTPUT
60+
- name: "Check if pull request exists for this branch"
61+
id: pr_exists
62+
env:
63+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64+
run: |
65+
branch_name=${GITHUB_HEAD_REF:-$(echo $GITHUB_REF | sed 's#refs/heads/##')}
66+
echo "Current branch is '$branch_name'"
67+
68+
pr_json=$(gh pr list --head "$branch_name" --state open --json number --limit 1)
69+
pr_number=$(echo "$pr_json" | jq -r '.[0].number // empty')
70+
71+
if [[ -n "$pr_number" ]]; then
72+
echo "Pull request exists: #$pr_number"
73+
echo "does_pull_request_exist=true" >> $GITHUB_OUTPUT
74+
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
75+
else
76+
echo "Pull request doesn't exist"
77+
echo "does_pull_request_exist=false" >> $GITHUB_OUTPUT
78+
echo "pr_number=" >> $GITHUB_OUTPUT
79+
fi
80+
- name: "List variables"
81+
run: |
82+
export BUILD_DATETIME_LONDON="${{ steps.variables.outputs.build_datetime_london }}"
83+
export BUILD_DATETIME="${{ steps.variables.outputs.build_datetime }}"
84+
export BUILD_TIMESTAMP="${{ steps.variables.outputs.build_timestamp }}"
85+
export BUILD_EPOCH="${{ steps.variables.outputs.build_epoch }}"
86+
export NODEJS_VERSION="${{ steps.variables.outputs.nodejs_version }}"
87+
export PYTHON_VERSION="${{ steps.variables.outputs.python_version }}"
88+
export TERRAFORM_VERSION="${{ steps.variables.outputs.terraform_version }}"
89+
export VERSION="${{ steps.variables.outputs.version }}"
90+
export DOES_PULL_REQUEST_EXIST="${{ steps.pr_exists.outputs.does_pull_request_exist }}"
91+
export IS_VERSION_PRERELEASE="${{ steps.variables.outputs.is_version_prerelease }}"
92+
make list-variables
2693
deploy-environment:
2794
runs-on: ubuntu-latest
2895
name: Deploy to Environment
96+
needs: metadata
2997
steps:
3098
- name: Checkout
3199
uses: actions/checkout@v4
32100

33101
- uses: actions/setup-node@v6
34102
with:
35-
node-version: 24
103+
node-version: "${{ needs.metadata.outputs.nodejs_version }}"
36104

37105
- name: Npm install
38106
working-directory: .
39107
run: npm ci
40108
shell: bash
41109

42-
- name: "Check if pull request exists for this branch and set ENVIRONMENT/APIM_ENV"
43-
id: pr_exists
44-
env:
45-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
110+
- name: "Set ENVIRONMENT/APIM_ENV"
111+
id: environments
46112
run: |
47113
branch_name=${GITHUB_HEAD_REF:-$(echo $GITHUB_REF | sed 's#refs/heads/##')}
48114
echo "Current branch is '$branch_name'"
@@ -53,20 +119,15 @@ jobs:
53119
ENVIRONMENT="${{ inputs.proxy_environment }}"
54120
fi
55121
56-
pr_json=$(gh pr list --head "$branch_name" --state open --json number --limit 1)
57-
pr_number=$(echo "$pr_json" | jq -r '.[0].number // empty')
122+
pr_exists="${{ needs.metadata.outputs.does_pull_request_exist }}"
123+
pr_number="${{ needs.metadata.outputs.pr_number }}"
58124
59-
if [[ -n "$pr_number" ]]; then
60-
echo "Pull request exists: #$pr_number"
61-
echo "does_pull_request_exist=true" >> $GITHUB_OUTPUT
62-
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
125+
if [[ $pr_exists = true ]]; then
63126
APIM_ENV="$ENVIRONMENT-pr"
64127
echo "changing environment variable so that PR number is used in proxy pipeline for setting env vars"
65128
ENVIRONMENT="pr$pr_number"
66129
else
67130
echo "Pull request doesn't exist, setting target env to main"
68-
echo "does_pull_request_exist=false" >> $GITHUB_OUTPUT
69-
echo "pr_number=" >> $GITHUB_OUTPUT
70131
APIM_ENV="$ENVIRONMENT"
71132
ENVIRONMENT="main"
72133
fi
@@ -77,7 +138,7 @@ jobs:
77138
- name: "Build proxies"
78139
env:
79140
PROXYGEN_API_NAME: nhs-notify-supplier
80-
PR_NUMBER: ${{ steps.pr_exists.outputs.pr_number }}
141+
PR_NUMBER: ${{ needs.metadata.outputs.pr_number }}
81142
APP_CLIENT_ID: ${{ secrets.APP_CLIENT_ID }}
82143
APP_PEM_FILE: ${{ secrets.APP_PEM_FILE }}
83144
uses: ./.github/actions/build-proxies

0 commit comments

Comments
 (0)