Skip to content

Commit

Permalink
feat: moved action to use GitHub Package Registry (#54)
Browse files Browse the repository at this point in the history
Updated the project to store an already built image in the GitHub Package Registry.

This PR brings 3 changes:
- It creates a new release CI flow.
- It modifies the Docker image to be a multi step image.
- It changes the action definition file (`action.yml`) to use an image from the registry.

## The CI

The new CI workflow does the following:
- Checks that the Docker image can be built.
- Checks that the version for the package and the docker image are the same
- If the version in the `package.json` changed:
  - Tag the commit with the new version
  - Generate a new release (with a changelog)
  - Builds and publishes the docker image using the new tag

## The Dockerimage

As the image is being stored, I modified it to actually use a second slim version with the compiled code so it is smaller (and gets downloaded faster).
  • Loading branch information
Bullrich committed Mar 21, 2023
1 parent ab74b59 commit b3d0fec
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 7 deletions.
115 changes: 115 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
name: Publish package to GitHub Packages
on:
push:
branches:
- master
pull_request:

env:
IMAGE_NAME: action

jobs:
test-image:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- name: Check that the image builds
run: docker build . --file Dockerfile

test-versions:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- name: Extract package.json version
id: package_version
run: echo "VERSION=$(jq '.version' -r package.json)" >> $GITHUB_OUTPUT
- name: Extract action.yml version
uses: mikefarah/yq@master
id: action_image
with:
cmd: yq '.runs.image' 'action.yml'
- name: Parse action.yml version
id: action_version
run: |
echo "IMAGE_VERSION=$(echo $IMAGE_URL | cut -d: -f3)" >> $GITHUB_OUTPUT
env:
IMAGE_URL: ${{ steps.action_image.outputs.result }}
- name: Compare versions
run: |
echo "Verifying that $IMAGE_VERSION from action.yml is the same as $PACKAGE_VERSION from package.json"
[[ $IMAGE_VERSION == $PACKAGE_VERSION ]]
env:
IMAGE_VERSION: ${{ steps.action_version.outputs.IMAGE_VERSION }}
PACKAGE_VERSION: ${{ steps.package_version.outputs.VERSION }}

tag:
if: github.event_name == 'push'
needs: [test-image, test-versions]
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
tagcreated: ${{ steps.autotag.outputs.tagcreated }}
tagname: ${{ steps.autotag.outputs.tagname }}
steps:
- uses: actions/[email protected]
with:
fetch-depth: 0
- uses: butlerlogic/action-autotag@stable
id: autotag
with:
head_branch: master
tag_prefix: "v"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Changelog
uses: Bullrich/[email protected]
id: Changelog
env:
REPO: ${{ github.repository }}
- name: Create Release
if: steps.autotag.outputs.tagname != ''
uses: actions/create-release@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.autotag.outputs.tagname }}
release_name: Release ${{ steps.autotag.outputs.tagname }}
body: |
${{ steps.Changelog.outputs.changelog }}
publish:
runs-on: ubuntu-latest
permissions:
packages: write
needs: [tag]
if: needs.tag.outputs.tagname != ''
steps:
- uses: actions/checkout@v3
- name: Build image
run: docker build . --file Dockerfile --tag $IMAGE_NAME
- name: Log into registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
- name: Push image
run: |
IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ ! -z $TAG ]] && VERSION=$(echo $TAG | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
env:
TAG: ${{ needs.tag.outputs.tagname }}
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM node:18
FROM node:18 as Builder

WORKDIR /action

COPY package.json yarn.lock ./

Expand All @@ -8,4 +10,8 @@ COPY . .

RUN yarn build

CMD ["yarn", "start"]
FROM node:18-slim

COPY --from=Builder /action/dist /action

ENTRYPOINT ["node", "/action/index.js"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# Introduction

![Publish](https://github.com/paritytech/github-issue-sync/actions/workflows/publish.yml/badge.svg?branch=master)

This project enables syncing GitHub Issues to a [GitHub Project](https://docs.github.com/en/issues/planning-and-tracking-with-projects/learning-about-projects/about-projects).

## Why is this necessary?
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ inputs:
type: string
runs:
using: 'docker'
image: 'Dockerfile'
image: 'docker://ghcr.io/paritytech/github-issue-sync/action:0.3.1'
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "github-issue-sync",
"version": "0.0.1",
"version": "0.3.1",
"description": "Synchronize issues to GitHub Project boards",
"author": "Parity <[email protected]> (https://parity.io)",
"repository": {
"type": "git",
"url": "git+https://github.com/paritytech/github-issue-sync.git"
},
"license": "Apache-2.0",
"main": "dist/main.js",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"start": "node --experimental-modules dist/main.js",
"build": "ncc build src/main.ts",
"start": "node --experimental-modules dist/index.js",
"test": "jest",
"typecheck": "tsc --noEmit",
"fix:eslint": "eslint --fix",
Expand All @@ -29,6 +29,7 @@
"devDependencies": {
"@octokit/graphql-schema": "^12.41.1",
"@types/jest": "^29.2.6",
"@vercel/ncc": "^0.36.1",
"jest": "^29.3.1",
"jest-mock-extended": "^3.0.1",
"opstooling-js-style": "https://github.com/paritytech/opstooling-js-style#c298d0f732d93712e4397fd53baa3317a3022c8c",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,11 @@
"@typescript-eslint/types" "5.48.1"
eslint-visitor-keys "^3.3.0"

"@vercel/ncc@^0.36.1":
version "0.36.1"
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.36.1.tgz#d4c01fdbbe909d128d1bf11c7f8b5431654c5b95"
integrity sha512-S4cL7Taa9yb5qbv+6wLgiKVZ03Qfkc4jGRuiUQMQ8HGBD5pcNRnHeYM33zBvJE4/zJGjJJ8GScB+WmTsn9mORw==

acorn-jsx@^5.3.2:
version "5.3.2"
resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-5.3.2.tgz#7ed5bb55908b3b2f1bc55c6af1653bada7f07937"
Expand Down

0 comments on commit b3d0fec

Please sign in to comment.