Skip to content

Commit

Permalink
feat: shared workflows (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickleet authored Nov 28, 2022
1 parent 6126099 commit 8dd87ea
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 369 deletions.
45 changes: 5 additions & 40 deletions .github/workflows/pr-close.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,8 @@ on:

jobs:

pr-close:
runs-on: ubuntu-latest

permissions:
issues: write
pull-requests: write
contents: write

steps:

- name: Checkout Preview Environments Project
uses: actions/checkout@v3
with:
repository: cloudnativeentrepreneur/example-preview-envs
persist-credentials: false

- name: Commit Preview Env Changes
run: |
rm helm/templates/${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}.yaml
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add .
git commit -m "remove: ${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}"
- name: Push Preview Env Changes
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GH_ORG_TOKEN }}
repository: cloudnativeentrepreneur/example-preview-envs

- name: Create comment about garbage collection
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
Your preview environment has been removed. It will be garbage collected soon.
To create a new preview environment, open a new pull request.
edit-mode: replace
preview-cleanup:
uses: CloudNativeEntrepreneur/actions/.github/workflows/gitops-preview-cleanup.yaml@main
secrets: inherit
with:
environment_repository: CloudNativeEntrepreneur/example-preview-envs
165 changes: 31 additions & 134 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,146 +8,43 @@ on:

jobs:

quality:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:

- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Run Npm Quality Scripts
run: |
npm ci
npm run lint --if-present
npm run build --if-present
npm run test --if-present
npx codecov
helm-quality:
uses: CloudNativeEntrepreneur/actions/.github/workflows/helm-quality.yaml@main
with:
helm_path: helm

preview-helm-quality:
uses: CloudNativeEntrepreneur/actions/.github/workflows/helm-quality.yaml@main
with:
helm_path: preview/helm

promote-helm-quality:
uses: CloudNativeEntrepreneur/actions/.github/workflows/helm-quality.yaml@main
with:
helm_path: promote/helm

node-quality:
uses: CloudNativeEntrepreneur/actions/.github/workflows/node-quality.yaml@main

preview:
# needs: quality
runs-on: ubuntu-latest

permissions:
packages: write
contents: write
issues: write
pull-requests: write

steps:

- name: downcase REPO
run: |
echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}
- name: Checkout
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ env.REPO }}

- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: |
${{ steps.meta.outputs.tags }}
ghcr.io/${{ env.REPO }}:pr-${{ github.event.pull_request.number }}-${{ github.sha }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=ghcr.io/${{ env.REPO }}:buildcache
cache-to: type=registry,ref=ghcr.io/${{ env.REPO }}:buildcache,mode=max

- name: Setup Helm
uses: azure/setup-helm@v3

- name: Checkout Preview Environments Project
uses: actions/checkout@v3
with:
path: previews
repository: cloudnativeentrepreneur/example-preview-envs
persist-credentials: false

- name: Create Preview from Helm template
run: |
mkdir -p previews/helm/templates
helm template preview/helm/ \
--set pr=pr-${{ github.event.pull_request.number }} \
--set tag=pr-${{ github.event.pull_request.number }}-${{ github.sha }} \
--set headRef=${{ github.head_ref }} \
--set repository.name=${{ github.event.repository.name }} \
> previews/helm/templates/${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}.yaml
- name: Commit Preview Env Changes
run: |
cd previews
if output=$(git status --porcelain) && [ -z "$output" ]; then
# Working directory clean
echo "No changes to commit"
else
# Uncommitted changes
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add -A
git commit -m "promote: ${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }} ${{ github.sha }}"
fi
- name: Push Preview Env Changes
uses: ad-m/github-push-action@master
with:
directory: previews
github_token: ${{ secrets.GH_ORG_TOKEN }}
repository: cloudnativeentrepreneur/example-preview-envs

- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: Your preview environment

- name: Create or update comment
uses: peter-evans/create-or-update-comment@v2
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
Your preview environment has been published! :rocket:
needs:
- helm-quality
- preview-helm-quality
- promote-helm-quality
- node-quality
uses: CloudNativeEntrepreneur/actions/.github/workflows/gitops-preview.yaml@main
secrets: inherit
with:
container: true
environment_repository: CloudNativeEntrepreneur/example-preview-envs
project: example-preview-envs
comment: |
Your preview environment has been published! :rocket:
This service doesn't have a public URL.
You can verify the PR is ready with `kubectl`:
```bash
kubectl get all -n ${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}-preview
kubectl get ksvc -n ${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}-preview
```
edit-mode: replace
116 changes: 24 additions & 92 deletions .github/workflows/publish-and-promote.yaml
Original file line number Diff line number Diff line change
@@ -1,98 +1,30 @@
name: publish-and-promote
name: promote
on:
push:
tags:
- v*.*.*
jobs:

publish:
runs-on: ubuntu-latest

permissions:
packages: write
contents: write

steps:

- name: downcase REPO
run: |
echo "REPO=${GITHUB_REPOSITORY,,}" >>${GITHUB_ENV}
# - name: use REPO
# run: echo "The value of REPO is: ${{ env.REPO }}"

- name: Checkout
uses: actions/checkout@v3
with:
persist-credentials: false
fetch-depth: 0

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ env.REPO }}

- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: |
${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=ghcr.io/${{ env.REPO }}:buildcache
cache-to: type=registry,ref=ghcr.io/${{ env.REPO }}:buildcache,mode=max

promote:
needs: publish
runs-on: ubuntu-latest

permissions:
contents: write

steps:
- name: Checkout production environment
uses: actions/checkout@v3
with:
repository: cloudnativeentrepreneur/example-prod-env
persist-credentials: false
fetch-depth: 0

- name: Update prod environment
uses: mikefarah/yq@master
with:
cmd: yq -i '.spec.source.targetRevision = "${{ github.ref_name }}"' helm/templates/${{ github.event.repository.name }}.yaml

- name: Commit Prod Env Changes
run: |
if output=$(git status --porcelain) && [ -z "$output" ]; then
# Working directory clean
echo "No changes to commit"
else
# Uncommitted changes
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add -A
git commit -m "feat: Promoting ${{ github.event.repository.name }} to ${{ github.ref_name }}"
fi
jobs:

- name: Push prod
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GH_ORG_TOKEN }}
repository: cloudnativeentrepreneur/example-prod-env
publish-container:
uses: CloudNativeEntrepreneur/actions/.github/workflows/publish-container.yaml@main
secrets: inherit

promote-local:
uses: CloudNativeEntrepreneur/actions/.github/workflows/gitops-promote-helm.yaml@main
secrets: inherit
with:
environment_repository: CloudNativeEntrepreneur/example-local-env
project: example-local-env
pull_request: true
values: |
# Set from promote job of CloudNativeEntrepreneur/example-hasura-projections-service
local: true
promote-prod:
uses: CloudNativeEntrepreneur/actions/.github/workflows/gitops-promote-helm.yaml@main
secrets: inherit
with:
environment_repository: CloudNativeEntrepreneur/example-prod-env
project: example-prod-env
pull_request: true
Loading

0 comments on commit 8dd87ea

Please sign in to comment.