|
1 | | -name: .Deploys |
| 1 | +name: .Deploy |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | workflow_call: |
5 | 5 | inputs: |
6 | | - atomic: |
7 | | - description: Atomic deployment? That means fail all or nothing |
8 | | - default: false |
9 | | - required: false |
10 | | - type: boolean |
11 | | - directory: |
12 | | - description: Chart directory |
13 | | - default: "charts/app" |
14 | | - required: false |
15 | | - type: string |
16 | 6 | environment: |
17 | | - description: Environment name; omit for PRs |
18 | | - required: false |
| 7 | + description: GitHub/OpenShift environment |
| 8 | + default: "" |
19 | 9 | type: string |
20 | | - oc_server: |
21 | | - default: https://api.silver.devops.gov.bc.ca:6443 |
22 | | - description: OpenShift server |
23 | | - required: false |
24 | | - type: string |
25 | | - params: |
26 | | - description: Extra parameters to pass to helm upgrade |
27 | | - required: false |
| 10 | + target: |
| 11 | + description: Deployment target; usually PR number, test or prod |
| 12 | + default: ${{ github.event.number }} |
28 | 13 | type: string |
29 | 14 | tag: |
30 | | - description: Specify a tag to deploy; defaults to PR number |
31 | | - required: false |
| 15 | + description: Container tag |
| 16 | + default: ${{ github.event.number }} |
32 | 17 | type: string |
33 | 18 | triggers: |
34 | | - description: Paths used to trigger a deployment; e.g. ('./backend/' './frontend/) |
35 | | - required: false |
36 | | - type: string |
37 | | - db_user: |
38 | | - description: The database user |
39 | | - required: false |
40 | | - default: "app" |
| 19 | + description: Triggers for deployment |
| 20 | + default: "" |
41 | 21 | type: string |
42 | | - debug: |
43 | | - description: Debug mode |
44 | | - default: false |
45 | | - required: false |
46 | | - type: boolean |
47 | | - |
48 | | - ### Usually a bad idea / not recommended |
49 | | - timeout-minutes: |
50 | | - description: "Timeout minutes" |
51 | | - default: 30 |
52 | | - required: false |
53 | | - type: number |
54 | | - values: |
55 | | - description: "Values file" |
56 | | - default: "values.yaml" |
57 | | - required: false |
58 | | - type: string |
59 | | - outputs: |
60 | | - tag: |
61 | | - description: "Which tag was used for deployment?" |
62 | | - value: ${{ jobs.deploy.outputs.tag }} |
63 | | - triggered: |
64 | | - description: "Has a deployment has been triggered?" |
65 | | - value: ${{ jobs.deploy.outputs.triggered }} |
66 | 22 |
|
67 | 23 | secrets: |
| 24 | + db_password: |
| 25 | + description: Database password (required except on pull_request) |
| 26 | + required: false |
68 | 27 | oc_namespace: |
69 | 28 | description: OpenShift namespace |
70 | 29 | required: true |
71 | 30 | oc_token: |
72 | 31 | description: OpenShift token |
73 | 32 | required: true |
| 33 | + outputs: |
| 34 | + triggered: |
| 35 | + description: Has deployment been triggered? |
| 36 | + value: ${{ jobs.init.outputs.triggered }} |
74 | 37 |
|
75 | 38 | permissions: {} |
76 | 39 |
|
77 | 40 | jobs: |
78 | | - deploy: |
79 | | - name: Stack |
| 41 | + init: |
| 42 | + name: Deploy (init) |
80 | 43 | environment: ${{ inputs.environment }} |
81 | | - runs-on: ubuntu-24.04 |
82 | 44 | outputs: |
83 | | - tag: ${{ inputs.tag || steps.pr.outputs.pr }} |
84 | 45 | triggered: ${{ steps.deploy.outputs.triggered }} |
85 | | - timeout-minutes: ${{ inputs.timeout-minutes }} |
| 46 | + runs-on: ubuntu-24.04 |
86 | 47 | steps: |
87 | | - - uses: bcgov/actions-openshift/crunchy@75454b2b33edf37135f6f167775c77a3414207f6 # v0.3.0 |
88 | | - name: Deploy Crunchy |
89 | | - id: deploy_crunchy |
90 | | - with: |
91 | | - oc_namespace: ${{ secrets.oc_namespace }} |
92 | | - oc_token: ${{ secrets.oc_token }} |
93 | | - environment: ${{ inputs.environment }} |
94 | | - values_file: charts/crunchy/values.yml |
95 | | - triggers: ${{ inputs.triggers }} |
96 | | - # Variables |
97 | | - - if: inputs.tag == '' |
98 | | - id: pr |
99 | | - uses: bcgov/action-get-pr@3931962bf13b3f4702d0013ec7c836f752ff1a3b # v0.3.0 |
100 | | - |
101 | | - - id: vars |
| 48 | + - name: Database password |
| 49 | + id: db |
| 50 | + env: |
| 51 | + EVENT_NAME: ${{ github.event_name }} |
| 52 | + DB_PASSWORD: ${{ secrets.db_password }} |
102 | 53 | run: | |
103 | | - # Vars: tag and release |
104 | | -
|
105 | | - # Tag defaults to PR number, but can be overridden by inputs.tag |
106 | | - tag=${{ inputs.tag || steps.pr.outputs.pr }} |
107 | | -
|
108 | | - # Release name includes run numbers to ensure uniqueness |
109 | | - release=${{ github.event.repository.name }}-${{ inputs.environment || steps.pr.outputs.pr || inputs.tag }} |
110 | | -
|
111 | | - # version, to support helm packaging for non-pr based releases (workflow_dispatch). default to 1.0.0+github run number |
112 | | - version=1.0.0+${{ github.run_number }} |
113 | | -
|
114 | | - # Summary |
115 | | - echo "tag=${tag}" |
116 | | - echo "release=${release}" |
117 | | - echo "version=${version}" |
118 | | -
|
119 | | - # Output |
120 | | - echo "tag=${tag}" >> $GITHUB_OUTPUT |
121 | | - echo "release=${release}" >> $GITHUB_OUTPUT |
122 | | - echo "version=${version}" >> $GITHUB_OUTPUT |
123 | | -
|
124 | | - - name: Stop pre-existing deployments on PRs (status = pending-upgrade) |
125 | | - if: github.event_name == 'pull_request' |
126 | | - uses: bcgov/actions-openshift/oc-runner@75454b2b33edf37135f6f167775c77a3414207f6 # v0.3.0 |
| 54 | + if [ "${EVENT_NAME}" = "pull_request" ]; then |
| 55 | + password="$(openssl rand -hex 16)" |
| 56 | + if [ -z "${password}" ]; then |
| 57 | + echo "::error::openssl failed to generate a PR database password" |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + else |
| 61 | + if [ -z "${DB_PASSWORD}" ]; then |
| 62 | + echo "::error::db_password GitHub secret is required for ${EVENT_NAME} deploys" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + password="${DB_PASSWORD}" |
| 66 | + fi |
| 67 | + echo "::add-mask::${password}" |
| 68 | + echo "password=${password}" >> "${GITHUB_OUTPUT}" |
| 69 | + - uses: bcgov/actions-openshift/deployer@75454b2b33edf37135f6f167775c77a3414207f6 # v0.3.0 |
| 70 | + id: deploy |
127 | 71 | with: |
| 72 | + file: common/openshift.init.yml |
128 | 73 | oc_namespace: ${{ secrets.oc_namespace }} |
129 | | - oc_token: ${{ secrets.oc_token }} |
130 | 74 | oc_server: ${{ vars.oc_server }} |
| 75 | + oc_token: ${{ secrets.oc_token }} |
| 76 | + # Secret + NetworkPolicies: create if missing. Postgres ignores |
| 77 | + # POSTGRES_PASSWORD after first boot; restamping the Secret desyncs Flyway. |
| 78 | + # New NetworkPolicy names still apply; existing NP specs do not update. |
| 79 | + overwrite: false |
| 80 | + parameters: -p DB_PASSWORD="${{ steps.db.outputs.password }}" |
| 81 | + -p ZONE="${{ inputs.target }}" |
| 82 | + -p NAME="${{ github.event.repository.name }}" |
131 | 83 | triggers: ${{ inputs.triggers }} |
132 | | - commands: | |
133 | | - # Interrupt any previous deployments (PR only) |
134 | | - PREVIOUS=$(helm status ${{ steps.vars.outputs.release }} -o json | jq .info.status || true) |
135 | | - if [[ ${PREVIOUS} =~ pending ]]; then |
136 | | - echo "Rollback triggered" |
137 | | - helm rollback ${{ steps.vars.outputs.release }} || \ |
138 | | - helm uninstall ${{ steps.vars.outputs.release }} |
139 | | - fi |
| 84 | + debug: true |
140 | 85 |
|
141 | | - - uses: actions/checkout@v6 |
142 | | - - name: Debug Values File |
143 | | - if: inputs.debug |
144 | | - run: ls -l charts/crunchy/values.yml |
145 | | - - name: Helm Deploy |
| 86 | + deploy: |
| 87 | + name: Deploy (${{ matrix.name }}) |
| 88 | + environment: ${{ inputs.environment }} |
| 89 | + needs: [init] |
| 90 | + runs-on: ubuntu-24.04 |
| 91 | + timeout-minutes: 15 |
| 92 | + strategy: |
| 93 | + matrix: |
| 94 | + name: [database, backend-java, backend-py] |
| 95 | + include: |
| 96 | + - name: database |
| 97 | + file: common/openshift.database.yml |
| 98 | + overwrite: false |
| 99 | + parameters: -p DB_PVC_SIZE="256Mi" |
| 100 | + - name: backend-java |
| 101 | + file: backend-java/openshift.deploy.yml |
| 102 | + overwrite: true |
| 103 | + parameters: -p IMAGE_TAG="${{ inputs.tag }}" |
| 104 | + -p COMPONENT="backend-java" |
| 105 | + - name: backend-py |
| 106 | + file: backend-py/openshift.deploy.yml |
| 107 | + overwrite: true |
| 108 | + parameters: -p IMAGE_TAG="${{ inputs.tag }}" |
| 109 | + -p COMPONENT="backend-py" |
| 110 | + -p MIGRATIONS="migrations-py" |
| 111 | + outputs: |
| 112 | + triggered: ${{ steps.deploy.outputs.triggered }} |
| 113 | + steps: |
| 114 | + - uses: bcgov/actions-openshift/deployer@75454b2b33edf37135f6f167775c77a3414207f6 # v0.3.0 |
146 | 115 | id: deploy |
147 | | - uses: bcgov/actions-openshift/oc-runner@75454b2b33edf37135f6f167775c77a3414207f6 # v0.3.0 |
148 | 116 | with: |
| 117 | + file: ${{ matrix.file }} |
149 | 118 | oc_namespace: ${{ secrets.oc_namespace }} |
150 | | - oc_token: ${{ secrets.oc_token }} |
151 | 119 | oc_server: ${{ vars.oc_server }} |
| 120 | + oc_token: ${{ secrets.oc_token }} |
| 121 | + overwrite: ${{ matrix.overwrite }} |
| 122 | + debug: true |
| 123 | + parameters: -p ZONE="${{ inputs.target }}" |
| 124 | + -p NAME="${{ github.event.repository.name }}" |
| 125 | + ${{ matrix.parameters }} |
152 | 126 | triggers: ${{ inputs.triggers }} |
153 | | - ref: ${{ github.ref }} |
154 | | - commands: | |
155 | | - # Deploy |
156 | | -
|
157 | | - # If directory provided, cd to it |
158 | | - [ -z "${{ inputs.directory }}" ]|| cd ${{ inputs.directory }} |
159 | | -
|
160 | | - # Helm package |
161 | | - sed -i 's/^name:.*/name: ${{ github.event.repository.name }}/' Chart.yaml |
162 | | - helm package -u . --app-version="tag-${{ steps.vars.outputs.tag }}_run-${{ github.run_number }}" --version=${{ steps.pr.outputs.pr || steps.vars.outputs.version }} |
163 | | - # print the values.yaml file to see the values being used |
164 | | - # Helm upgrade/rollout |
165 | | - helm upgrade \ |
166 | | - --set-string global.repository=${{ github.repository }} \ |
167 | | - --set-string global.tag="${{ steps.vars.outputs.tag }}" \ |
168 | | - --set-string global.config.databaseUser="${{ inputs.db_user }}" \ |
169 | | - --set-string global.databaseAlias="${{ steps.deploy_crunchy.outputs.release }}-crunchy" \ |
170 | | - ${{ inputs.params }} \ |
171 | | - --install --wait ${{ inputs.atomic && '--atomic' || '' }} ${{ steps.vars.outputs.release }} \ |
172 | | - --timeout ${{ inputs.timeout-minutes }}m \ |
173 | | - --values ${{ inputs.values }} \ |
174 | | - ./${{ github.event.repository.name }}-${{ steps.pr.outputs.pr || steps.vars.outputs.version }}.tgz |
175 | | -
|
176 | | - # Helm release history |
177 | | - helm history ${{ steps.vars.outputs.release }} |
178 | | -
|
179 | | - # Completed pod cleanup |
180 | | - oc delete po --field-selector=status.phase==Succeeded || true |
0 commit comments