Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shared workflows #70

Merged
merged 4 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
173 changes: 37 additions & 136 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,141 +8,42 @@ 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 }} > 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:

It may take a few minutes to spin up, but you can view it here once it's ready: [${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}-preview](http://${{ github.event.repository.name }}.example-ui-pr-${{ github.event.pull_request.number }}-preview.127.0.0.1.sslip.io)

You can verify the PR is ready with `kubectl`:

```bash
kubectl get ksvc -n example-ui-pr-${{ github.event.pull_request.number }}-preview
```
edit-mode: replace
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:

It may take a few minutes to spin up, but you can view it here once it's ready: [${{ github.event.repository.name }}-pr-${{ github.event.pull_request.number }}-preview](http://${{ github.event.repository.name }}.example-ui-pr-${{ github.event.pull_request.number }}-preview.127.0.0.1.sslip.io)

You can verify the PR is ready with `kubectl`:
```bash
kubectl get ksvc -n example-ui-pr-${{ github.event.pull_request.number }}-preview
```
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/sveltekit-web3auth-template
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