├── .gitignore ├── Dockerfile ├── action.yml ├── run.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .npmrc 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3-alpine 2 | WORKDIR /app 3 | 4 | COPY run.py /app 5 | ENTRYPOINT ["python3", "/app/run.py"] -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: "Azure DevOps NPM" 2 | description: Setup .npmrc file for Azure DevOps" 3 | branding: 4 | icon: "align-justify" 5 | color: "orange" 6 | inputs: 7 | organisation: 8 | description: "Your Azure organisation" 9 | required: true 10 | project: 11 | description: "Your Azure project" 12 | required: false 13 | registry: 14 | description: "Your Azure registry" 15 | required: true 16 | user: 17 | description: "Your Azure user" 18 | required: true 19 | password: 20 | description: "Your Azure password" 21 | required: true 22 | email: 23 | description: "Your Azure email" 24 | required: true 25 | scope: 26 | description: "Your package scope" 27 | required: false 28 | encode_password: 29 | description: Encode the given password to base64 30 | required: false 31 | default: "false" 32 | runs: 33 | using: "docker" 34 | image: "Dockerfile" 35 | args: 36 | - "--organisation" 37 | - ${{ inputs.organisation }} 38 | - "--project" 39 | - ${{ inputs.project }} 40 | - "--registry" 41 | - ${{ inputs.registry }} 42 | - "--user" 43 | - ${{ inputs.user }} 44 | - "--password" 45 | - ${{ inputs.password }} 46 | - "--encode_password" 47 | - ${{ inputs.encode_password }} 48 | - "--email" 49 | - ${{ inputs.email }} 50 | - "--scope" 51 | - ${{ inputs.scope }} -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import base64 3 | import os 4 | 5 | def parse_args() -> argparse.Namespace: 6 | parser = argparse.ArgumentParser(description="Generate .npmrc file for Azure DevOps") 7 | parser.add_argument("--organisation", dest="organisation", required=True, help="Your Azure organisation") 8 | parser.add_argument("--project", dest="project", required=False, help="Your Azure project") 9 | parser.add_argument("--registry", dest="registry", required=True, help="Your Azure registry") 10 | parser.add_argument("--user", dest="user", required=True, help="Your Azure user") 11 | parser.add_argument("--password", dest="password", required=True, help="Your Azure password") 12 | parser.add_argument("--encode_password", dest="encode_password", default=False, help="Encode the given password to base64") 13 | parser.add_argument("--email", dest="email", required=True, help="Your Azure email") 14 | parser.add_argument("--scope", dest="scope", required=False, help="Your package scope") 15 | return parser.parse_args() 16 | 17 | def generate_url(args: argparse.Namespace): 18 | if args.project is not None: 19 | return f"pkgs.dev.azure.com/{args.organisation}/{args.project}/_packaging/{args.registry}/npm" 20 | 21 | return f"pkgs.dev.azure.com/{args.organisation}/_packaging/{args.registry}/npm" 22 | 23 | def generate_registry(args): 24 | scope = "" 25 | if args.scope is not None: 26 | scope = f"@{args.scope}:" 27 | return f"""{scope}registry=https://{generate_url(args)}/registry/ 28 | always-auth=true 29 | """ 30 | 31 | def encode_password(args): 32 | if args.encode_password: 33 | return base64.b64encode(args.password.encode('utf-8')).decode("utf-8") 34 | return args.password 35 | 36 | def generate_credentials(args): 37 | url = generate_url(args) 38 | password = encode_password(args) 39 | return f"""; begin auth token 40 | //{url}/registry/:username={args.user} 41 | //{url}/registry/:_password="{password}" 42 | //{url}/registry/:email={args.email} 43 | //{url}/:username={args.user} 44 | //{url}/:_password="{password}" 45 | //{url}/:email={args.email} 46 | ; end auth token""" 47 | 48 | def write_file(content): 49 | path = os.path.join(os.environ["GITHUB_WORKSPACE"], ".npmrc") 50 | with open(path, 'w') as f: 51 | f.write(content) 52 | 53 | def main(): 54 | args = parse_args() 55 | content = generate_registry(args) 56 | content += generate_credentials(args) 57 | write_file(content) 58 | 59 | if __name__ == "__main__": 60 | main() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🦄 Generate .npmrc file for Azure DevOps 2 | 3 | This github action generates a `.npmrc` file for Azure DevOps 4 | 5 | ```yaml 6 | - uses: ponicode/azure-devops-npm-action@master 7 | with: 8 | organisation: "Ponicode" 9 | project: "my_project" 10 | registry: "my_registry" 11 | user: "unicorn" 12 | password: ${{ secrets.AZURE_TOKEN }} 13 | email: "unicorn@ponicode.com" 14 | scope: "ponicode" 15 | ``` 16 | # How to setup (You must follow steps 1 and 2 to make the action work) 17 | ## **Step 1**: Create a yaml workflow file in your project 18 | Go to the root of your project, and create the path to your workflow file. For example 19 | 20 | ``` 21 | mkdir -p .github/workflows 22 | ``` 23 | 24 | Here is an example of what to put in your `.github/workflows/publish-npm.yml` file to trigger the action. 25 | 26 | ```yaml 27 | name: Publish on Azure NPM registry 28 | on: 29 | push: 30 | branches: [master] 31 | jobs: 32 | build: 33 | runs-on: ubuntu-latest 34 | steps: 35 | - uses: actions/checkout@v2 36 | - uses: ponicode/azure-devops-npm-action@master 37 | with: 38 | organisation: ponicode_org 39 | project: my_project 40 | registry: my_npm_registry 41 | user: unicorn 42 | password: ${{ secrets.AZURE_TOKEN }} 43 | email: unicorn@ponicode.com 44 | scope: ponicode 45 | - run: cp `pwd`/.npmrc ~ # We need the .npmrc file in the $HOME directory 46 | - name: Install dependencies 47 | run: npm install 48 | - name: Build 49 | run: npm run build 50 | - name: Publish to Azure 51 | run: npm publish 52 | ``` 53 | **This yaml file build and publish your project everytime you push on master** 54 | 55 | ## **Step 2:** Add your Azure DevOps token to github secrets 56 | Go to [Azure DevOps](https://dev.azure.com) to generate a new token. More imformation on the [offical documentation](https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page). Once your token is correctly base64 encoded, you can had it to your github secrets by following these steps: 57 | 58 | - Open your project on Github 59 | - Click on **Settings** 60 | - Click on **Secrets** 61 | - Click on **New Secret** 62 | - Name: **AZURE_TOKEN**, Value: (Paste your token from Azure) 63 | 64 | That's it! Once this is done, the action will be triggered on every push to master. 65 | 66 | # Ponicode Action inputs 67 | 68 | | Name | Description | Required | 69 | | ----------------- | ---------------------------------------------------- | -------- | 70 | | `organisation` | Your Azure organisation | true | 71 | | `project` | Your Azure project | false | 72 | | `registry` | Your Azure registry | true | 73 | | `user` | Your Azure user | true | 74 | | `password ` | Your Azure password | true | 75 | | `email` | Your Azure email | true | 76 | | `encode_password` | Encode the given password to base64 (default: false) | false | 77 | | `scope` | Your Azure scope | false | 78 | 79 | ## Contact us 80 | 81 | We would love to hear your feedback! Tell us what you loved and what you want us to improve about this action at feedback@ponicode.com, or feel free to open a Github Issue.
82 | We also have a [Slack community channel](https://ponicode-community.slack.com/join/shared_invite/zt-fiq4fhkg-DE~a_FkJ7xtiZxW7efyA4Q#/), where people can ask for help if they encounter problems with our products and where we keep you informed about our latest releases.
83 | If you want to know more about Ponicode and the different services we propose, check out our website [www.ponicode.com](https://ponicode.com)!

84 | Ponicode Logo 85 | --------------------------------------------------------------------------------