Change the sample text prior to the demo (#8) #27
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: Main | |
on: | |
push: | |
branches: ["main"] | |
# Ensure only one instance of the workflow is running at a time | |
# This helps with race conditions when upserting releases | |
concurrency: | |
group: '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 AND to write tags | |
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 }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:staging | |
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 so that if we get into a weird state | |
* we can provide a better error. | |
*/ | |
const PAGE_SIZE = 10; | |
const releases = await github.rest.repos.listReleases({ | |
owner, | |
repo, | |
per_page: PAGE_SIZE, | |
}); | |
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(', ')}`); | |
} | |
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 }); | |
} | |
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.html_url}`); |