Skip to content

Commit

Permalink
Add workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsheepuk committed Dec 15, 2023
1 parent 0797219 commit 27f8bd6
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: main-publish

on:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
outputs:
tags: ${{ steps.meta.outputs.tags }}
steps:
- name: Login to docker registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker metadata
id: meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/${{ github.repository }}/wfexample
tags: |
type=ref,event=branch
type=ref,event=pr,suffix=-{{sha}}
- name: Build and Push to GitHub Packages
id: build-push
uses: docker/build-push-action@v2
continue-on-error: true
timeout-minutes: 3
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

deploy-live:
needs:
- build
runs-on: ubuntu-latest
container:
image: quay.io/appvia-wayfinder/wftoolbox:latest
steps:
- name: Deploy to app environment
id: deploy
env:
WAYFINDER_SERVER: ${{ vars.WAYFINDER_SERVER }}
WAYFINDER_WORKSPACE: ${{ vars.WAYFINDER_WORKSPACE }}
WAYFINDER_TOKEN: ${{ secrets.WAYFINDER_TOKEN }}
run: |
wf deploy component ${{ vars.APP_NAME }} ${{ vars.LIVE_ENV_NAME }} --component ${{ vars.APP_COMPONENT_NAME }} --image ${{ needs.build.outputs.tags }} --wait-for-ready 3m
20 changes: 20 additions & 0 deletions .github/workflows/pr-closed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: pr-preview-teardown

on:
pull_request:
types: [closed]

jobs:
teardown-preview:
runs-on: ubuntu-latest
container:
image: quay.io/appvia-wayfinder/wftoolbox:latest
steps:
- name: Clean up old resources
env:
WAYFINDER_SERVER: ${{ vars.WAYFINDER_SERVER }}
WAYFINDER_WORKSPACE: ${{ vars.WAYFINDER_WORKSPACE }}
WAYFINDER_TOKEN: ${{ secrets.WAYFINDER_TOKEN }}
run: |
export ENV_NAME="pr-${{ github.event.pull_request.number }}"
wf delete appenv ${{ vars.APP_NAME }}-${ENV_NAME} || true
77 changes: 77 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: build-publish

on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
outputs:
tags: ${{ steps.meta.outputs.tags }}
steps:
- name: Login to docker registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker metadata
id: meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/${{ github.repository }}/wfexample
tags: |
type=ref,event=branch
type=ref,event=pr,suffix=-{{sha}}
- name: Build and Push to GitHub Packages
id: build-push
uses: docker/build-push-action@v2
continue-on-error: true
timeout-minutes: 3
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

deploy-pr-preview:
needs:
- build
runs-on: ubuntu-latest
container:
image: quay.io/appvia-wayfinder/wftoolbox:latest
steps:
- name: Deploy to app environment
id: deploy
env:
WAYFINDER_SERVER: ${{ vars.WAYFINDER_SERVER }}
WAYFINDER_WORKSPACE: ${{ vars.WAYFINDER_WORKSPACE }}
WAYFINDER_TOKEN: ${{ secrets.WAYFINDER_TOKEN }}
IMAGE_TAG: ${{ needs.build.outputs.tags }}
run: |
export ENV_NAME="pr-${{ github.event.pull_request.number }}"
wf create appenv --app ${{ vars.APP_NAME }} --env ${ENV_NAME} --cluster ${{ vars.PR_PREVIEW_CLUSTER }} --stage nonprod --wildcard-certs || true
wf deploy component ${{ vars.APP_NAME }} ${ENV_NAME} --component ${{ vars.APP_COMPONENT_NAME }} --image ${IMAGE_TAG} --wait-for-ready 3m
echo "PREVIEW_URL=$(wf get appenv ${{ vars.APP_NAME }}-${ENV_NAME} -o json | jq -r '.status.deployment.components[0].endpoint')" >> $GITHUB_OUTPUT
- name: Comment the PR
uses: actions/github-script@v6
with:
script: |
const commentInfo = { issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo }
const commentIdentifier = 'Preview available for this PR'
const baseURL = '${{ steps.deploy.outputs.PREVIEW_URL }}'
const deployed = new Date().toUTCString()
const comment = { ...commentInfo, body: `${commentIdentifier}:
${baseURL}
<sub>Last deployed: ${deployed}</sub>
`}
for (let c of (await github.rest.issues.listComments(commentInfo)).data) {
if (c.user.type === 'Bot' && c.body.includes(commentIdentifier)) {
github.rest.issues.updateComment({ ...comment, comment_id: c.id })
return
}
}
github.rest.issues.createComment(comment)
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# wf-example-app
An example app to demonstrate using Wayfinder for PR previews and deployment

To use this example, generate an access token in your workspace and add as a secret:

```
wf use workspace ws1
wf create workspaceaccesstoken wf-example-app-ghactions --reset-token --show-token | gh secret set WAYFINDER_TOKEN -R appvia/wf-example-app -a actions
```

Permission the token:
* Add cluster.deployment on an existing cluster to host the PR environments (e.g. demo-envs)

Configure other variables, adjust the values as appropriate for your app, environment, and PR preview cluster:
```
gh variable set WAYFINDER_WORKSPACE -r appvia/wf-example-app --body ws1
gh variable set WAYFINDER_SERVER -r appvia/wf-example-app --body https://api-wayfinder.myorg.io
gh variable set APP_NAME -r appvia/wf-example-app --body wfexampleapp
gh variable set APP_COMPONENT_NAME -r appvia/wf-example-app --body ui
gh variable set PR_PREVIEW_CLUSTER -r appvia/wf-example-app --body demo-envs
gh variable set LIVE_ENV_NAME -r appvia/wf-example-app --body prod
```

0 comments on commit 27f8bd6

Please sign in to comment.