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

Add basic CI #1

Merged
merged 18 commits into from
Feb 28, 2024
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
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: CI

on:
pull_request:
branches: ["main"]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Test building the docker image
run: docker build .
82 changes: 82 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Main

on:
push:
branches: ["main"]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build:
runs-on: ubuntu-latest
permissions:
packages: write
contents: write # Write is required to create/update releases
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Generate version based on date
run: echo "RELEASE_VERSION=$(date '+%Y-%m-%d_%H_%M')" >> $GITHUB_ENV

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.RELEASE_VERSION }}
build-args: |
APP_VERSION=${{ env.RELEASE_VERSION }}

- name: Create tag
uses: rickstaa/action-create-tag@v1
id: "tag_create"
with:
tag: ${{ env.RELEASE_VERSION }}

- name: Upsert pending release
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
/* We should only need to load 2 releases, as either both will be latest, or one will be latest and
* the other will be pending. We're loading a few extras here in case we get into a weird state.
*/
const PAGE_SIZE = 5;
const releases = await github.rest.repos.listReleases({ owner, repo });

const pendingReleases = releases.data.filter(release => release.prerelease);
if (pendingReleases.length > 1) {
throw new Error(`Found more than one pending release: ${pendingReleases.map(release => release.tag_name).join(', ')}`);
}

const latestRelease = releases.data.find(release => !release.prerelease);
if (!latestRelease) {
throw new Error('No latest release found');
}

if (pendingReleases.length === 1) {
console.log(`Found existing pending release: ${pendingReleases[0].tag_name}, replacing it`);
await github.rest.repos.deleteRelease({ owner, repo, release_id: pendingReleases[0].id });
}

console.log('No pending release found, creating one');
const newRelease = await github.rest.repos.createRelease({
owner,
repo,
prerelease: true,
tag_name: "${{ env.RELEASE_VERSION }}",
name: "${{ env.RELEASE_VERSION }}",
generate_release_notes: true,
});
console.log(`Created pending release: ${newRelease.data.tag_name}`);
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,23 @@ Proof-of-concept for a deployment/rollback flow using github actions and release
- The last release should be marked as a draft
- A deployment should be done to the `production` environment using the selected release

## Implementation

- ~~https://github.com/marketplace/actions/get-release is used to get the id of the last release~~
- ~~https://github.com/softprops/action-gh-release is used to create the new release~~
- ~~https://github.com/irongut/EditRelease is used to edit the release if it already exists~~
- Instead of using multiple poorly maintained I instead opted to make heavy use of `actions/github-script@v7`
- ~~`git log --pretty=oneline tagA...tagB` is used to get the commit messages since the last release~~
- It turned out github automatic release notes are good enough

## Run Sample App Locally

```sh
docker build --build-arg APP_VERSION=local -t action-deployment-poc:local .
docker run -p 7890:80 action-deployment-poc:local
```

# Initial setup

- The latest state of the main branch should be deployed to production
- A date tag and release should be up to date with the latest state of the main branch
Loading