Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
skorfmann committed Jun 21, 2023
1 parent e8d1be6 commit 1ec3428
Show file tree
Hide file tree
Showing 22 changed files with 4,453 additions and 6,320 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ dist/
lib/
node_modules/
jest.config.js
__tests__/
plugins/
examples/
23 changes: 20 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,25 @@ jobs:
npm run all
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
# env: # set the env vars for the action
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# AWS_DEFAULT_REGION: 'us-east-1'
# TF_BACKEND_BUCKET: <your-bucket-name>
# TF_BACKEND_BUCKET_REGION: 'us-east-1'
steps:
- uses: actions/checkout@v3
- uses: ./
with:
milliseconds: 1000
# local testing only for now
# - uses: ./
# name: "Plain Wing app"
# with:
# entry: './examples/plain/main.w'
# target: 'tf-aws'
# backend: 's3'
# - uses: ./
# name: "Wing app with npm dependencies"
# with:
# entry: 'main.w'
# working-directory: 'examples/with-dependencies'
# target: 'tf-aws'
# backend: 's3'
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ Thumbs.db

# Ignore built ts files
__tests__/runner/*
lib/**/*
lib/**/*

target
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:18-slim

RUN apt-get update -y && apt-get install -y unzip curl

ENV TF_PLUGIN_CACHE_DIR="/root/.terraform.d/plugin-cache"
ENV TERRAFORM_VERSION="1.5.0"

RUN mkdir -p ${TF_PLUGIN_CACHE_DIR}

# Install Terraform
RUN curl -LOk https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip && \
mkdir -p /usr/local/bin/tf/versions/${TERRAFORM_VERSION} && \
unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/local/bin/tf/versions/${TERRAFORM_VERSION} && \
ln -s /usr/local/bin/tf/versions/${TERRAFORM_VERSION}/terraform /usr/local/bin/terraform

COPY ./dist ./dist
COPY ./plugins ./plugins

ENTRYPOINT ["node", "/dist/index.js"]
139 changes: 65 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,105 +1,96 @@
<p align="center">
<a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a>
<a href="https://github.com/winglang/wing-github-action/actions"><img alt="wing-github-action status" src="https://github.com/winglang/wing-github-action/workflows/build-test/badge.svg"></a>
</p>

# Create a JavaScript Action using TypeScript
# Winglang Github Action

Use this template to bootstrap the creation of a TypeScript action.:rocket:
The 'Winglang Deployment Github Action' is a powerful tool that allows you to seamlessly deploy your Winglang code to a cloud target of your choice directly from your GitHub workflows.

This template includes compilation support, tests, a validation workflow, publishing, and versioning guidance.
## Usage

If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action)
To use the 'Winglang Deployment Github Action' in your workflow, add the following step:

## Create an action from this template

Click the `Use this Template` and provide the new repo details for your action

## Code in Main

> First, you'll need to have a reasonably modern version of `node` handy. This won't work with versions older than 9, for instance.
Install the dependencies
```bash
$ npm install
```

Build the typescript and package it for distribution
```bash
$ npm run build && npm run package
```yaml
steps:
- name: Deploy Winglang App
uses: winglang/wing-github-action@main
with:
entry: 'main.wing' # Required, replace this with your entry file if different
target: 'tf-aws' # Required, the target to deploy to. e.g. tf-aws, tf-gcp, tf-azure or awscdk.
version: 'latest' # Optional, specify a different version of the Winglang CLI if required
working-directory: '' # Optional, the working directory to use. e.g. ./examples/with-dependencies. Will set backend-scope to the relative path of the working directory.
backend: 's3' # Optional, currently only 's3' is supported
backendScope: '' # Optional, allows setting a postfix to the generated state file name. Useful if multiple wing apps are deployed from the same repo
env:
TF_BACKEND_BUCKET: '<your-bucket-name>' # required, only required if s3 backend is
TF_BACKEND_BUCKET_REGION: '<your-bucket-region>' # required, only required if s3 backend is
```
Run the tests :heavy_check_mark:
```bash
$ npm test

PASS ./index.test.js
✓ throws invalid number (3ms)
wait 500 ms (504ms)
test runs (95ms)
A minimal working config for [AWS with OIDC](https://github.com/aws-actions/configure-aws-credentials) could look like this and deploy a `main.w` Wing application.

...
```yaml
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role
aws-region: us-east-1
- name: Deploy Winglang App
uses: winglang/wing-github-action@main
with:
entry: 'main.w'
target: 'tf-aws'
env:
TF_BACKEND_BUCKET: my-tf-state-bucket-name
TF_BACKEND_BUCKET_REGION: us-east-1
```

## Change action.yml
### Environment Variables

The action.yml defines the inputs and output for your action.
- `TF_BACKEND_BUCKET`: The name of your S3 bucket used for the Terraform backend.
- `TF_BACKEND_BUCKET_REGION`: The region of your S3 bucket.
- `AWS_ACCESS_KEY_ID`: The AWS Access Key ID for your account.
- `AWS_SECRET_ACCESS_KEY`: The AWS Secret Access Key for your account.

Update the action.yml with your name, description, inputs and outputs for your action.
**Note:** If you're using the `s3` backend, the `TF_BACKEND_BUCKET` and `TF_BACKEND_BUCKET_REGION` environment variables need to be set with appropriate values. These credentials must have access to the specified S3 bucket.

See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions)
For better security, it is recommended to use GitHub's OpenID Connect service with Amazon Web Services. It is a security-hardened service for getting temporary credentials. You can find more about it here: [Configuring OpenID Connect in Amazon Web Services](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services) and [configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials).

## Change the Code
### Node Dependencies

Most toolkit and CI/CD operations involve async operations so the action is run in an async function.
This Action runs in a Docker container with Node v18.

```javascript
import * as core from '@actions/core';
...
Dependencies are automatically installed via NPM. Not yet implemented: Please make sure to add the `packageManager` field to your `package.json` file if you're using anything else than NPM.

async function run() {
try {
...
}
catch (error) {
core.setFailed(error.message);
}
}

run()
```

See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.

## Publish to a distribution branch

Actions are run from GitHub repos so we will checkin the packed dist folder.

Then run [ncc](https://github.com/zeit/ncc) and push the results:
```bash
$ npm run package
$ git add dist
$ git commit -a -m "prod dependencies"
$ git push origin releases/v1
{
"packageManager": "[email protected]"
}
```

Note: We recommend using the `--license` option for ncc, which will create a license file for all of the production node modules used in your project.
### Terraform

Your action is now published! :rocket:
This Action includes a recent version of Terraform (v1.5.0).

See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
## Development

## Validate
Setup:

You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml))
- a working Docker setup
- [act](https://github.com/nektos/act) for local Action testing

```yaml
uses: ./
with:
milliseconds: 1000
```
npm install
npm run all
act -j test ./.github/workflows/test.yml -s AWS_SECRET_ACCESS_KEY=<value> -s AWS_ACCESS_KEY_ID=<value>
```

## Notes

See the [actions tab](https://github.com/actions/typescript-action/actions) for runs of this action! :rocket:
- Github Actions - without [Docker](https://docs.github.com/en/actions/creating-actions/dockerfile-support-for-github-actions) - support Node [v16 only](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runsusing-for-javascript-actions) at this point.
- Having a known version of Terraform as part of the Docker image is helpful, needs an automated update worklfow though
-

## Usage:
## Roadmap

After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action
- [ ] Publish Dockerimage so it's not building the image all the time (can be used like [this](https://github.com/hashicorp/tfc-workflows-github/blob/c198b4e6a2c69feba9cf62940e80b7e458884c9c/actions/upload-configuration/action.yml#L45-L46))
31 changes: 4 additions & 27 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
import {wait} from '../src/wait'
import * as process from 'process'
import * as cp from 'child_process'
import * as path from 'path'
import {expect, test} from '@jest/globals'
import {} from '../src/main'
import {expect, test} from 'vitest'

test('throws invalid number', async () => {
const input = parseInt('foo', 10)
await expect(wait(input)).rejects.toThrow('milliseconds not a number')
})

test('wait 500 ms', async () => {
const start = new Date()
await wait(500)
const end = new Date()
var delta = Math.abs(end.getTime() - start.getTime())
expect(delta).toBeGreaterThan(450)
})

// shows how the runner will run a javascript action with env / stdout protocol
test('test runs', () => {
process.env['INPUT_MILLISECONDS'] = '500'
const np = process.execPath
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecFileSyncOptions = {
env: process.env
}
console.log(cp.execFileSync(np, [ip], options).toString())
test('true', async () => {
expect(true).toBe(true)
})
36 changes: 28 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
name: 'Your name here'
description: 'Provide a description here'
author: 'Your name or organization here'
name: 'Winglang Deployment Github Action'
description: 'Deploy your Winglang code with Github Actions to a cloud target of your choice.'
author: 'Sebastian Korfmann'
inputs:
milliseconds: # change this
entry:
required: true
description: 'input description here'
default: 'default value if applicable'
description: 'The entry file to use. e.g. main.wing'
default: ''
backend:
required: false
description: 'Which backend to inject. Right now only s3 is supported.'
default: 's3'
backend-scope:
required: false
description: 'Used as postfix for the backend state key. e.g. my-bucket/gh-user/gh-repo/gh-branch/backend-scope'
default: ''
working-directory:
required: false
description: 'The working directory to use. e.g. ./examples/with-dependencies. Will set backend-scope to the relative path of the cwd.'
default: ''
version:
required: false
description: 'The version of the Winglang CLI to use.'
default: 'latest'
target:
required: true
description: 'The target to deploy to. e.g. tf-aws, tf-gcp, tf-azure or awscdk.'
default: ''
runs:
using: 'node16'
main: 'dist/index.js'
using: 'docker'
image: 'Dockerfile'
Loading

0 comments on commit 1ec3428

Please sign in to comment.