Introduction
This guide shows you how to create a basic workflow that is triggered when code is pushed to your repository.
Pour commencer Ă utiliser les flux de travail prĂ©configurĂ©s, parcourez la liste des modĂšles dans le rĂ©fĂ©rentiel actions/starter-workflows. Pour plus dâinformations, consultez « Utilisation de modĂšles de workflow ».
Important
For more information about best practices for securing your workflows and secure use of GitHub Actions features, see Secure use reference.
CrĂ©ation dâun exemple de workflow
GitHub Actions utilise la syntaxe YAML pour définir le workflow. Chaque workflow est stocké en tant que fichier YAML distinct dans votre référentiel de code, dans un répertoire appelé .github/workflows
.
Vous pouvez créer un exemple de workflow dans votre dépÎt qui déclenche automatiquement une série de commandes chaque fois que du code est poussé (push). Dans ce workflow, GitHub Actions extrait le code envoyé, installe le framework de test bats et exécute une commande de base pour générer la version de bats : bats -v
.
-
Dans votre dépÎt, créez le répertoire
.github/workflows/
pour stocker vos fichiers de workflow. -
Dans le répertoire
.github/workflows/
, créez un nouveau fichier appelélearn-github-actions.yml
et ajoutez le code suivant.YAML name: learn-github-actions run-name: ${{ github.actor }} is learning GitHub Actions on: [push] jobs: check-bats-version: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '20' - run: npm install -g bats - run: bats -v
name: learn-github-actions run-name: ${{ github.actor }} is learning GitHub Actions on: [push] jobs: check-bats-version: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '20' - run: npm install -g bats - run: bats -v
-
Validez ces modifications et poussez-les vers votre dépÎt GitHub.
Votre nouveau fichier de workflow GitHub Actions est maintenant installĂ© dans votre dĂ©pĂŽt et sâexĂ©cute automatiquement chaque fois que quelquâun pousse (push) une modification vers le dĂ©pĂŽt. Pour afficher les dĂ©tails sur lâhistorique dâexĂ©cution dâun flux de travail, consultez Affichage de lâactivitĂ© pour une exĂ©cution de flux de travail.
Présentation du fichier de workflow
Pour vous aider Ă comprendre comment la syntaxe YAML est utilisĂ©e pour crĂ©er un fichier de workflow, cette section explique chaque ligne de lâexemple dâintroduction :
# Optional - The name of the workflow as it will appear in the "Actions" tab of the GitHub repository. If this field is omitted, the name of the workflow file will be used instead. name: learn-github-actions # Optional - The name for workflow runs generated from the workflow, which will appear in the list of workflow runs on your repository's "Actions" tab. This example uses an expression with the `github` context to display the username of the actor that triggered the workflow run. For more information, see [AUTOTITLE](/actions/using-workflows/workflow-syntax-for-github-actions#run-name). run-name: ${{ github.actor }} is learning GitHub Actions # Specifies the trigger for this workflow. This example uses the `push` event, so a workflow run is triggered every time someone pushes a change to the repository or merges a pull request. This is triggered by a push to every branch; for examples of syntax that runs only on pushes to specific branches, paths, or tags, see [AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore). on: [push] # Groups together all the jobs that run in the `learn-github-actions` workflow. jobs: # Defines a job named `check-bats-version`. The child keys will define properties of the job. check-bats-version: # Configures the job to run on the latest version of an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by GitHub. For syntax examples using other runners, see [AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on) runs-on: ubuntu-latest # Groups together all the steps that run in the `check-bats-version` job. Each item nested under this section is a separate action or shell script. steps: # The `uses` keyword specifies that this step will run `v4` of the `actions/checkout` action. This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code (such as build and test tools). You should use the checkout action any time your workflow will use the repository's code. - uses: actions/checkout@v4 # This step uses the `actions/setup-node@v4` action to install the specified version of the Node.js. (This example uses version 20.) This puts both the `node` and `npm` commands in your `PATH`. - uses: actions/setup-node@v4 with: node-version: '20' # The `run` keyword tells the job to execute a command on the runner. In this case, you are using `npm` to install the `bats` software testing package. - run: npm install -g bats # Finally, you'll run the `bats` command with a parameter that outputs the software version. - run: bats -v
name: learn-github-actions
Optional - The name of the workflow as it will appear in the "Actions" tab of the GitHub repository. If this field is omitted, the name of the workflow file will be used instead.
run-name: ${{ github.actor }} is learning GitHub Actions
Optional - The name for workflow runs generated from the workflow, which will appear in the list of workflow runs on your repository's "Actions" tab. This example uses an expression with the github
context to display the username of the actor that triggered the workflow run. For more information, see Workflow syntax for GitHub Actions.
on: [push]
Specifies the trigger for this workflow. This example uses the push
event, so a workflow run is triggered every time someone pushes a change to the repository or merges a pull request. This is triggered by a push to every branch; for examples of syntax that runs only on pushes to specific branches, paths, or tags, see AUTOTITLE.
jobs:
Groups together all the jobs that run in the learn-github-actions
workflow.
check-bats-version:
Defines a job named check-bats-version
. The child keys will define properties of the job.
runs-on: ubuntu-latest
Configures the job to run on the latest version of an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by GitHub. For syntax examples using other runners, see AUTOTITLE
steps:
Groups together all the steps that run in the check-bats-version
job. Each item nested under this section is a separate action or shell script.
- uses: actions/checkout@v4
The uses
keyword specifies that this step will run v4
of the actions/checkout
action. This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code (such as build and test tools). You should use the checkout action any time your workflow will use the repository's code.
- uses: actions/setup-node@v4
with:
node-version: '20'
This step uses the actions/setup-node@v4
action to install the specified version of the Node.js. (This example uses version 20.) This puts both the node
and npm
commands in your PATH
.
- run: npm install -g bats
The run
keyword tells the job to execute a command on the runner. In this case, you are using npm
to install the bats
software testing package.
- run: bats -v
Finally, you'll run the bats
command with a parameter that outputs the software version.
# Optional - The name of the workflow as it will appear in the "Actions" tab of the GitHub repository. If this field is omitted, the name of the workflow file will be used instead.
name: learn-github-actions
# Optional - The name for workflow runs generated from the workflow, which will appear in the list of workflow runs on your repository's "Actions" tab. This example uses an expression with the `github` context to display the username of the actor that triggered the workflow run. For more information, see [AUTOTITLE](/actions/using-workflows/workflow-syntax-for-github-actions#run-name).
run-name: ${{ github.actor }} is learning GitHub Actions
# Specifies the trigger for this workflow. This example uses the `push` event, so a workflow run is triggered every time someone pushes a change to the repository or merges a pull request. This is triggered by a push to every branch; for examples of syntax that runs only on pushes to specific branches, paths, or tags, see [AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore).
on: [push]
# Groups together all the jobs that run in the `learn-github-actions` workflow.
jobs:
# Defines a job named `check-bats-version`. The child keys will define properties of the job.
check-bats-version:
# Configures the job to run on the latest version of an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by GitHub. For syntax examples using other runners, see [AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on)
runs-on: ubuntu-latest
# Groups together all the steps that run in the `check-bats-version` job. Each item nested under this section is a separate action or shell script.
steps:
# The `uses` keyword specifies that this step will run `v4` of the `actions/checkout` action. This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code (such as build and test tools). You should use the checkout action any time your workflow will use the repository's code.
- uses: actions/checkout@v4
# This step uses the `actions/setup-node@v4` action to install the specified version of the Node.js. (This example uses version 20.) This puts both the `node` and `npm` commands in your `PATH`.
- uses: actions/setup-node@v4
with:
node-version: '20'
# The `run` keyword tells the job to execute a command on the runner. In this case, you are using `npm` to install the `bats` software testing package.
- run: npm install -g bats
# Finally, you'll run the `bats` command with a parameter that outputs the software version.
- run: bats -v
Visualisation du fichier de workflow
Dans ce diagramme, vous pouvez voir le fichier de workflow que vous venez de crĂ©er et comment les composants GitHub Actions sont organisĂ©s dans une hiĂ©rarchie. Chaque Ă©tape exĂ©cute une action ou un script dâinterprĂ©teur de commandes unique. Les Ă©tapes 1 et 2 exĂ©cutent des actions, tandis que les Ă©tapes 3 et 4 exĂ©cutent des scripts dâinterprĂ©teur de commandes. Pour trouver dâautres actions prĂ©dĂ©finies destinĂ©es Ă vos flux de travail, consultez Utilisation de blocs Ă©lĂ©mentaires prĂ©-Ă©crits dans votre workflow.
Affichage de lâactivitĂ© pour une exĂ©cution de workflow
Lorsque votre workflow est dĂ©clenchĂ©, une exĂ©cution de workflow est créée et exĂ©cute le workflow. Une fois lâexĂ©cution de votre workflow dĂ©marrĂ©e, vous pouvez voir un graphe de visualisation de la progression de lâexĂ©cution, ainsi que lâactivitĂ© de chaque Ă©tape sur GitHub.
-
Sur GitHub, accédez à la page principale du référentiel.
-
Sous le nom de votre dépÎt, cliquez sur Actions.
-
Dans la barre latérale gauche, cliquez sur le workflow que vous souhaitez afficher.
-
Dans la liste des exĂ©cutions de workflow, cliquez sur le nom de lâexĂ©cution pour voir le rĂ©sumĂ© de lâexĂ©cution du workflow.
-
Dans la barre latérale à gauche ou dans le graphe de visualisation, cliquez sur le travail que vous souhaitez voir.
-
Pour voir les rĂ©sultats dâune Ă©tape, cliquez sur lâĂ©tape.