Remarque : GitHub Packages est actuellement en version bêta pour GitHub AE.
À propos de GitHub Packages avec GitHub Actions
GitHub Actions vous aide à automatiser vos workflows de développement logiciel au même endroit où vous stockez le code et collaborez sur les demandes de tirage et les problèmes. Vous pouvez écrire des tâches individuelles, appelées actions, et les combiner pour créer un workflow personnalisé. Avec GitHub Actions, vous pouvez créer des fonctionnalités d’intégration continue (CI) et de déploiement continu (CD) de bout en bout directement dans votre dépôt. Pour plus d’informations, consultez « Découvrir GitHub Actions ».
Vous pouvez étendre les fonctionnalités CI et CD de votre dépôt en publiant ou en installant des packages dans le cadre de votre workflow.
Pour s’authentifier auprès des registres de packages sur GitHub AE, nous recommandons d’utiliser GITHUB_TOKEN
que GitHub AE crée automatiquement pour votre dépôt quand vous activez GitHub Actions. Vous devez définir les autorisations de ce jeton d’accès dans le fichier de workflow afin d’octroyer l’accès en lecture pour l’étendue contents
et l’accès en écriture pour l’étendue packages
. Pour les duplications, GITHUB_TOKEN
reçoit un accès en lecture pour le dépôt parent. Pour plus d’informations, consultez « Authentification par jeton automatique ».
Vous pouvez référencer le GITHUB_TOKEN
de votre fichier de workflow à l’aide du contexte {{secrets.GITHUB_TOKEN}}
. Pour plus d’informations, consultez « Authentification par jeton automatique ».
À propos des autorisations et de l’accès aux packages
Lorsque vous activez GitHub Actions, GitHub installe une application GitHub sur votre dépôt. Le secret GITHUB_TOKEN
est un jeton d’accès d’installation d’application GitHub. Vous pouvez utiliser le jeton d’accès d’installation pour vous authentifier au nom de l’application GitHub installée sur votre dépôt. Les autorisations du jeton sont limitées au dépôt qui contient votre workflow. Pour plus d’informations, consultez « Authentification par jeton automatique ».
GitHub Packages vous permet d’envoyer (push) et d’extraire des packages via le secret GITHUB_TOKEN
disponible pour un workflow GitHub Actions.
Publication d’un package à l’aide d’une action
Vous pouvez utiliser GitHub Actions pour publier automatiquement des packages dans le cadre de votre flux d’intégration continue (CI). Cette approche du déploiement continu (CD) vous permet d’automatiser la création de nouvelles versions de package si le code répond à vos normes de qualité. Par exemple, vous pourriez créer un workflow qui exécute des tests de CI chaque fois qu’un développeur envoie du code à une branche particulière. Si les tests réussissent, le workflow peut publier une nouvelle version de package sur GitHub Packages.
Les étapes de configuration varient selon le client de package. Pour des informations générales sur la configuration d’un workflow pour GitHub Actions, consultez « Utilisation de flux de travail ».
L’exemple suivant montre comment utiliser GitHub Actions pour générer votre application and test, puis créer automatiquement une image Docker et la publier sur GitHub Packages. Les paramètres pertinents sont expliqués dans le code. Pour avoir des détails complets sur chaque élément d’un workflow, consultez « Workflow syntax for GitHub Actions ».
Créez un fichier de workflow dans votre dépôt (par exemple .github/workflows/deploy-image.yml
), puis ajoutez le YAML suivant.
Remarques :
- Ce workflow utilise des actions qui ne sont pas certifiées par GitHub. Elles sont fournies par un tiers et sont régies par des conditions d’utilisation du service, une politique de confidentialité et une documentation de support distinctes.
- GitHub recommande d’épingler les actions à un SHA de commit. Pour obtenir une version plus récente, vous devez mettre à jour le SHA. Vous pouvez également référencer une balise ou une branche, mais l’action peut changer sans avertissement.
# name: Create and publish a Docker image # Configures this workflow to run every time a change is pushed to the branch called `release`. on: push: branches: ['release'] jobs: # This job checks out the repository contents, installs `npm`, uses npm and webpack to build the app, and uploads the built files as an artifact that can be downloaded later in the workflow. # It assumes that the built files are written to a directory called `public`. run-npm-build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: npm install and build webpack run: | npm install npm run build - uses: actions/upload-artifact@v3 with: name: webpack artifacts path: public/ # This job uses `npm test` to test the code. `needs: run-npm-build` makes this job dependent on the `run-npm-build` job. run-npm-test: runs-on: ubuntu-latest needs: run-npm-build strategy: matrix: os: [ubuntu-latest] node-version: [14.x, 16.x] steps: - uses: actions/checkout@v4 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - uses: actions/download-artifact@v3 with: name: webpack artifacts path: public - name: npm install, and test run: | npm install npm test env: CI: true # This job publishes the package. `needs: run-npm-test` makes this job dependent on the `run-npm-test` job. build-and-push-image: runs-on: ubuntu-latest needs: run-npm-test # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. permissions: contents: read packages: write # steps: - name: Checkout uses: actions/checkout@v4 # Uses the `docker/login-action` action to log in to the registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. - name: Log in to GitHub Docker Registry uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 with: registry: docker.YOUR-HOSTNAME.com username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. # It uses the `tags` parameter to tag the image with the SHA of the commit that triggered the workflow. - name: Build and push Docker image uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 with: push: true tags: | docker.YOUR-HOSTNAME.com/${{ github.repository }}/octo-image:${{ github.sha }}
name: Create and publish a Docker image
on:
push:
branches: ['release']
jobs:
Configures this workflow to run every time a change is pushed to the branch called release
.
run-npm-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: npm install and build webpack
run: |
npm install
npm run build
- uses: actions/upload-artifact@v3
with:
name: webpack artifacts
path: public/
This job checks out the repository contents, installs npm
, uses npm and webpack to build the app, and uploads the built files as an artifact that can be downloaded later in the workflow.
It assumes that the built files are written to a directory called public
.
run-npm-test:
runs-on: ubuntu-latest
needs: run-npm-build
strategy:
matrix:
os: [ubuntu-latest]
node-version: [14.x, 16.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- uses: actions/download-artifact@v3
with:
name: webpack artifacts
path: public
- name: npm install, and test
run: |
npm install
npm test
env:
CI: true
This job uses npm test
to test the code. needs: run-npm-build
makes this job dependent on the run-npm-build
job.
build-and-push-image:
runs-on: ubuntu-latest
needs: run-npm-test
This job publishes the package. needs: run-npm-test
makes this job dependent on the run-npm-test
job.
permissions:
contents: read
packages: write
Sets the permissions granted to the GITHUB_TOKEN
for the actions in this job.
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Log in to GitHub Docker Registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: docker.YOUR-HOSTNAME.com
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
Uses the docker/login-action
action to log in to the registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
push: true
tags: |
docker.YOUR-HOSTNAME.com/${{ github.repository }}/octo-image:${{ github.sha }}
This step uses the docker/build-push-action
action to build the image, based on your repository's Dockerfile
. If the build succeeds, it pushes the image to GitHub Packages.
It uses the tags
parameter to tag the image with the SHA of the commit that triggered the workflow.
#
name: Create and publish a Docker image
# Configures this workflow to run every time a change is pushed to the branch called `release`.
on:
push:
branches: ['release']
jobs:
# This job checks out the repository contents, installs `npm`, uses npm and webpack to build the app, and uploads the built files as an artifact that can be downloaded later in the workflow.
# It assumes that the built files are written to a directory called `public`.
run-npm-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: npm install and build webpack
run: |
npm install
npm run build
- uses: actions/upload-artifact@v3
with:
name: webpack artifacts
path: public/
# This job uses `npm test` to test the code. `needs: run-npm-build` makes this job dependent on the `run-npm-build` job.
run-npm-test:
runs-on: ubuntu-latest
needs: run-npm-build
strategy:
matrix:
os: [ubuntu-latest]
node-version: [14.x, 16.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- uses: actions/download-artifact@v3
with:
name: webpack artifacts
path: public
- name: npm install, and test
run: |
npm install
npm test
env:
CI: true
# This job publishes the package. `needs: run-npm-test` makes this job dependent on the `run-npm-test` job.
build-and-push-image:
runs-on: ubuntu-latest
needs: run-npm-test
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
permissions:
contents: read
packages: write
#
steps:
- name: Checkout
uses: actions/checkout@v4
# Uses the `docker/login-action` action to log in to the registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
- name: Log in to GitHub Docker Registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: docker.YOUR-HOSTNAME.com
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
# It uses the `tags` parameter to tag the image with the SHA of the commit that triggered the workflow.
- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
push: true
tags: |
docker.YOUR-HOSTNAME.com/${{ github.repository }}/octo-image:${{ github.sha }}
Ce nouveau workflow s’exécute automatiquement chaque fois que vous envoyez une modification à une branche nommée release
dans le dépôt. Vous pouvez visualiser la progression sous l’onglet Actions.
Quelques minutes après la fin du workflow, le nouveau package sera visible dans votre dépôt. Pour trouver vos packages disponibles, consultez « Affichage de packages ».
Installation d’un package à l’aide d’une action
Vous pouvez installer des packages dans le cadre de votre flux de CI en utilisant GitHub Actions. Par exemple, vous pourriez configurer un workflow de façon à ce que, chaque fois qu’un développeur envoie (push) du code à une demande de tirage, le workflow résolve les dépendances en téléchargeant et en installant des packages hébergés par GitHub Packages. Ensuite, le workflow peut exécuter des tests de CI qui nécessitent les dépendances.
L’installation de packages hébergés par GitHub Packages via GitHub Actions nécessite une configuration minimale ou une authentification supplémentaire lorsque vous utilisez le secret GITHUB_TOKEN
.
Les étapes de configuration varient selon le client de package. Pour des informations générales sur la configuration d’un workflow pour GitHub Actions, consultez « Utilisation de flux de travail ».