GHA... #12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Test & Build & Deploy | |
on: | |
push: | |
branches: [master] | |
pull_request: | |
release: | |
types: [published] | |
jobs: | |
build_info: | |
timeout-minutes: 1 | |
runs-on: ubuntu-latest | |
outputs: | |
GITHUB_SHA_SHORT: ${{ steps.create.outputs.GITHUB_SHA_SHORT }} | |
BRANCH_ESCAPED: ${{ steps.create.outputs.BRANCH_ESCAPED }} | |
COMMIT_URL: ${{ steps.create.outputs.COMMIT_URL }} | |
BUILD_URL: ${{ steps.create.outputs.BUILD_URL }} | |
BUILD_TIMESTAMP: ${{ steps.create.outputs.BUILD_TIMESTAMP }} | |
DOCKER_TAG: ${{ steps.create.outputs.DOCKER_TAG }} | |
steps: | |
- id: create | |
name: "@${{ github.actor }} initiated CI/CD for CSMS from branch '${{ github.head_ref }}' (or main, if empty)..." | |
run: | | |
# Create short Git SHA equivalent to `$(git rev-parse --short HEAD)` e.g. "49b08be", fake change | |
GITHUB_SHA_SHORT="${GITHUB_SHA::7}" | |
echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT" && echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT" >> "$GITHUB_OUTPUT" | |
# Create escaped branch head ref, e.g. "`feature/CSMS-424-readable-tags` -> `feature.CSMS.424.readable.tags`" | |
echo "gh ref: $GITHUB_REF" | |
echo "head ref: $GITHUB_HEAD_REF" | |
echo "tag: $GITHUB_TAG" | |
GITHUB_BRANCH=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}} | |
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then | |
GITHUB_TAG=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/tags/}} | |
echo "Override branch with GITHUB_TAG=$GITHUB_TAG" | |
GITHUB_BRANCH=${GITHUB_TAG} | |
fi | |
BRANCH_ESCAPED="${GITHUB_BRANCH//[\-\/+]/.}" | |
echo "GITHUB_BRANCH=$GITHUB_BRANCH ($GITHUB_HEAD_REF), BRANCH_ESCAPED=\"$BRANCH_ESCAPED\"" | |
echo "BRANCH_ESCAPED=$BRANCH_ESCAPED" >> "$GITHUB_OUTPUT" | |
# Create commit URL, e.g. "https://github.com/MyCompany/mcsms/commit/49b08be" | |
COMMIT_URL="https://github.com/${{ github.repository }}/commit/$GITHUB_SHA_SHORT" | |
echo "COMMIT_URL=$COMMIT_URL" && echo "COMMIT_URL=$COMMIT_URL" >> "$GITHUB_OUTPUT" | |
# Create build URL to the GHA workflow, e.g. "https://github.com/MyCompany/mcsms/actions/runs/6942892986/" | |
BUILD_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}/" | |
echo "BUILD_URL=$BUILD_URL" && echo "BUILD_URL=$BUILD_URL" >> "$GITHUB_OUTPUT" | |
# Create ISO format build timestamp in UTC, e.g. "2023-11-21T11:06:25Z" | |
BUILD_TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") | |
echo "BUILD_TIMESTAMP=$BUILD_TIMESTAMP" && echo "BUILD_TIMESTAMP=$BUILD_TIMESTAMP" >> "$GITHUB_OUTPUT" | |
# Create registry-safe Docker image tag with `<yymmdd'T'hhmm-date>-<gitBranch>-<gitSha>` info, | |
# e.g. "231121T1106-feature.CSMS.424.readable.tags-49b08be" | |
DOCKER_TAG=$(date -u +"%y%m%dT%H%M")-${BRANCH_ESCAPED}-${GITHUB_SHA_SHORT} | |
echo "DOCKER_TAG=$DOCKER_TAG" && echo "DOCKER_TAG=$DOCKER_TAG" >> "$GITHUB_OUTPUT" | |
# Create build-info.json for embedding in container images | |
cat <<EOF > ./build-info.json | |
{ | |
"COMMIT_URL": "$COMMIT_URL", | |
"BUILD_URL": "$BUILD_URL", | |
"BUILD_TIMESTAMP": "$BUILD_TIMESTAMP", | |
"DOCKER_TAG": "$DOCKER_TAG" | |
} | |
EOF | |
- id: build-info | |
name: Store build-info.json artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: build-info-json | |
path: build-info.json | |
test: | |
timeout-minutes: 10 | |
needs: build_info | |
services: | |
postgres: | |
image: postgis/postgis:14-3.3-alpine | |
env: | |
POSTGRES_PASSWORD: 'postgres' | |
ports: | |
- 5432:5432 | |
redis: | |
image: redis | |
# Set health checks to wait until redis has started | |
options: >- | |
--health-cmd "redis-cli ping" | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
ports: | |
- 6379:6379 | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
shell: bash | |
env: | |
COMMIT_URL: ${{ needs.build_info.outputs.COMMIT_URL }} | |
DOCKER_TAG: ${{ needs.build_info.outputs.DOCKER_TAG }} | |
BACKOFFICE_SKIP_VARIABLE_REPLACEMENT: true | |
steps: | |
- uses: actions/checkout@v3 | |
- name: '@${{ github.actor }} is testing CSMS from ${{ needs.build_info.outputs.BRANCH_ESCAPED }}...' | |
run: echo "Testing $COMMIT_URL before building Docker tag \"${{ needs.build_info.outputs.DOCKER_TAG }}\"..." | |
- name: Fake success! | |
run: sleep 5 && exit 0 | |
deploy_staging: | |
timeout-minutes: 30 | |
if: ${{ github.ref_name == 'master' || github.event_name == 'release' }} | |
needs: | |
- build_info | |
- test | |
concurrency: staging_env | |
environment: staging | |
defaults: | |
run: | |
#working-directory: ./infra/environments/staging | |
shell: bash | |
runs-on: ubuntu-latest | |
env: | |
COMMIT_URL: ${{ needs.build_info.outputs.COMMIT_URL }} | |
DOCKER_TAG: ${{ needs.build_info.outputs.DOCKER_TAG }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Fake success! | |
run: sleep 5 && exit 0 | |
push_to_prod: | |
timeout-minutes: 5 | |
if: ${{ github.event_name == 'release' }} | |
needs: | |
- build_info | |
- deploy_staging | |
environment: prod | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
name: | |
- api-cpo | |
- api-ctek-poller | |
- api-emsp | |
- ui-backoffice | |
env: | |
COMMIT_URL: ${{ needs.build_info.outputs.COMMIT_URL }} | |
DOCKER_TAG: ${{ needs.build_info.outputs.DOCKER_TAG }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: '@${{ github.actor }} is pushing CSMS `${{ matrix.name }}` image from ${{ needs.build_info.outputs.BRANCH_ESCAPED }} to prod...' | |
run: echo "Pushing Docker image \"${{ matrix.name }}:$DOCKER_TAG\" from $COMMIT_URL to prod Artifact Registry..." | |
- name: Fake success! | |
run: sleep 5 && exit 0 | |
deploy_prod: | |
timeout-minutes: 30 | |
if: ${{ github.event_name == 'release' }} | |
needs: | |
- build_info | |
- push_to_prod | |
- deploy_staging | |
concurrency: prod_env | |
environment: prod | |
defaults: | |
run: | |
#working-directory: ./infra/environments/prod | |
shell: bash | |
runs-on: ubuntu-latest | |
env: | |
COMMIT_URL: ${{ needs.build_info.outputs.COMMIT_URL }} | |
DOCKER_TAG: ${{ needs.build_info.outputs.DOCKER_TAG }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Fake success! | |
run: sleep 5 && exit 0 | |
notify_slack: | |
timeout-minutes: 1 | |
needs: | |
- build_info | |
- deploy_staging | |
- deploy_prod | |
if: ${{ github.ref_name == 'master' || github.event_name == 'release' }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Fake success! | |
run: sleep 5 && exit 0 |